| 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 | */ |
| 13 | export function legacyCliLauncherScript(): string { |
| 14 | return `import { spawn } from "node:child_process"; |
| 15 | import { constants } from "node:os"; |
| 16 | import { dirname, join } from "node:path"; |
| 17 | import { createRequire } from "node:module"; |
| 18 | const require = createRequire(import.meta.url); |
| 19 | const executableName = process.platform === "win32" ? "t3.exe" : "t3"; |
| 20 | const executable = join(dirname(require.resolve("@t3code/t3-" + process.platform + "-" + process.arch + "/package.json")), executableName); |
| 21 | const ipc = process.send !== undefined; |
| 22 | const child = spawn(executable, process.argv.slice(2), { |
| 23 | stdio: ipc ? ["inherit", "inherit", "inherit", "ipc"] : "inherit", |
| 24 | }); |
| 25 | const fail = (error) => { |
| 26 | if (!error) return; |
| 27 | process.stderr.write("t3: " + error.message + "\\n"); |
| 28 | child.kill("SIGTERM"); |
| 29 | process.exitCode = 1; |
| 30 | }; |
| 31 | if (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 | } |
| 36 | for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) { |
| 37 | process.on(signal, () => child.kill(signal)); |
| 38 | } |
| 39 | child.on("error", (error) => { fail(error); process.exit(1); }); |
| 40 | child.on("exit", (code, signal) => process.exit(code ?? 128 + (constants.signals[signal] || 1))); |
| 41 | `; |
| 42 | } |