Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

bin.ts

76 lines3.3 KB

1// @effect-diagnostics nodeBuiltinImport:off -- The service launcher starts before the CLI runtime.
2import * as NodeModule from "node:module";
3import * as NodeSea from "node:sea";
4import * as NodeURL from "node:url";
5​
6import { isEntrypoint } from "./entrypoint.ts";
7​
8// Loads the CLI outside the launcher's module graph. Packaged builds embed it as a raw asset.
9async function loadCliMain(): Promise<typeof import("./binMain.ts")> {
10 if (!NodeSea.isSea()) return import("./binMain.ts");
11​
12 // Keep the executable's module URL so the CLI finds the web client and native packages beside it.
13 const require = NodeModule.createRequire(import.meta.url);
14 // require() resolves links in the executable path, such as a Windows junction.
15 const cliUrl = NodeURL.pathToFileURL(require.resolve(process.execPath)).href;
16 // The CLI's chunks are assets that appear to sit beside the executable. The hooks stay
17 // registered because the CLI imports most of its chunks lazily.
18 const assets = new Map([[cliUrl, "binMain.mjs"]]);
19 for (const key of NodeSea.getAssetKeys()) {
20 if (key !== "binMain.mjs") assets.set(new URL(key, cliUrl).href, key);
21 }
22 NodeModule.registerHooks({
23 resolve(specifier, context, next) {
24 const { parentURL } = context;
25 const url =
26 parentURL !== undefined && assets.has(parentURL) && specifier.startsWith("./")
27 ? new URL(specifier, parentURL).href
28 : undefined;
29 if (url === undefined || !assets.has(url)) return next(specifier, context);
30 return { url, format: "module", shortCircuit: true };
31 },
32 load(url, context, next) {
33 const asset = assets.get(url);
34 if (asset === undefined) return next(url, context);
35 return {
36 format: "module",
37 source: new Uint8Array(NodeSea.getRawAsset(asset)),
38 shortCircuit: true,
39 };
40 },
41 });
42 return require(process.execPath) as typeof import("./binMain.ts");
44​
45function ignoreOutputError(): void {}
46​
47if (
48 isEntrypoint({
49 moduleUrl: import.meta.url,
50 entryPath: process.argv[1],
51 runtimeMain: import.meta.main,
52 })
53) {
54 // Logs go to stdout and stderr, which may be a closed desktop launcher pipe or a log file on
55 // a full disk. Node emits a failed write as an unhandled stream error that crashes the process
56 // and skips shutdown, so drop the line instead.
57 process.stdout.on("error", ignoreOutputError);
58 process.stderr.on("error", ignoreOutputError);
59​
60 // The launcher owns shutdown until its child exits, so it bypasses the Effect CLI runtime.
61 // A service unit passes nothing, or on Windows the launcher's own flags; help and anything
62 // else go through the full CLI.
63 const launcher =
64 process.argv[2] === "__service-launcher" ? await import("./cli/serviceLauncher.ts") : undefined;
65 const launcherOptions = launcher?.parseServiceLauncherArgs(process.argv.slice(3));
66 if (launcher !== undefined && launcherOptions !== undefined) {
67 await launcher.runServiceLauncherCli(launcherOptions);
68 } else if (process.argv[2] === "__process-launcher") {
69 // Single executables host the launcher here; other builds run it with `node -e`.
70 const { run } = await import("@effect/platform-node-shared/NodeChildProcessProxy");
71 run();
72 } else {
73 const { runCliCommand } = await loadCliMain();
74 await runCliCommand();
75 }