41 lines
998 B
JavaScript
41 lines
998 B
JavaScript
|
|
import { spawn } from "node:child_process";
|
||
|
|
|
||
|
|
const executable = process.argv[2];
|
||
|
|
if (!executable) {
|
||
|
|
console.error("usage: node scripts/spawn-test.mjs <exe> [...args]");
|
||
|
|
process.exit(2);
|
||
|
|
}
|
||
|
|
const env = { ...process.env };
|
||
|
|
delete env.ELECTRON_RUN_AS_NODE;
|
||
|
|
|
||
|
|
const child = spawn(executable, process.argv.slice(3), {
|
||
|
|
env,
|
||
|
|
stdio: ["ignore", "pipe", "pipe"],
|
||
|
|
windowsHide: false
|
||
|
|
});
|
||
|
|
|
||
|
|
child.stdout.on("data", (data) => process.stdout.write(`[out] ${data}`));
|
||
|
|
child.stderr.on("data", (data) => process.stderr.write(`[err] ${data}`));
|
||
|
|
|
||
|
|
const timer = setTimeout(() => {
|
||
|
|
console.log(`STILL_RUNNING pid=${child.pid}`);
|
||
|
|
try {
|
||
|
|
child.kill("SIGKILL");
|
||
|
|
} catch {
|
||
|
|
// ignore
|
||
|
|
}
|
||
|
|
setTimeout(() => process.exit(0), 1000);
|
||
|
|
}, 9000);
|
||
|
|
|
||
|
|
child.on("exit", (code, signal) => {
|
||
|
|
clearTimeout(timer);
|
||
|
|
console.log(`EXIT code=${code} signal=${signal}`);
|
||
|
|
process.exit(0);
|
||
|
|
});
|
||
|
|
|
||
|
|
child.on("error", (error) => {
|
||
|
|
clearTimeout(timer);
|
||
|
|
console.error(`SPAWN_ERROR ${error.message}`);
|
||
|
|
process.exit(1);
|
||
|
|
});
|