Open History to see the last commit
1import {
2 listLoginShellCandidates,
3 mergePathEntries,
4 readPathFromLaunchctl,
5 readEnvironmentFromLoginShell,
6 resolveWindowsEnvironment,
7} from "@t3tools/shared/shell";
8import type {
9 CommandAvailabilityOptions,
10 ShellEnvironmentReader,
11 WindowsShellEnvironmentReader,
12} from "@t3tools/shared/shell";
13
14type WindowsCommandAvailabilityChecker = (
15 command: string,
16 options?: CommandAvailabilityOptions,
17) => boolean;
18
19const LOGIN_SHELL_ENV_NAMES = [
20 "PATH",
21 "SSH_AUTH_SOCK",
22 "HOMEBREW_PREFIX",
23 "HOMEBREW_CELLAR",
24 "HOMEBREW_REPOSITORY",
25 "XDG_CONFIG_HOME",
26 "XDG_DATA_HOME",
27] as const;
28
29function logShellEnvironmentWarning(message: string, error?: unknown): void {
30 console.warn(`[desktop] ${message}`, error instanceof Error ? error.message : (error ?? ""));
31}
32
33export function syncShellEnvironment(
34 env: NodeJS.ProcessEnv = process.env,
35 options: {
36 platform?: NodeJS.Platform;
37 readEnvironment?: ShellEnvironmentReader;
38 readWindowsEnvironment?: WindowsShellEnvironmentReader;
39 isWindowsCommandAvailable?: WindowsCommandAvailabilityChecker;
40 readLaunchctlPath?: typeof readPathFromLaunchctl;
41 userShell?: string;
42 logWarning?: (message: string, error?: unknown) => void;
43 } = {},
44): void {
45 const platform = options.platform ?? process.platform;
46
47 const logWarning = options.logWarning ?? logShellEnvironmentWarning;
48 const readEnvironment = options.readEnvironment ?? readEnvironmentFromLoginShell;
49 const shellEnvironment: Partial<Record<string, string>> = {};
50
51 try {
52 if (platform === "win32") {
53 const repairedEnvironment = resolveWindowsEnvironment(env, {
54 ...(options.readWindowsEnvironment
55 ? { readEnvironment: options.readWindowsEnvironment }
56 : {}),
57 ...(options.isWindowsCommandAvailable
58 ? { commandAvailable: options.isWindowsCommandAvailable }
59 : {}),
60 });
61 for (const [key, value] of Object.entries(repairedEnvironment)) {
62 if (value !== undefined) {
63 env[key] = value;
64 }
65 }
66 return;
67 }
68
69 if (platform !== "darwin" && platform !== "linux") return;
70
71 for (const shell of listLoginShellCandidates(platform, env.SHELL, options.userShell)) {
72 try {
73 Object.assign(shellEnvironment, readEnvironment(shell, LOGIN_SHELL_ENV_NAMES));
74 if (shellEnvironment.PATH) {
75 break;
76 }
77 } catch (error) {
78 logWarning(`Failed to read login shell environment from ${shell}.`, error);
79 }
80 }
81
82 const launchctlPath =
83 platform === "darwin" && !shellEnvironment.PATH
84 ? (options.readLaunchctlPath ?? readPathFromLaunchctl)()
85 : undefined;
86 const mergedPath = mergePathEntries(shellEnvironment.PATH ?? launchctlPath, env.PATH, platform);
87 if (mergedPath) {
88 env.PATH = mergedPath;
89 }
90
91 if (!env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) {
92 env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK;
93 }
94
95 for (const name of [
96 "HOMEBREW_PREFIX",
97 "HOMEBREW_CELLAR",
98 "HOMEBREW_REPOSITORY",
99 "XDG_CONFIG_HOME",
100 "XDG_DATA_HOME",
101 ] as const) {
102 if (!env[name] && shellEnvironment[name]) {
103 env[name] = shellEnvironment[name];
104 }
105 }
106 } catch (error) {
107 logWarning("Failed to synchronize the desktop shell environment.", error);
108 }
109}