Skip to content

Commit 2cf0b95

Browse files
committed
Improved the command handler to support more variations
1 parent 5984c56 commit 2cf0b95

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,50 @@ export enum ConnectCommand {
1212

1313
const CommandInfo = {
1414
[ConnectCommand.TERMINAL]: {
15-
args: [],
15+
command: () => [],
16+
requiresDocumentId: false,
1617
},
1718
[ConnectCommand.APPLY]: {
18-
args: ['-c', 'codify apply']
19+
command: (args) => ['-c', `codify apply ${args}`],
20+
requiresDocumentId: true,
21+
},
22+
[ConnectCommand.PLAN]: {
23+
command: (args) => ['-c', `codify plan ${args}`],
24+
requiresDocumentId: true,
25+
},
26+
[ConnectCommand.IMPORT]: {
27+
command: (args) => ['-c', `codify import ${args}`],
28+
requiresDocumentId: true,
1929
}
20-
2130
}
2231

2332
export function createCommandHandler(command: ConnectCommand): Router {
24-
// if (!Object.values(ConnectCommand).includes(command)) {
25-
// throw new Error(`Unknown command ${command}. Please check code`);
26-
// }
27-
//
28-
// const commandInfo = CommandInfo[command];
29-
// if (!commandInfo) {
30-
// throw new Error(`Command info not provided for ${command}. Please check code`);
31-
// }
33+
if (!Object.values(ConnectCommand).includes(command)) {
34+
throw new Error(`Unknown command ${command}. Please check code`);
35+
}
36+
37+
const commandInfo = CommandInfo[command];
38+
if (!commandInfo) {
39+
throw new Error(`Command info not provided for ${command}. Please check code`);
40+
}
3241

3342
const router = Router({
3443
mergeParams: true,
3544
});
3645

3746
router.post('/:sessionId/start', async (req, res) => {
3847
const { sessionId } = req.params;
48+
const { documentId } = req.body;
3949
console.log(`Received request to ${command}, sessionId: ${sessionId}`)
4050

4151
if (!sessionId) {
4252
return res.status(400).json({ error: 'SessionId must be provided' });
4353
}
4454

55+
if (commandInfo.requiresDocumentId && !documentId) {
56+
return res.status(400).json({ error: 'Document id must be provided' });
57+
}
58+
4559
const manager = SocketServer.get();
4660
const session = manager.getSession(sessionId);
4761
if (!session) {
@@ -57,7 +71,8 @@ export function createCommandHandler(command: ConnectCommand): Router {
5771
return res.status(304).json({ status: 'Already started' })
5872
}
5973

60-
const pty = spawn('zsh', [], {
74+
console.log('Running command:', commandInfo.command(documentId))
75+
const pty = spawn('zsh', commandInfo.command(documentId), {
6176
name: 'xterm-color',
6277
cols: 80,
6378
rows: 30,

src/connect/http-routes/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Router } from 'express';
22

3-
import { createCommandHandler } from './handlers/create-command.js';
3+
import { ConnectCommand, createCommandHandler } from './handlers/create-command.js';
44
import defaultHandler from './handlers/index.js';
55

66
const router = Router();
77

88
router.use('/', defaultHandler);
9-
router.use('/apply', createCommandHandler('apply'));
10-
router.use('/plan', createCommandHandler('plan'));
11-
router.use('/import', createCommandHandler('import'));
12-
router.use('/terminal', createCommandHandler(''));
9+
router.use('/apply', createCommandHandler(ConnectCommand.APPLY));
10+
router.use('/plan', createCommandHandler(ConnectCommand.PLAN))
11+
router.use('/import', createCommandHandler(ConnectCommand.IMPORT));
12+
router.use('/terminal', createCommandHandler(ConnectCommand.TERMINAL));
1313

1414
export default router;

0 commit comments

Comments
 (0)