index.js
1.15 KB
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
import {spawn} from 'cross-spawn';
import commandConvert from './command';
export default crossEnv;
const envSetterRegex = /(\w+)=('(.+)'|"(.+)"|(.+))/;
function crossEnv(args) {
const [command, commandArgs, env] = getCommandArgsAndEnvVars(args);
if (command) {
const proc = spawn(command, commandArgs, {stdio: 'inherit', env});
process.on('SIGTERM', () => proc.kill('SIGTERM'));
process.on('SIGINT', () => proc.kill('SIGINT'));
process.on('SIGBREAK', () => proc.kill('SIGBREAK'));
process.on('SIGHUP', () => proc.kill('SIGHUP'));
proc.on('exit', process.exit);
return proc;
}
}
function getCommandArgsAndEnvVars(args) { // eslint-disable-line
let command;
const envVars = Object.assign({}, process.env);
const commandArgs = args.map(commandConvert);
while (commandArgs.length) {
const shifted = commandArgs.shift();
const match = envSetterRegex.exec(shifted);
if (match) {
envVars[match[1]] = match[3] || match[4] || match[5];
} else {
command = shifted;
break;
}
if (process.env.APPDATA) {
envVars.APPDATA = process.env.APPDATA;
}
}
return [command, commandArgs, envVars];
}