1- import { spawn } from '@homebridge/node-pty-prebuilt-multiarch' ;
1+ import { IPty , spawn } from '@homebridge/node-pty-prebuilt-multiarch' ;
22import chalk from 'chalk' ;
33import { Router } from 'express' ;
44
55import { ConnectOrchestrator } from '../../../orchestrators/connect.js' ;
6- import { SocketServer } from '../../socket-server.js' ;
6+ import { Session , SocketServer } from '../../socket-server.js' ;
7+ import WebSocket from 'ws' ;
78
89export enum ConnectCommand {
910 TERMINAL = 'terminal' ,
@@ -12,33 +13,20 @@ export enum ConnectCommand {
1213 IMPORT = 'import'
1314}
1415
15- const CommandInfo = {
16- [ ConnectCommand . TERMINAL ] : {
17- command : ( ) => [ ] ,
18- requiresDocumentId : false ,
19- } ,
20- [ ConnectCommand . APPLY ] : {
21- command : ( args ) => [ '-c' , `${ ConnectOrchestrator . rootCommand } apply ${ args } ` ] ,
22- requiresDocumentId : true ,
23- } ,
24- [ ConnectCommand . PLAN ] : {
25- command : ( args ) => [ '-c' , `${ ConnectOrchestrator . rootCommand } plan ${ args } ` ] ,
26- requiresDocumentId : true ,
27- } ,
28- [ ConnectCommand . IMPORT ] : {
29- command : ( args ) => [ '-c' , `${ ConnectOrchestrator . rootCommand } import -p ${ args } ` ] ,
30- requiresDocumentId : true ,
31- }
16+ interface Params {
17+ name : ConnectCommand ;
18+ command ?: string [ ] ;
19+ spawnCommand ?: ( body : Record < string , unknown > , ws : WebSocket , session : Session ) => IPty | Promise < IPty > ;
20+ onExit ?: ( exitCode : number , ws : WebSocket , session : Session ) => Promise < void > | void ;
3221}
3322
34- export function createCommandHandler ( command : ConnectCommand ) : Router {
35- if ( ! Object . values ( ConnectCommand ) . includes ( command ) ) {
36- throw new Error ( `Unknown command ${ command } . Please check code` ) ;
23+ export function createCommandHandler ( { name , command, spawnCommand , onExit } : Params ) : Router {
24+ if ( ! Object . values ( ConnectCommand ) . includes ( name ) ) {
25+ throw new Error ( `Unknown command ${ name } . Please check code` ) ;
3726 }
3827
39- const commandInfo = CommandInfo [ command ] ;
40- if ( ! commandInfo ) {
41- throw new Error ( `Command info not provided for ${ command } . Please check code` ) ;
28+ if ( ! command && ! spawnCommand ) {
29+ throw new Error ( 'One of command or spawnCommand must be provided to createCommandHandler' ) ;
4230 }
4331
4432 const router = Router ( {
@@ -47,17 +35,12 @@ export function createCommandHandler(command: ConnectCommand): Router {
4735
4836 router . post ( '/:sessionId/start' , async ( req , res ) => {
4937 const { sessionId } = req . params ;
50- const { documentId } = req . body ;
51- console . log ( `Received request to ${ command } , sessionId: ${ sessionId } ` )
38+ console . log ( `Received request to ${ name } , sessionId: ${ sessionId } ` )
5239
5340 if ( ! sessionId ) {
5441 return res . status ( 400 ) . json ( { error : 'SessionId must be provided' } ) ;
5542 }
5643
57- if ( commandInfo . requiresDocumentId && ! documentId ) {
58- return res . status ( 400 ) . json ( { error : 'Document id must be provided' } ) ;
59- }
60-
6144 const manager = SocketServer . get ( ) ;
6245 const session = manager . getSession ( sessionId ) ;
6346 if ( ! session ) {
@@ -73,8 +56,9 @@ export function createCommandHandler(command: ConnectCommand): Router {
7356 return res . status ( 304 ) . json ( { status : 'Already started' } )
7457 }
7558
76- console . log ( 'Running command:' , commandInfo . command ( documentId ) )
77- const pty = spawn ( 'zsh' , commandInfo . command ( documentId ) , {
59+ console . log ( req . body ) ;
60+
61+ const pty = spawnCommand ? await spawnCommand ( req . body , ws , session ) : spawn ( 'zsh' , command ! , {
7862 name : 'xterm-color' ,
7963 cols : 80 ,
8064 rows : 30 ,
@@ -92,10 +76,12 @@ export function createCommandHandler(command: ConnectCommand): Router {
9276 pty . write ( message . toString ( 'utf8' ) ) ;
9377 } )
9478
95- pty . onExit ( ( { exitCode, signal } ) => {
96- console . log ( 'pty exit' , exitCode , signal ) ;
79+ pty . onExit ( async ( { exitCode, signal } ) => {
80+ console . log ( `Command ${ name } exited with exit code` , exitCode ) ;
9781 ws . send ( Buffer . from ( chalk . blue ( `Session ended exit code ${ exitCode } ` ) , 'utf8' ) )
9882
83+ await onExit ?.( exitCode , ws , session )
84+
9985 ws . terminate ( ) ;
10086 server . close ( ) ;
10187 } )
0 commit comments