diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts index 9d2e6d5..0b08f19 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts @@ -241,6 +241,30 @@ class CLI { } }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types + killThread = (thread:any): void => { + const killArray = [ + 'SIGKILL', + 'SIGHUP', + 'SIGTERM', + 'SIGINT', + ]; + + try { + thread.kill(); + } catch (err) { + // err + } + + killArray.forEach((com: string) => { + try { + thread.kill(com); + } catch (err) { + // err + } + }); + } + runCli = async (): Promise<{ cliExitCode: number, errorLogFull: string[], @@ -249,7 +273,6 @@ class CLI { const errorLogFull: string[] = []; - // eslint-disable-next-line no-console this.config.jobLog(`Running ${this.config.cli} ${this.config.spawnArgs.join(' ')}`); const cliExitCode: number = await new Promise((resolve) => { try { @@ -257,16 +280,26 @@ class CLI { const spawnArgs = this.config.spawnArgs.map((row) => row.trim()).filter((row) => row !== ''); const thread = childProcess.spawn(this.config.cli, spawnArgs, opts); + process.on('exit', () => { + try { + // eslint-disable-next-line no-console + console.log('Main thread exiting, cleaning up running CLI'); + this.killThread(thread); + } catch (err) { + // eslint-disable-next-line no-console + console.log('Error running cliUtils on Exit function'); + // eslint-disable-next-line no-console + console.log(err); + } + }); + thread.stdout.on('data', (data: string) => { - // eslint-disable-next-line no-console - // console.log(data.toString()); errorLogFull.push(data.toString()); this.parseOutput(data); }); thread.stderr.on('data', (data: string) => { // eslint-disable-next-line no-console - // console.log(data.toString()); errorLogFull.push(data.toString()); this.parseOutput(data); });