-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
55 lines (47 loc) · 1.05 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
'use strict';
const execa = require('execa');
const stripAnsi = require('strip-ansi');
const defaultShell = require('default-shell');
const args = [
'-ilc',
'echo -n "_SHELL_ENV_DELIMITER_"; env; echo -n "_SHELL_ENV_DELIMITER_"; exit'
];
const parseEnv = env => {
env = env.split('_SHELL_ENV_DELIMITER_')[1];
const ret = {};
for (const line of stripAnsi(env).split('\n').filter(line => Boolean(line))) {
const [key, ...values] = line.split('=');
ret[key] = values.join('=');
}
return ret;
};
module.exports = async shell => {
if (process.platform === 'win32') {
return process.env;
}
try {
const {stdout} = await execa(shell || defaultShell, args);
return parseEnv(stdout);
} catch (error) {
if (shell) {
throw error;
} else {
return process.env;
}
}
};
module.exports.sync = shell => {
if (process.platform === 'win32') {
return process.env;
}
try {
const {stdout} = execa.sync(shell || defaultShell, args);
return parseEnv(stdout);
} catch (error) {
if (shell) {
throw error;
} else {
return process.env;
}
}
};