From d51307fe3a7600f7c68d7535221259f43486bf18 Mon Sep 17 00:00:00 2001 From: wk989898 Date: Wed, 23 Oct 2024 13:47:38 +0800 Subject: [PATCH 1/3] feat: support killing previous process --- src/command-runner.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/command-runner.ts b/src/command-runner.ts index 602cd4d..34c7533 100644 --- a/src/command-runner.ts +++ b/src/command-runner.ts @@ -1,5 +1,5 @@ import * as vscode from "vscode"; -import { exec } from "child_process"; +import { exec, ChildProcess } from "child_process"; import OutputChannel from "./output-channel"; import { isCmdShell, getClickableLinksInMsg } from "./utils"; import { @@ -19,8 +19,10 @@ interface IProcessHandlers { class CommandRunner { public isRunProcess: boolean = false; + private cp: ChildProcess | null = null; - public constructor(private outputChannel: OutputChannel) {} + public constructor(private outputChannel: OutputChannel) { + } private resolveProcessSuccess(msg: string): StatusType { this.outputChannel.showMessage(getClickableLinksInMsg(msg)); @@ -37,7 +39,10 @@ class CommandRunner { execOptions: Nullable ): Promise { return new Promise((resolve) => { - exec(cmdVal, execOptions, (_, stdout, stderr) => { + if (this.cp != null) { + this.cp.kill() + } + this.cp = exec(cmdVal, execOptions, (_, stdout, stderr) => { const statusType: StatusType = stderr !== "" ? this.resolveProcessError(String(stderr)) From 03a0d8f484b91cdac107fbb3371bcc2837475513 Mon Sep 17 00:00:00 2001 From: wk989898 Date: Wed, 23 Oct 2024 13:54:28 +0800 Subject: [PATCH 2/3] update --- src/command-runner.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/command-runner.ts b/src/command-runner.ts index 34c7533..74966cf 100644 --- a/src/command-runner.ts +++ b/src/command-runner.ts @@ -38,9 +38,13 @@ class CommandRunner { cmdVal: string, execOptions: Nullable ): Promise { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { if (this.cp != null) { - this.cp.kill() + let killed = this.cp.kill() + if (!killed) { + console.error("previouse is not killed") + reject(StatusType.Error) + } } this.cp = exec(cmdVal, execOptions, (_, stdout, stderr) => { const statusType: StatusType = From f351fc9089f9744f28f9b9214d1b6275bda1388e Mon Sep 17 00:00:00 2001 From: wk989898 Date: Wed, 23 Oct 2024 14:12:17 +0800 Subject: [PATCH 3/3] update --- src/command-runner.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/command-runner.ts b/src/command-runner.ts index 74966cf..cdc6383 100644 --- a/src/command-runner.ts +++ b/src/command-runner.ts @@ -42,15 +42,16 @@ class CommandRunner { if (this.cp != null) { let killed = this.cp.kill() if (!killed) { - console.error("previouse is not killed") - reject(StatusType.Error) + let statusType = this.resolveProcessError("previouse is not killed") + reject(statusType) + return } } this.cp = exec(cmdVal, execOptions, (_, stdout, stderr) => { - const statusType: StatusType = + let statusType: StatusType = stderr !== "" - ? this.resolveProcessError(String(stderr)) - : this.resolveProcessSuccess(String(stdout)); + ? this.resolveProcessError(`stdout:${stdout}\nstderr${stderr}`) + : this.resolveProcessSuccess(`stdout:${stdout}\nstderr${stderr}`); resolve(statusType); });