1/**
2 * `dist/bin.mjs` of the `t3` npm package: the entry point boot-service
3 * launchers installed before 0.0.41 run with Node to start a new version they
4 * just npm-installed. It forwards everything (arguments, stdio, the IPC
5 * channel the launcher talks over, signals, exit status) to the platform
6 * executable in the sibling `@t3code/t3-<platform>-<arch>` package.
7 *
8 * The first server started this way rewrites the service unit to run the
9 * executable directly, so nothing depends on this file after one update.
10 * Remove it once no supported release predates the executable (after the
11 * first stable release that ships it).
12 */
13export function legacyCliLauncherScript(): string {
14 return `import { spawn } from "node:child_process";
15import { constants } from "node:os";
16import { dirname, join } from "node:path";
17import { createRequire } from "node:module";
18const require = createRequire(import.meta.url);
19const executableName = process.platform === "win32" ? "t3.exe" : "t3";
20const executable = join(dirname(require.resolve("@t3code/t3-" + process.platform + "-" + process.arch + "/package.json")), executableName);
21const ipc = process.send !== undefined;
22const child = spawn(executable, process.argv.slice(2), {
23 stdio: ipc ? ["inherit", "inherit", "inherit", "ipc"] : "inherit",
24});
25const fail = (error) => {
26 if (!error) return;
27 process.stderr.write("t3: " + error.message + "\\n");
28 child.kill("SIGTERM");
29 process.exitCode = 1;
30};
31if (ipc) {
32 process.on("message", (message) => { if (child.connected) child.send(message, fail); });
33 child.on("message", (message) => { if (process.connected) process.send(message, fail); });
34 process.on("disconnect", () => { if (child.connected) child.disconnect(); });
35}
36for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
37 process.on(signal, () => child.kill(signal));
38}
39child.on("error", (error) => { fail(error); process.exit(1); });
40child.on("exit", (code, signal) => process.exit(code ?? 128 + (constants.signals[signal] || 1)));
41`;
42}