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 extractPathFromShellOutput(output: string): string | null {
14 const startIndex = output.indexOf(PATH_CAPTURE_START);
15 if (startIndex === -1) return null;
16
17 const valueStartIndex = startIndex + PATH_CAPTURE_START.length;
18 const endIndex = output.indexOf(PATH_CAPTURE_END, valueStartIndex);
19 if (endIndex === -1) return null;
20
21 const pathValue = output.slice(valueStartIndex, endIndex).trim();
22 return pathValue.length > 0 ? pathValue : null;
23}
24
25export function readPathFromLoginShell(
26 shell: string,
27 execFile: ExecFileSyncLike = execFileSync,
28): string | undefined {
29 return readEnvironmentFromLoginShell(shell, ["PATH"], execFile).PATH;
30}
31
32function envCaptureStart(name: string): string {
33 return `__T3CODE_ENV_${name}_START__`;
34}
35
36function envCaptureEnd(name: string): string {
37 return `__T3CODE_ENV_${name}_END__`;
38}
39
40function buildEnvironmentCaptureCommand(names: ReadonlyArray<string>): string {
41 return names
42 .map((name) => {
43 if (!SHELL_ENV_NAME_PATTERN.test(name)) {
44 throw new Error(`Unsupported environment variable name: ${name}`);
45 }
46
47 return [
48 `printf '%s\\n' '${envCaptureStart(name)}'`,
49 `printenv ${name} || true`,
50 `printf '%s\\n' '${envCaptureEnd(name)}'`,
51 ].join("; ");
52 })
53 .join("; ");
54}
55
56function extractEnvironmentValue(output: string, name: string): string | undefined {
57 const startMarker = envCaptureStart(name);
58 const endMarker = envCaptureEnd(name);
59 const startIndex = output.indexOf(startMarker);
60 if (startIndex === -1) return undefined;
61
62 const valueStartIndex = startIndex + startMarker.length;
63 const endIndex = output.indexOf(endMarker, valueStartIndex);
64 if (endIndex === -1) return undefined;
65
66 let value = output.slice(valueStartIndex, endIndex);
67 if (value.startsWith("\n")) {
68 value = value.slice(1);
69 }
70 if (value.endsWith("\n")) {
71 value = value.slice(0, -1);
72 }
73
74 return value.length > 0 ? value : undefined;
75}
76
77export type ShellEnvironmentReader = (
78 shell: string,
79 names: ReadonlyArray<string>,
80 execFile?: ExecFileSyncLike,
81) => Partial<Record<string, string>>;
82
83export const readEnvironmentFromLoginShell: ShellEnvironmentReader = (
84 shell,
85 names,
86 execFile = execFileSync,
87) => {
88 if (names.length === 0) {
89 return {};
90 }
91
92 const output = execFile(shell, ["-ilc", buildEnvironmentCaptureCommand(names)], {
93 encoding: "utf8",
94 timeout: 5000,
95 });
96
97 const environment: Partial<Record<string, string>> = {};
98 for (const name of names) {
99 const value = extractEnvironmentValue(output, name);
100 if (value !== undefined) {
101 environment[name] = value;
102 }
103 }
104
105 return environment;
106};