import { spawn } from "node:child_process";
import { existsSync, chmodSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const here = dirname(fileURLToPath(import.meta.url));
const TARGETS = {
"darwin-arm64": "darwin-arm64/effectfence",
"linux-x64": "linux-x64/effectfence",
"linux-arm64": "linux-arm64/effectfence",
"win32-x64": "win32-x64/effectfence.exe",
};
const key = `${process.platform}-${process.arch}`;
const rel = TARGETS[key];
if (!rel) {
console.error(
`effectfence: no prebuilt binary for ${key}.\n` +
`Supported: ${Object.keys(TARGETS).join(", ")}.\n` +
`\n` +
`Intel macOS (darwin-x64) is deliberately absent: GitHub retired the\n` +
`Intel runners, and shipping a cross-compiled binary that has never been\n` +
`executed would be worse than shipping none.\n` +
`\n` +
`You can still build it yourself with a Rust toolchain:\n` +
` cargo install effectfence\n`,
);
process.exit(1);
}
const binary = join(here, "..", "vendor", rel);
if (!existsSync(binary)) {
console.error(
`effectfence: the binary for ${key} is missing from this package at\n` +
` ${binary}\n` +
`This means the package was published incorrectly — please open an issue at\n` +
`https://github.com/aurumflux20/effectfence/issues\n`,
);
process.exit(1);
}
if (process.platform !== "win32") {
try {
chmodSync(binary, 0o755);
} catch {
}
}
const child = spawn(binary, process.argv.slice(2), { stdio: "inherit" });
for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) {
process.on(sig, () => {
if (!child.killed) child.kill(sig);
});
}
child.on("error", (err) => {
console.error(`effectfence: failed to start the server: ${err.message}`);
process.exit(1);
});
child.on("exit", (code, signal) => {
if (signal) process.kill(process.pid, signal);
else process.exit(code ?? 0);
});