Open History to see the last commit
1import * as HostProcess 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* (options?: {
52 readonly shellEnvironmentPrepared?: boolean | undefined;
53}): Effect.fn.Return<void, never, FileSystem.FileSystem | Path.Path> {
54 const platform = yield* HostProcess.Platform;
55 const env = yield* HostProcess.Environment;
56
57 if (platform === "win32") {
58 const repairedEnvironment = yield* resolveWindowsEnvironment(env).pipe(
59 Effect.catchDefect((defect) =>
60 Effect.sync(() => {
61 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
62 return {} as Partial<NodeJS.ProcessEnv>;
63 }),
64 ),
65 );
66 for (const [key, value] of Object.entries(repairedEnvironment)) {
67 if (value !== undefined) {
68 env[key] = value;
69 }
70 }
71 return;
72 }
73
74 if (platform !== "darwin" && platform !== "linux") return;
75
76 yield* Effect.sync(() => hydratePosixHome(env)).pipe(
77 Effect.catchDefect((defect) =>
78 Effect.sync(() => {
79 logPathHydrationWarning("Failed to hydrate HOME from the user account.", defect);
80 }),
81 ),
82 );
83 if (options?.shellEnvironmentPrepared === true) return;
84 yield* Effect.sync(() => hydratePosixPath(env, platform)).pipe(
85 Effect.catchDefect((defect) =>
86 Effect.sync(() => {
87 logPathHydrationWarning("Failed to hydrate PATH from the user environment.", defect);
88 }),
89 ),
90 );
91});
92
93export const expandHomePath = Effect.fn(function* (input: string) {
94 const { join } = yield* Path.Path;
95 const home = yield* HostProcess.HomeDirectory;
96 if (input === "~") {
97 return home;
98 }
99 if (input.startsWith("~/") || input.startsWith("~\\")) {
100 return join(home, 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(yield* HostProcess.HomeDirectory, ".t3");
109 }
110 return resolve(yield* expandHomePath(raw.trim()));
111});