import { spawn } from "node:child_process"; export const runProcess = ( command: string, args: string[], options: { cwd: string; timeoutMs: number }, ): Promise<{ code: number | null; stdout: string; stderr: string }> => { return new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: options.cwd, env: process.env, stdio: ["ignore", "pipe", "pipe"], }); let stdout = ""; let stderr = ""; const t = setTimeout(() => { child.kill("SIGTERM"); reject(new Error(`Process timeout after ${options.timeoutMs}ms`)); }, options.timeoutMs); child.stdout?.on("data", (d: Buffer) => { stdout += d.toString("utf-8"); }); child.stderr?.on("data", (d: Buffer) => { stderr += d.toString("utf-8"); }); child.on("close", (code) => { clearTimeout(t); resolve({ code, stdout, stderr }); }); child.on("error", (e) => { clearTimeout(t); reject(e); }); }); };