Skip to content

Commit 989cad3

Browse files
committed
Added new finish event to unify sending the remote result back to the dashboard
1 parent 028766a commit 989cad3

4 files changed

Lines changed: 12 additions & 25 deletions

File tree

src/connect/http-routes/create-command.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface Params {
1717
name: ConnectCommand;
1818
command?: string[];
1919
spawnCommand?: (body: Record<string, unknown>, ws: WebSocket, session: Session) => IPty | Promise<IPty>;
20-
onExit?: (exitCode: number, ws: WebSocket, session: Session) => Promise<void> | void;
20+
onExit?: (exitCode: number, ws: WebSocket, session: Session) => Promise<Record<string, unknown> | undefined | void>;
2121
}
2222

2323
export function createCommandHandler({ name, command, spawnCommand, onExit }: Params): Router {
@@ -47,7 +47,7 @@ export function createCommandHandler({ name, command, spawnCommand, onExit }: Pa
4747
return res.status(400).json({ error: 'SessionId does not exist' });
4848
}
4949

50-
const { ws, server } = session;
50+
const { ws, server, clientId } = session;
5151
if (!ws) {
5252
return res.status(400).json({ error: 'SessionId not open' });
5353
}
@@ -80,7 +80,11 @@ export function createCommandHandler({ name, command, spawnCommand, onExit }: Pa
8080
console.log(`Command ${name} exited with exit code`, exitCode);
8181
ws.send(Buffer.from(chalk.blue(`Session ended exit code ${exitCode}`), 'utf8'))
8282

83-
await onExit?.(exitCode, ws, session)
83+
const mainWs = SocketServer.get().getMainConnection(clientId);
84+
if (mainWs) {
85+
const data = await onExit?.(exitCode, ws, session) ?? {}
86+
mainWs.send(JSON.stringify({ key: `finish:${sessionId}`, success: exitCode === 0, data })) // Send finish command only if client connection is still open
87+
}
8488

8589
ws.terminate();
8690
server.close();

src/connect/http-routes/handlers/import-handler.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,10 @@ export function importHandler() {
7676
if (!ws) {
7777
throw new Error(`Unable to find client for clientId ${session.clientId}`);
7878
}
79-
80-
ws.send(JSON.stringify({ key: 'new_import', data: {
81-
updated: updatedFile,
82-
} }))
8379
}
8480

85-
8681
await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true });
82+
return { updated: updatedFile }
8783
}
8884
}
8985

src/connect/http-routes/handlers/init-handler.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ export function initHandler() {
3939
throw new Error(`Unable to find client for clientId ${session.clientId}`);
4040
}
4141

42-
ws.send(JSON.stringify({ key: 'new_init', data: {
43-
updated: updatedFile,
44-
} }))
42+
await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true });
43+
return { updated: updatedFile };
4544
}
46-
47-
48-
await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true });
4945
}
5046
}
5147

src/connect/http-routes/handlers/refresh-handler.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,9 @@ export function refreshHandler() {
7272
if (diffChars(updatedFile, session.additionalData.existingFile as string).length > 0) {
7373
console.log('Writing imported changes to Codify dashboard');
7474

75-
const ws = SocketServer.get().getMainConnection(session.clientId);
76-
if (!ws) {
77-
throw new Error(`Unable to find client for clientId ${session.clientId}`);
78-
}
79-
80-
ws.send(JSON.stringify({ key: 'new_import', data: {
81-
updated: updatedFile,
82-
} }))
75+
await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true });
76+
return { updated: updatedFile };
8377
}
84-
85-
86-
await fs.rm(session.additionalData.filePath as string, { recursive: true, force: true });
8778
}
8879
}
8980

0 commit comments

Comments
 (0)