1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | const childProcess = require("child_process");
|
5 | const tree_kill_1 = require("./vendor/tree-kill");
|
6 | const IS_WINDOWS = /^win/.test(process.platform);
|
7 | function concurrent(commands) {
|
8 | var childrenInfo = {};
|
9 | var children = commands.map(function (cmd, index) {
|
10 |
|
11 | cmd = stripCmdQuotes(cmd);
|
12 | var spawnOpts = { stdio: 'inherit' };
|
13 | if (IS_WINDOWS) {
|
14 | spawnOpts.detached = false;
|
15 | }
|
16 | var child = spawnChild(cmd, spawnOpts);
|
17 | childrenInfo[child.pid] = {
|
18 | command: cmd,
|
19 | index: index,
|
20 | options: spawnOpts,
|
21 | };
|
22 | return child;
|
23 | });
|
24 | ['SIGINT', 'SIGTERM'].forEach(function (signal) {
|
25 | process.on(signal, function () {
|
26 | children.forEach(function (child) {
|
27 | tree_kill_1.killTree(child.pid, signal);
|
28 | });
|
29 | });
|
30 | });
|
31 | }
|
32 | exports.concurrent = concurrent;
|
33 | function stripCmdQuotes(cmd) {
|
34 |
|
35 | if (cmd[0] === '"' || cmd[0] === '\'') {
|
36 | return cmd.substr(1, cmd.length - 2);
|
37 | }
|
38 | else {
|
39 | return cmd;
|
40 | }
|
41 | }
|
42 | function spawnChild(cmd, options) {
|
43 | let child;
|
44 | try {
|
45 | child = spawnChildProcess(cmd, options);
|
46 | }
|
47 | catch (e) {
|
48 | console.error('Error occured when executing command: ' + cmd);
|
49 | console.error(e.stack);
|
50 | return process.exit(1);
|
51 | }
|
52 | return child;
|
53 | }
|
54 | function spawnChildProcess(command, options) {
|
55 | var file, args;
|
56 | if (process.platform === 'win32') {
|
57 | file = 'cmd.exe';
|
58 | args = ['/s', '/c', '"' + command + '"'];
|
59 | options = Object.assign({}, options);
|
60 | options.windowsVerbatimArguments = true;
|
61 | }
|
62 | else {
|
63 | file = '/bin/sh';
|
64 | args = ['-c', command];
|
65 | }
|
66 | return childProcess.spawn(file, args, options);
|
67 | }
|
68 | ;
|