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

nodeRuntime.ts

78 lines3.1 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 {
8 HostProcessEnvironment,
9 HostProcessExecutablePath,
10 HostProcessIsExecutable,
11 HostProcessPlatform,
12} from "./hostProcess.ts";
13import { CommandResolutionCache, resolveCommandPath } from "./shell.ts";
14​
15const NodeRuntimeFeature = Schema.Literals([
16 "Local device support",
17 "Device automation",
18 "Antigravity",
19 "Antigravity sign-in",
20]);
21​
22export const nodeRuntimeUnavailableMessage = (feature: typeof NodeRuntimeFeature.Type): string =>
23 `${feature} requires Node.js. Install Node.js and make sure node is on PATH, then retry.`;
24​
25export class NodeRuntimeUnavailableError extends Schema.TaggedError<NodeRuntimeUnavailableError>()(
26 "NodeRuntimeUnavailableError",
27 { feature: NodeRuntimeFeature, cause: Schema.optional(Schema.Defect()) },
28) {
29 override get message(): string {
30 return nodeRuntimeUnavailableMessage(this.feature);
31 }
33​
34/** A standalone T3 binary runs its embedded CLI, regardless of script arguments. */
35export const resolveNodeExecutable = Effect.fn("nodeRuntime.resolveNodeExecutable")(function* (
36 feature: typeof NodeRuntimeFeature.Type,
37 environment?: NodeJS.ProcessEnv,
38) {
39 const executablePath = yield* HostProcessExecutablePath;
40 if (!(yield* HostProcessIsExecutable)) return executablePath;
41​
42 const fs = yield* FileSystem.FileSystem;
43 const path = yield* Path.Path;
44 const platform = yield* HostProcessPlatform;
45 const env = environment ?? (yield* HostProcessEnvironment);
46 const nodePath = yield* resolveCommandPath(platform === "win32" ? "node.exe" : "node", {
47 // Batch wrappers require a shell; helper callers launch the runtime directly.
48 env: platform === "win32" ? { ...env, PATHEXT: ".EXE" } : env,
49 }).pipe(
50 // Refresh immediately after the user installs Node and retries setup.
51 Effect.provideService(CommandResolutionCache, new Map()),
52 Effect.map((commandPath) => path.resolve(commandPath)),
53 Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })),
54 );
55 // A launcher or symlink named node must not point back at the standalone app.
56 const resolvedPath = yield* fs
57 .realPath(nodePath)
58 .pipe(Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })));
59 if (resolvedPath === executablePath) return yield* new NodeRuntimeUnavailableError({ feature });
60 const [hostInfo, nodeInfo] = yield* Effect.all([
61 fs.stat(executablePath).pipe(Effect.option),
62 fs.stat(nodePath).pipe(Effect.option),
63 ]);
64 if (
65 Option.isSome(hostInfo) &&
66 Option.isSome(nodeInfo) &&
67 hostInfo.value.dev === nodeInfo.value.dev &&
68 Option.isSome(hostInfo.value.ino) &&
69 Option.isSome(nodeInfo.value.ino) &&
70 Number.isSafeInteger(hostInfo.value.ino.value) &&
71 hostInfo.value.ino.value > 0 &&
72 hostInfo.value.ino.value === nodeInfo.value.ino.value
73 ) {
74 return yield* new NodeRuntimeUnavailableError({ feature });
75 }
76 // Launchers such as Vite+ dispatch by argv[0]; keep the node name intact.
77 return nodePath;
78});