Open History to see the last commit
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 const hook = NodeModule.registerHooks({
17 load(url, context, next) {
18 if (url !== cliUrl) return next(url, context);
19 return {
20 format: "module",
21 source: new Uint8Array(NodeSea.getRawAsset("binMain.mjs")),
22 shortCircuit: true,
23 };
24 },
25 });
26 try {
27 return require(process.execPath) as typeof import("./binMain.ts");
28 } finally {
29 hook.deregister();
30 }
31}
32
33if (
34 isEntrypoint({
35 moduleUrl: import.meta.url,
36 entryPath: process.argv[1],
37 runtimeMain: import.meta.main,
38 })
39) {
40 // The launcher owns shutdown until its child exits, so it bypasses the Effect CLI runtime.
41 // The service unit passes no arguments; flags and help go through the full CLI.
42 if (process.argv[2] === "__service-launcher" && process.argv.length === 3) {
43 const { runServiceLauncherCli } = await import("./cli/serviceLauncher.ts");
44 await runServiceLauncherCli();
45 } else {
46 const { runCliCommand } = await loadCliMain();
47 await runCliCommand();
48 }
49}