From d1ca9eeb9333e7e616fb0782b4c40780dd704efc Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Thu, 7 Dec 2023 06:49:21 +0000 Subject: [PATCH] Refactor thread kill --- FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts | 36 +++++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts index 0b08f19..925b498 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/cliUtils.ts @@ -274,24 +274,30 @@ class CLI { const errorLogFull: string[] = []; this.config.jobLog(`Running ${this.config.cli} ${this.config.spawnArgs.join(' ')}`); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types + let thread: any; + + process.on('exit', () => { + if (thread) { + 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); + } + } + }); + const cliExitCode: number = await new Promise((resolve) => { try { const opts = this.config.spawnOpts || {}; 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 = childProcess.spawn(this.config.cli, spawnArgs, opts); thread.stdout.on('data', (data: string) => { errorLogFull.push(data.toString()); @@ -328,6 +334,8 @@ class CLI { } }); + thread = undefined; + if (!this.config.logFullCliOutput) { this.config.jobLog(errorLogFull.slice(-1000).join('')); }