Open History to see the last commit
1import {
2 readEnvironmentFromLoginShell,
3 resolveLoginShell,
4 ShellEnvironmentReader,
5} from "@t3tools/shared/shell";
6
7export function syncShellEnvironment(
8 env: NodeJS.ProcessEnv = process.env,
9 options: {
10 platform?: NodeJS.Platform;
11 readEnvironment?: ShellEnvironmentReader;
12 } = {},
13): void {
14 const platform = options.platform ?? process.platform;
15 if (platform !== "darwin" && platform !== "linux") return;
16
17 try {
18 const shell = resolveLoginShell(platform, env.SHELL);
19 if (!shell) return;
20
21 const shellEnvironment = (options.readEnvironment ?? readEnvironmentFromLoginShell)(shell, [
22 "PATH",
23 "SSH_AUTH_SOCK",
24 ]);
25
26 if (shellEnvironment.PATH) {
27 env.PATH = shellEnvironment.PATH;
28 }
29
30 if (!env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) {
31 env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK;
32 }
33 } catch {
34 // Keep inherited environment if shell lookup fails.
35 }
36}