Open History to see the last commit
1import { execFileSync } from "node:child_process";
2
3const PATH_CAPTURE_START = "__T3CODE_PATH_START__";
4const PATH_CAPTURE_END = "__T3CODE_PATH_END__";
5const SHELL_ENV_NAME_PATTERN = /^[A-Z0-9_]+$/;
6
7type ExecFileSyncLike = (
8 file: string,
9 args: ReadonlyArray<string>,
10 options: { encoding: "utf8"; timeout: number },
11) => string;
12
13export function resolveLoginShell(
14 platform: NodeJS.Platform,
15 shell: string | undefined,
16): string | undefined {
17 const trimmedShell = shell?.trim();
18 if (trimmedShell) {
19 return trimmedShell;
20 }
21
22 if (platform === "darwin") {
23 return "/bin/zsh";
24 }
25
26 if (platform === "linux") {
27 return "/bin/bash";
28 }
29
30 return undefined;
31}
32
33export function extractPathFromShellOutput(output: string): string | null {
34 const startIndex = output.indexOf(PATH_CAPTURE_START);
35 if (startIndex === -1) return null;
36
37 const valueStartIndex = startIndex + PATH_CAPTURE_START.length;
38 const endIndex = output.indexOf(PATH_CAPTURE_END, valueStartIndex);
39 if (endIndex === -1) return null;
40
41 const pathValue = output.slice(valueStartIndex, endIndex).trim();
42 return pathValue.length > 0 ? pathValue : null;
43}
44
45export function readPathFromLoginShell(
46 shell: string,
47 execFile: ExecFileSyncLike = execFileSync,
48): string | undefined {
49 return readEnvironmentFromLoginShell(shell, ["PATH"], execFile).PATH;
50}
51
52function envCaptureStart(name: string): string {
53 return `__T3CODE_ENV_${name}_START__`;
54}
55
56function envCaptureEnd(name: string): string {
57 return `__T3CODE_ENV_${name}_END__`;
58}
59
60function buildEnvironmentCaptureCommand(names: ReadonlyArray<string>): string {
61 return names
62 .map((name) => {
63 if (!SHELL_ENV_NAME_PATTERN.test(name)) {
64 throw new Error(`Unsupported environment variable name: ${name}`);
65 }
66
67 return [
68 `printf '%s\\n' '${envCaptureStart(name)}'`,
69 `printenv ${name} || true`,
70 `printf '%s\\n' '${envCaptureEnd(name)}'`,
71 ].join("; ");
72 })
73 .join("; ");
74}
75
76function extractEnvironmentValue(output: string, name: string): string | undefined {
77 const startMarker = envCaptureStart(name);
78 const endMarker = envCaptureEnd(name);
79 const startIndex = output.indexOf(startMarker);
80 if (startIndex === -1) return undefined;
81
82 const valueStartIndex = startIndex + startMarker.length;
83 const endIndex = output.indexOf(endMarker, valueStartIndex);
84 if (endIndex === -1) return undefined;
85
86 let value = output.slice(valueStartIndex, endIndex);
87 if (value.startsWith("\n")) {
88 value = value.slice(1);
89 }
90 if (value.endsWith("\n")) {
91 value = value.slice(0, -1);
92 }
93
94 return value.length > 0 ? value : undefined;
95}
96
97export type ShellEnvironmentReader = (
98 shell: string,
99 names: ReadonlyArray<string>,
100 execFile?: ExecFileSyncLike,
101) => Partial<Record<string, string>>;
102
103export const readEnvironmentFromLoginShell: ShellEnvironmentReader = (
104 shell,
105 names,
106 execFile = execFileSync,
107) => {
108 if (names.length === 0) {
109 return {};
110 }
111
112 const output = execFile(shell, ["-ilc", buildEnvironmentCaptureCommand(names)], {
113 encoding: "utf8",
114 timeout: 5000,
115 });
116
117 const environment: Partial<Record<string, string>> = {};
118 for (const name of names) {
119 const value = extractEnvironmentValue(output, name);
120 if (value !== undefined) {
121 environment[name] = value;
122 }
123 }
124
125 return environment;
126};