@@ -3,7 +3,33 @@ import { Router } from 'express';
33
44import { SocketServer } from '../../socket-server.js' ;
55
6- export function createCommandHandler ( command : string , args ?: string ) : Router {
6+ export enum ConnectCommand {
7+ TERMINAL = 'terminal' ,
8+ APPLY = 'apply' ,
9+ PLAN = 'plan' ,
10+ IMPORT = 'import'
11+ }
12+
13+ const CommandInfo = {
14+ [ ConnectCommand . TERMINAL ] : {
15+ args : [ ] ,
16+ } ,
17+ [ ConnectCommand . APPLY ] : {
18+ args : [ '-c' , 'codify apply' ]
19+ }
20+
21+ }
22+
23+ 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+ // }
32+
733 const router = Router ( {
834 mergeParams : true ,
935 } ) ;
@@ -22,11 +48,15 @@ export function createCommandHandler(command: string, args?: string): Router {
2248 return res . status ( 400 ) . json ( { error : 'SessionId does not exist' } ) ;
2349 }
2450
25- const { ws, server} = session ;
51+ const { ws, server } = session ;
2652 if ( ! ws ) {
2753 return res . status ( 400 ) . json ( { error : 'SessionId not open' } ) ;
2854 }
2955
56+ if ( session . pty ) {
57+ return res . status ( 304 ) . json ( { status : 'Already started' } )
58+ }
59+
3060 const pty = spawn ( 'zsh' , [ ] , {
3161 name : 'xterm-color' ,
3262 cols : 80 ,
@@ -35,6 +65,8 @@ export function createCommandHandler(command: string, args?: string): Router {
3565 env : process . env
3666 } ) ;
3767
68+ session . pty = pty ;
69+
3870 pty . onData ( ( data ) => {
3971 ws . send ( Buffer . from ( data , 'utf8' ) ) ;
4072 } ) ;
@@ -48,6 +80,9 @@ export function createCommandHandler(command: string, args?: string): Router {
4880 ws . terminate ( ) ;
4981 server . close ( ) ;
5082 } )
83+
84+
85+ return res . status ( 204 ) . json ( { } ) ;
5186 } ) ;
5287
5388 return router ;
0 commit comments