diff --git a/src/modules/cli/lib/cli-events-subscriber.ts b/src/modules/cli/lib/cli-events-subscriber.ts index e83e57a3b3..55a0a8a46a 100644 --- a/src/modules/cli/lib/cli-events-subscriber.ts +++ b/src/modules/cli/lib/cli-events-subscriber.ts @@ -5,17 +5,6 @@ import { sendIpcEventToRenderer } from 'src/ipc-utils'; import { executeCliCommand } from 'src/modules/cli/lib/execute-command'; import { SiteServer } from 'src/site-server'; -const cliSiteEventSchema = z.object( { - action: z.literal( 'keyValuePair' ), - key: z.literal( 'site-event' ), - value: z - .string() - .transform( ( val ) => JSON.parse( val ) ) - .pipe( siteEventSchema ), -} ); - -let subscriber: ReturnType< typeof executeCliCommand > | null = null; - function siteDetailsToServerDetails( site: SiteDetails, running: boolean, @@ -70,6 +59,17 @@ const handleSiteEvent = sequential( async ( event: SiteEvent ): Promise< void > void sendIpcEventToRenderer( 'site-event', event ); } ); +const cliSiteEventSchema = z.object( { + action: z.literal( 'keyValuePair' ), + key: z.literal( 'site-event' ), + value: z + .string() + .transform( ( val ) => JSON.parse( val ) ) + .pipe( siteEventSchema ), +} ); + +let subscriber: ReturnType< typeof executeCliCommand > | null = null; + export async function startCliEventsSubscriber(): Promise< void > { return new Promise( ( resolve, reject ) => { if ( subscriber ) { @@ -111,8 +111,8 @@ export async function startCliEventsSubscriber(): Promise< void > { export function stopCliEventsSubscriber(): void { if ( subscriber ) { - const [ , childProcess ] = subscriber; + const [ eventEmitter, childProcess ] = subscriber; + eventEmitter.removeAllListeners(); childProcess.kill( 'SIGKILL' ); - subscriber = null; } } diff --git a/src/modules/cli/lib/execute-command.ts b/src/modules/cli/lib/execute-command.ts index 49cd5e1634..478cb6123d 100644 --- a/src/modules/cli/lib/execute-command.ts +++ b/src/modules/cli/lib/execute-command.ts @@ -107,7 +107,14 @@ export function executeCliCommand( function appQuitHandler() { const pid = child.pid; const result = child.kill(); - console.log( `Child process with pid ${ pid } killed with result: ${ result }` ); + if ( result ) { + console.log( `Successfully killed child process with pid ${ pid }` ); + } else { + console.error( + `Failed to kill child process with pid ${ pid }. This likely means the process is already terminated. CLI args:`, + args + ); + } } child.on( 'close', ( code ) => {