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

os-jank.ts

125 lines4.2 KB

1import * as HostProcess from "@t3tools/shared/HostProcess";
2import {
3 listLoginShellCandidates,
4 mergePathEntries,
5 readEnvironmentFromLoginShell,
6 readPathFromLaunchctl,
7 resolveWindowsEnvironment,
8 setWindowsPath,
9} from "@t3tools/shared/shell";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13import * as NodeOS from "node:os";
14​
15function logPathHydrationWarning(message: string, error?: unknown): void {
16 process.stderr.write(
17 `[server] ${message} ${error instanceof Error ? error.message : (error ?? "")}\n`,
18 );
20​
21function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): void {
22 let shellEnvironment: Partial<Record<string, string>> = {};
23 for (const shell of listLoginShellCandidates(platform, env.SHELL)) {
24 try {
25 // mise's record of the PATH entries it added. It must travel with the
26 // shell's PATH, or `mise x` can rebuild PATH behind a same-named wrapper
27 // (`exec mise x claude -- claude`) that then re-runs itself forever.
28 shellEnvironment = readEnvironmentFromLoginShell(shell, ["PATH", "__MISE_DIFF"]);
29 } catch (error) {
30 logPathHydrationWarning(`Failed to read PATH from login shell ${shell}.`, error);
31 }
32​
33 if (shellEnvironment.PATH) break;
34 }
35​
36 const shellPath = shellEnvironment.PATH;
37 const launchctlPath = platform === "darwin" && !shellPath ? readPathFromLaunchctl() : undefined;
38 const mergedPath = mergePathEntries(shellPath ?? launchctlPath, env.PATH, platform);
39 if (mergedPath) {
40 env.PATH = mergedPath;
41 }
42 if (shellPath && shellEnvironment.__MISE_DIFF) {
43 env.__MISE_DIFF = shellEnvironment.__MISE_DIFF;
44 }
46​
47export function hydratePosixHome(
48 env: NodeJS.ProcessEnv,
49 resolveHomeDir = () => NodeOS.userInfo().homedir,
50): void {
51 if ((env.HOME?.trim() ?? "").length > 0) return;
52​
53 const homeDir = resolveHomeDir();
54 if (homeDir.length > 0) {
55 env.HOME = homeDir;
56 }
58​
59export const fixPath = Effect.fn("fixPath")(function* (options?: {
60 readonly shellEnvironmentPrepared?: boolean | undefined;
61}): Effect.fn.Return<void, never, FileSystem.FileSystem | Path.Path> {
62 const platform = yield* HostProcess.Platform;
63 const env = yield* HostProcess.Environment;
64​
65 if (platform === "win32") {
66 // The desktop already ran both PowerShell probes into the env this child
67 // extends. Repeating them here costs seconds of startup on Windows.
68 if (options?.shellEnvironmentPrepared === true) return;
69 const repairedEnvironment = yield* resolveWindowsEnvironment(env).pipe(
70 Effect.catchDefect((defect) =>
71 Effect.sync(() => {
72 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
73 return {} as Partial<NodeJS.ProcessEnv>;
74 }),
75 ),
76 );
77 for (const [key, value] of Object.entries(repairedEnvironment)) {
78 if (value === undefined) continue;
79 if (key === "PATH") {
80 setWindowsPath(env, value);
81 } else {
82 env[key] = value;
83 }
84 }
85 return;
86 }
87​
88 if (platform !== "darwin" && platform !== "linux") return;
89​
90 yield* Effect.sync(() => hydratePosixHome(env)).pipe(
91 Effect.catchDefect((defect) =>
92 Effect.sync(() => {
93 logPathHydrationWarning("Failed to hydrate HOME from the user account.", defect);
94 }),
95 ),
96 );
97 if (options?.shellEnvironmentPrepared === true) return;
98 yield* Effect.sync(() => hydratePosixPath(env, platform)).pipe(
99 Effect.catchDefect((defect) =>
100 Effect.sync(() => {
101 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
102 }),
103 ),
104 );
105});
106​
107export const expandHomePath = Effect.fn(function* (input: string) {
108 const { join } = yield* Path.Path;
109 const home = yield* HostProcess.HomeDirectory;
110 if (input === "~") {
111 return home;
112 }
113 if (input.startsWith("~/") || input.startsWith("~\\")) {
114 return join(home, input.slice(2));
115 }
116 return input;
117});
118​
119export const resolveBaseDir = Effect.fn(function* (raw: string | undefined) {
120 const { join, resolve } = yield* Path.Path;
121 if (!raw || raw.trim().length === 0) {
122 return join(yield* HostProcess.HomeDirectory, ".t3");
123 }
124 return resolve(yield* expandHomePath(raw.trim()));
125});