-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
60 lines (51 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import process from 'node:process';
import execa from 'execa';
import stripAnsi from 'strip-ansi';
import defaultShell from 'default-shell';
const args = [
'-ilc',
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit',
];
const env = {
// Disables Oh My Zsh auto-update thing that can block the process.
DISABLE_AUTO_UPDATE: 'true',
};
const parseEnv = env => {
env = env.split('_SHELL_ENV_DELIMITER_')[1];
const returnValue = {};
for (const line of stripAnsi(env).split('\n').filter(line => Boolean(line))) {
const [key, ...values] = line.split('=');
returnValue[key] = values.join('=');
}
return returnValue;
};
export async function shellEnv(shell) {
if (process.platform === 'win32') {
return process.env;
}
try {
const {stdout} = await execa(shell || defaultShell, args, {env});
return parseEnv(stdout);
} catch (error) {
if (shell) {
throw error;
} else {
return process.env;
}
}
}
export function shellEnvSync(shell) {
if (process.platform === 'win32') {
return process.env;
}
try {
const {stdout} = execa.sync(shell || defaultShell, args, {env});
return parseEnv(stdout);
} catch (error) {
if (shell) {
throw error;
} else {
return process.env;
}
}
}