UNPKG

1.24 kBJavaScriptView Raw
1const utils = require('./utils');
2const merge = utils.merge;
3const bus = utils.bus;
4const spawn = require('child_process').spawn;
5
6module.exports = function spawnCommand(command, config, eventArgs) {
7 var stdio = ['pipe', 'pipe', 'pipe'];
8
9 if (config.options.stdout) {
10 stdio = ['pipe', process.stdout, process.stderr];
11 }
12
13 var sh = 'sh';
14 var shFlag = '-c';
15
16 if (utils.isWindows) {
17 sh = 'cmd';
18 shFlag = '/c';
19 }
20
21
22 if (!Array.isArray(command)) {
23 command = [command];
24 }
25
26 const args = command.join(' ');
27
28 const env = merge(process.env, { FILENAME: eventArgs[0] });
29 const child = spawn(sh, [shFlag, args], {
30 env: merge(config.options.execOptions.env, env),
31 stdio: stdio,
32 });
33
34 if (config.required) {
35 var emit = {
36 stdout: function (data) {
37 bus.emit('stdout', data);
38 },
39 stderr: function (data) {
40 bus.emit('stderr', data);
41 },
42 };
43
44 // now work out what to bind to...
45 if (config.options.stdout) {
46 child.on('stdout', emit.stdout).on('stderr', emit.stderr);
47 } else {
48 child.stdout.on('data', emit.stdout);
49 child.stderr.on('data', emit.stderr);
50
51 bus.stdout = child.stdout;
52 bus.stderr = child.stderr;
53 }
54 }
55};