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

nodeRuntime.ts

93 lines3.8 KB

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 "Browser tab access",
16]);
17​
18export const nodeRuntimeUnavailableMessage = (feature: typeof NodeRuntimeFeature.Type): string =>
19 `${feature} requires Node.js. Install Node.js and make sure node is on PATH, then retry.`;
20​
21export class NodeRuntimeUnavailableError extends Schema.TaggedError<NodeRuntimeUnavailableError>()(
22 "NodeRuntimeUnavailableError",
23 { feature: NodeRuntimeFeature, cause: Schema.optional(Schema.Defect()) },
24) {
25 override get message(): string {
26 return nodeRuntimeUnavailableMessage(this.feature);
27 }
29​
30export interface SelfInvocation {
31 readonly command: string;
32 /** Absolute script entrypoint; undefined for packaged binaries, which dispatch subcommands from argv[2]. */
33 readonly entrypoint: string | undefined;
35​
36/** Packaged binaries repeat the binary at argv[1]; callers must not assemble execPath/argv[1] themselves. */
37export const resolveSelfInvocation = Effect.fn("nodeRuntime.resolveSelfInvocation")(function* () {
38 const command = yield* HostProcess.ExecutablePath;
39 if (yield* HostProcess.IsExecutable)
40 return { command, entrypoint: undefined } satisfies SelfInvocation;
41 const path = yield* Path.Path;
42 const entry = (yield* HostProcess.Arguments)[1];
43 // Children spawn from their own working directory, so the script path must be absolute.
44 return {
45 command,
46 entrypoint: entry === undefined ? undefined : path.resolve(entry),
47 } satisfies SelfInvocation;
48});
49​
50export const selfInvocationArgs = (
51 invocation: SelfInvocation,
52 args: ReadonlyArray<string>,
53): ReadonlyArray<string> =>
54 invocation.entrypoint === undefined ? args : [invocation.entrypoint, ...args];
55​
56export const resolveNodeExecutable = Effect.fn("nodeRuntime.resolveNodeExecutable")(function* (
57 feature: typeof NodeRuntimeFeature.Type,
58 environment?: NodeJS.ProcessEnv,
59) {
60 const executablePath = yield* HostProcess.ExecutablePath;
61 if (!(yield* HostProcess.IsExecutable)) return executablePath;
62​
63 const fs = yield* FileSystem.FileSystem;
64 const path = yield* Path.Path;
65 const platform = yield* HostProcess.Platform;
66 const env = environment ?? (yield* HostProcess.Environment);
67 const resolvedNodePath = yield* resolveCommandPath(platform === "win32" ? "node.exe" : "node", {
68 env: platform === "win32" ? { ...env, PATHEXT: ".EXE" } : env,
69 }).pipe(Effect.provideService(CommandResolutionCache, new Map()));
70 if (resolvedNodePath === null) return yield* new NodeRuntimeUnavailableError({ feature });
71 const nodePath = path.resolve(resolvedNodePath);
72 const resolvedPath = yield* fs
73 .realPath(nodePath)
74 .pipe(Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })));
75 if (resolvedPath === executablePath) return yield* new NodeRuntimeUnavailableError({ feature });
76 const [hostInfo, nodeInfo] = yield* Effect.all([
77 fs.stat(executablePath).pipe(Effect.option),
78 fs.stat(nodePath).pipe(Effect.option),
79 ]);
80 if (
81 Option.isSome(hostInfo) &&
82 Option.isSome(nodeInfo) &&
83 hostInfo.value.dev === nodeInfo.value.dev &&
84 Option.isSome(hostInfo.value.ino) &&
85 Option.isSome(nodeInfo.value.ino) &&
86 hostInfo.value.ino.value > 0n &&
87 hostInfo.value.ino.value === nodeInfo.value.ino.value
88 ) {
89 return yield* new NodeRuntimeUnavailableError({ feature });
90 }
91 // Launchers such as Vite+ dispatch by argv[0]; keep the node name intact.
92 return nodePath;
93});