get-shell.js
980 Bytes
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
const DETECT_CMD_REGEX = /cmd.exe/;
// All sh variant names I found end with "sh":
// https://en.wikipedia.org/wiki/List_of_command-line_interpreters
const DETECT_SH_REGEX = /sh$/;
function detectPlatformShell() {
if (process.platform === 'darwin') {
return process.env.SHELL || '/bin/bash';
}
if (process.platform === 'win32') {
return process.env.SHELL || process.env.COMSPEC || 'cmd.exe';
}
return process.env.SHELL || '/bin/sh';
}
function detectExecuteFlag(shell) {
if (process.env.SHELL_EXECUTE_FLAG) {
return process.env.SHELL_EXECUTE_FLAG;
}
if (shell.match(DETECT_CMD_REGEX)) {
return '/c';
} else if (shell.match(DETECT_SH_REGEX)) {
return '-c';
}
throw new Error('Unable to detect platform shell type. Please set SHELL_EXECUTE_FLAG env variable.');
}
function getShell() {
const shell = detectPlatformShell();
return {
shell: shell,
executeFlag: detectExecuteFlag(shell),
};
}
module.exports = getShell;