const { spawn } = require("child_process");
const { describeExit } = require("./exit.js");
const FORWARDED = ["SIGINT", "SIGTERM", "SIGHUP"];
function run(bin, args) {
const child = spawn(bin, args, { stdio: "inherit" });
const forwarders = FORWARDED.map((signal) => {
const forward = () => {
try {
child.kill(signal);
} catch {}
};
process.on(signal, forward);
return [signal, forward];
});
child.on("error", (error) => {
console.error(
`sessionwiki: failed to run the prebuilt binary (${error.message}). ` +
"Try `cargo install sessionwiki`."
);
process.exit(1);
});
child.on("exit", (status, signal) => {
for (const [name, forward] of forwarders) process.off(name, forward);
const { code, note } = describeExit({ status, signal });
if (note) console.error(note);
process.exit(code);
});
}
module.exports = { run };