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

os-jank.ts

101 lines3.1 KB

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