| 27 | } |
| 28 | |
| 29 | export async function killTree(proc: ChildProcess, opts?: { exited?: () => boolean }): Promise<void> { |
| 30 | const pid = proc.pid |
| 31 | if (!pid || opts?.exited?.()) return |
| 32 | |
| 33 | if (process.platform === "win32") { |
| 34 | await new Promise<void>((resolve) => { |
| 35 | const killer = spawn("taskkill", ["/pid", String(pid), "/f", "/t"], { |
| 36 | stdio: "ignore", |
| 37 | windowsHide: true, |
| 38 | }) |
| 39 | killer.once("exit", () => resolve()) |
| 40 | killer.once("error", () => resolve()) |
| 41 | }) |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | try { |
| 46 | process.kill(-pid, "SIGTERM") |
| 47 | await sleep(SIGKILL_TIMEOUT_MS) |
| 48 | if (!opts?.exited?.()) { |
| 49 | process.kill(-pid, "SIGKILL") |
| 50 | } |
| 51 | } catch (_e) { |
| 52 | proc.kill("SIGTERM") |
| 53 | await sleep(SIGKILL_TIMEOUT_MS) |
| 54 | if (!opts?.exited?.()) { |
| 55 | proc.kill("SIGKILL") |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | function full(file: string) { |
| 61 | if (process.platform !== "win32") return file |