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

nodeRuntime.ts

117 lines4.7 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 HostProcessArguments,
9 HostProcessEnvironment,
10 HostProcessExecutablePath,
11 HostProcessIsExecutable,
12 HostProcessPlatform,
13} from "./hostProcess.ts";
14import { CommandResolutionCache, resolveCommandPath } from "./shell.ts";
15​
16const NodeRuntimeFeature = Schema.Literals([
17 "Local device support",
18 "Device automation",
19 "Antigravity",
20 "Antigravity sign-in",
21]);
22​
23export const nodeRuntimeUnavailableMessage = (feature: typeof NodeRuntimeFeature.Type): string =>
24 `${feature} requires Node.js. Install Node.js and make sure node is on PATH, then retry.`;
25​
26export class NodeRuntimeUnavailableError extends Schema.TaggedError<NodeRuntimeUnavailableError>()(
27 "NodeRuntimeUnavailableError",
28 { feature: NodeRuntimeFeature, cause: Schema.optional(Schema.Defect()) },
29) {
30 override get message(): string {
31 return nodeRuntimeUnavailableMessage(this.feature);
32 }
34​
35export interface SelfInvocation {
36 /** The binary to spawn: Node, Electron (with `ELECTRON_RUN_AS_NODE`), or the packaged T3. */
37 readonly command: string;
38 /**
39 * The absolute entrypoint script to place before the subcommand, or
40 * undefined for the packaged binary, which dispatches subcommands from
41 * argv[2] and treats a leading path as the subcommand itself.
42 */
43 readonly entrypoint: string | undefined;
45​
46/**
47 * How another process runs this T3 install's CLI, for hidden subcommands the
48 * server hands to children such as `acp-mcp-bridge`. `process.execPath` plus
49 * `argv[1]` only works for a script run by Node; the single-executable has no
50 * entrypoint script (Node repeats the binary at argv[1]), so callers must not
51 * assemble the pair themselves.
52 */
53export const resolveSelfInvocation = Effect.fn("nodeRuntime.resolveSelfInvocation")(function* () {
54 const command = yield* HostProcessExecutablePath;
55 if (yield* HostProcessIsExecutable)
56 return { command, entrypoint: undefined } satisfies SelfInvocation;
57 const path = yield* Path.Path;
58 const entry = (yield* HostProcessArguments)[1];
59 // Children spawn from their own working directory, so the script path must be absolute.
60 return {
61 command,
62 entrypoint: entry === undefined ? undefined : path.resolve(entry),
63 } satisfies SelfInvocation;
64});
65​
66/** `[entrypoint?, ...args]`: the argv that runs `args` against this T3 install. */
67export const selfInvocationArgs = (
68 invocation: SelfInvocation,
69 args: ReadonlyArray<string>,
70): ReadonlyArray<string> =>
71 invocation.entrypoint === undefined ? args : [invocation.entrypoint, ...args];
72​
73/** A standalone T3 binary runs its embedded CLI, regardless of script arguments. */
74export const resolveNodeExecutable = Effect.fn("nodeRuntime.resolveNodeExecutable")(function* (
75 feature: typeof NodeRuntimeFeature.Type,
76 environment?: NodeJS.ProcessEnv,
77) {
78 const executablePath = yield* HostProcessExecutablePath;
79 if (!(yield* HostProcessIsExecutable)) return executablePath;
80​
81 const fs = yield* FileSystem.FileSystem;
82 const path = yield* Path.Path;
83 const platform = yield* HostProcessPlatform;
84 const env = environment ?? (yield* HostProcessEnvironment);
85 const nodePath = yield* resolveCommandPath(platform === "win32" ? "node.exe" : "node", {
86 // Batch wrappers require a shell; helper callers launch the runtime directly.
87 env: platform === "win32" ? { ...env, PATHEXT: ".EXE" } : env,
88 }).pipe(
89 // Refresh immediately after the user installs Node and retries setup.
90 Effect.provideService(CommandResolutionCache, new Map()),
91 Effect.map((commandPath) => path.resolve(commandPath)),
92 Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })),
93 );
94 // A launcher or symlink named node must not point back at the standalone app.
95 const resolvedPath = yield* fs
96 .realPath(nodePath)
97 .pipe(Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })));
98 if (resolvedPath === executablePath) return yield* new NodeRuntimeUnavailableError({ feature });
99 const [hostInfo, nodeInfo] = yield* Effect.all([
100 fs.stat(executablePath).pipe(Effect.option),
101 fs.stat(nodePath).pipe(Effect.option),
102 ]);
103 if (
104 Option.isSome(hostInfo) &&
105 Option.isSome(nodeInfo) &&
106 hostInfo.value.dev === nodeInfo.value.dev &&
107 Option.isSome(hostInfo.value.ino) &&
108 Option.isSome(nodeInfo.value.ino) &&
109 Number.isSafeInteger(hostInfo.value.ino.value) &&
110 hostInfo.value.ino.value > 0 &&
111 hostInfo.value.ino.value === nodeInfo.value.ino.value
112 ) {
113 return yield* new NodeRuntimeUnavailableError({ feature });
114 }
115 // Launchers such as Vite+ dispatch by argv[0]; keep the node name intact.
116 return nodePath;
117});