Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as FileSystem from "effect/FileSystem";
3import * as Path from "effect/Path";
4import * as Option from "effect/Option";
5import * as Schema from "effect/Schema";
6
7import * as HostProcess from "./HostProcess.ts";
8import { CommandResolutionCache, resolveCommandPath } from "./shell.ts";
9
10const NodeRuntimeFeature = Schema.Literals([
11 "Local device support",
12 "Device automation",
13 "Antigravity",
14 "Antigravity sign-in",
15]);
16
17export const nodeRuntimeUnavailableMessage = (feature: typeof NodeRuntimeFeature.Type): string =>
18 `${feature} requires Node.js. Install Node.js and make sure node is on PATH, then retry.`;
19
20export class NodeRuntimeUnavailableError extends Schema.TaggedError<NodeRuntimeUnavailableError>()(
21 "NodeRuntimeUnavailableError",
22 { feature: NodeRuntimeFeature, cause: Schema.optional(Schema.Defect()) },
23) {
24 override get message(): string {
25 return nodeRuntimeUnavailableMessage(this.feature);
26 }
27}
28
29export interface SelfInvocation {
30 readonly command: string;
31 /**
32 * The absolute entrypoint script to place before the subcommand, or
33 * undefined for the packaged binary, which dispatches subcommands from
34 * argv[2] and treats a leading path as the subcommand itself.
35 */
36 readonly entrypoint: string | undefined;
37}
38
39/**
40 * `process.execPath` plus `argv[1]` only works for a script run by Node; the
41 * single-executable has no entrypoint script (Node repeats the binary at
42 * argv[1]), so callers must not assemble the pair themselves.
43 */
44export const resolveSelfInvocation = Effect.fn("nodeRuntime.resolveSelfInvocation")(function* () {
45 const command = yield* HostProcess.ExecutablePath;
46 if (yield* HostProcess.IsExecutable)
47 return { command, entrypoint: undefined } satisfies SelfInvocation;
48 const path = yield* Path.Path;
49 const entry = (yield* HostProcess.Arguments)[1];
50 // Children spawn from their own working directory, so the script path must be absolute.
51 return {
52 command,
53 entrypoint: entry === undefined ? undefined : path.resolve(entry),
54 } satisfies SelfInvocation;
55});
56
57export const selfInvocationArgs = (
58 invocation: SelfInvocation,
59 args: ReadonlyArray<string>,
60): ReadonlyArray<string> =>
61 invocation.entrypoint === undefined ? args : [invocation.entrypoint, ...args];
62
63export const resolveNodeExecutable = Effect.fn("nodeRuntime.resolveNodeExecutable")(function* (
64 feature: typeof NodeRuntimeFeature.Type,
65 environment?: NodeJS.ProcessEnv,
66) {
67 const executablePath = yield* HostProcess.ExecutablePath;
68 if (!(yield* HostProcess.IsExecutable)) return executablePath;
69
70 const fs = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 const platform = yield* HostProcess.Platform;
73 const env = environment ?? (yield* HostProcess.Environment);
74 const resolvedNodePath = yield* resolveCommandPath(platform === "win32" ? "node.exe" : "node", {
75 env: platform === "win32" ? { ...env, PATHEXT: ".EXE" } : env,
76 }).pipe(Effect.provideService(CommandResolutionCache, new Map()));
77 if (resolvedNodePath === null) return yield* new NodeRuntimeUnavailableError({ feature });
78 const nodePath = path.resolve(resolvedNodePath);
79 const resolvedPath = yield* fs
80 .realPath(nodePath)
81 .pipe(Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })));
82 if (resolvedPath === executablePath) return yield* new NodeRuntimeUnavailableError({ feature });
83 const [hostInfo, nodeInfo] = yield* Effect.all([
84 fs.stat(executablePath).pipe(Effect.option),
85 fs.stat(nodePath).pipe(Effect.option),
86 ]);
87 if (
88 Option.isSome(hostInfo) &&
89 Option.isSome(nodeInfo) &&
90 hostInfo.value.dev === nodeInfo.value.dev &&
91 Option.isSome(hostInfo.value.ino) &&
92 Option.isSome(nodeInfo.value.ino) &&
93 hostInfo.value.ino.value > 0n &&
94 hostInfo.value.ino.value === nodeInfo.value.ino.value
95 ) {
96 return yield* new NodeRuntimeUnavailableError({ feature });
97 }
98 // Launchers such as Vite+ dispatch by argv[0]; keep the node name intact.
99 return nodePath;
100});