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