refactor: extract process management utilities for cross-platform support

Consolidate process lifecycle management (kill, check alive) into a new
utility module with proper Windows/Unix handling, replacing scattered
process.kill() calls with consistent async APIs.
This commit is contained in:
weishu
2025-12-26 16:12:03 +08:00
parent eee7a249fa
commit 702072e9ba
13 changed files with 152 additions and 80 deletions
+71
View File
@@ -0,0 +1,71 @@
import type { ChildProcess } from 'node:child_process';
import spawn from 'cross-spawn';
export const isWindows = (): boolean => process.platform === 'win32';
export function isProcessAlive(pid: number): boolean {
if (!Number.isFinite(pid) || pid <= 0) {
return false;
}
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
function killProcessWindows(pid: number, force: boolean): boolean {
const args = ['/T', '/PID', pid.toString()];
if (force) {
args.unshift('/F');
}
try {
const result = spawn.sync('taskkill', args, { stdio: 'pipe' });
if (result.error) {
return false;
}
return result.status === 0;
} catch {
return false;
}
}
export async function killProcess(pid: number, force: boolean = false): Promise<boolean> {
if (!Number.isFinite(pid) || pid <= 0) {
return false;
}
if (isWindows()) {
return killProcessWindows(pid, force);
}
try {
process.kill(pid, force ? 'SIGKILL' : 'SIGTERM');
return true;
} catch {
return false;
}
}
export async function killProcessByChildProcess(
child: ChildProcess,
force: boolean = false
): Promise<boolean> {
const pid = child.pid;
if (!pid) {
return false;
}
if (isWindows()) {
return killProcess(pid, force);
}
try {
child.kill(force ? 'SIGKILL' : 'SIGTERM');
return true;
} catch {
return false;
}
}