Open History to see the last commit
1import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
2import {
3 listLoginShellCandidates,
4 mergePathEntries,
5 readPathFromLoginShell,
6 readPathFromLaunchctl,
7 resolveWindowsEnvironment,
8} from "@t3tools/shared/shell";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Path from "effect/Path";
12import * as NodeOS from "node:os";
13
14function logPathHydrationWarning(message: string, error?: unknown): void {
15 process.stderr.write(
16 `[server] ${message} ${error instanceof Error ? error.message : (error ?? "")}\n`,
17 );
18}
19
20function hydratePosixPath(env: NodeJS.ProcessEnv, platform: NodeJS.Platform): void {
21 let shellPath: string | undefined;
22 for (const shell of listLoginShellCandidates(platform, env.SHELL)) {
23 try {
24 shellPath = readPathFromLoginShell(shell);
25 } catch (error) {
26 logPathHydrationWarning(`Failed to read PATH from login shell ${shell}.`, error);
27 }
28
29 if (shellPath) break;
30 }
31
32 const launchctlPath = platform === "darwin" && !shellPath ? readPathFromLaunchctl() : undefined;
33 const mergedPath = mergePathEntries(shellPath ?? launchctlPath, env.PATH, platform);
34 if (mergedPath) {
35 env.PATH = mergedPath;
36 }
37}
38
39export function hydratePosixHome(
40 env: NodeJS.ProcessEnv,
41 resolveHomeDir = () => NodeOS.userInfo().homedir,
42): void {
43 if ((env.HOME?.trim() ?? "").length > 0) return;
44
45 const homeDir = resolveHomeDir();
46 if (homeDir.length > 0) {
47 env.HOME = homeDir;
48 }
49}
50
51export const fixPath = Effect.fn("fixPath")(function* (): Effect.fn.Return<
52 void,
53 never,
54 FileSystem.FileSystem | Path.Path
55> {
56 const platform = yield* HostProcessPlatform;
57 const env = yield* HostProcessEnvironment;
58
59 if (platform === "win32") {
60 const repairedEnvironment = yield* resolveWindowsEnvironment(env).pipe(
61 Effect.catchDefect((defect) =>
62 Effect.sync(() => {
63 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
64 return {} as Partial<NodeJS.ProcessEnv>;
65 }),
66 ),
67 );
68 for (const [key, value] of Object.entries(repairedEnvironment)) {
69 if (value !== undefined) {
70 env[key] = value;
71 }
72 }
73 return;
74 }
75
76 if (platform !== "darwin" && platform !== "linux") return;
77
78 yield* Effect.sync(() => hydratePosixHome(env)).pipe(
79 Effect.catchDefect((defect) =>
80 Effect.sync(() => {
81 logPathHydrationWarning("Failed to hydrate HOME from the user account.", defect);
82 }),
83 ),
84 );
85 yield* Effect.sync(() => hydratePosixPath(env, platform)).pipe(
86 Effect.catchDefect((defect) =>
87 Effect.sync(() => {
88 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
89 }),
90 ),
91 );
92});
93
94export const expandHomePath = Effect.fn(function* (input: string) {
95 const { join } = yield* Path.Path;
96 if (input === "~") {
97 return NodeOS.homedir();
98 }
99 if (input.startsWith("~/") || input.startsWith("~\\")) {
100 return join(NodeOS.homedir(), input.slice(2));
101 }
102 return input;
103});
104
105export const resolveBaseDir = Effect.fn(function* (raw: string | undefined) {
106 const { join, resolve } = yield* Path.Path;
107 if (!raw || raw.trim().length === 0) {
108 return join(NodeOS.homedir(), ".t3");
109 }
110 return resolve(yield* expandHomePath(raw.trim()));
111});