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