-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
concurrently.js
92 lines (77 loc) · 2.6 KB
/
concurrently.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const assert = require('assert');
const _ = require('lodash');
const spawn = require('spawn-command');
const treeKill = require('tree-kill');
const StripQuotes = require('./command-parser/strip-quotes');
const ExpandNpmShortcut = require('./command-parser/expand-npm-shortcut');
const ExpandNpmWildcard = require('./command-parser/expand-npm-wildcard');
const CompletionListener = require('./completion-listener');
const getSpawnOpts = require('./get-spawn-opts');
const Command = require('./command');
const defaults = {
spawn,
kill: treeKill,
raw: false,
controllers: [],
cwd: undefined,
};
module.exports = (commands, options) => {
assert.ok(Array.isArray(commands), '[concurrently] commands should be an array');
assert.notStrictEqual(commands.length, 0, '[concurrently] no commands provided');
options = _.defaults(options, defaults);
const commandParsers = [
new StripQuotes(),
new ExpandNpmShortcut(),
new ExpandNpmWildcard()
];
commands = _(commands)
.map(mapToCommandInfo)
.flatMap(command => parseCommand(command, commandParsers))
.map((command, index) => new Command(
Object.assign({
index,
spawnOpts: getSpawnOpts({
raw: options.raw,
env: command.env,
cwd: command.cwd || options.cwd,
}),
killProcess: options.kill,
spawn: options.spawn,
}, command)
))
.value();
commands = options.controllers.reduce(
(prevCommands, controller) => controller.handle(prevCommands),
commands
);
const commandsLeft = commands.slice();
const maxProcesses = Math.max(1, Number(options.maxProcesses) || commandsLeft.length);
for (let i = 0; i < maxProcesses; i++) {
maybeRunMore(commandsLeft);
}
return new CompletionListener({ successCondition: options.successCondition }).listen(commands);
};
function mapToCommandInfo(command) {
return {
command: command.command || command,
name: command.name || '',
prefixColor: command.prefixColor || '',
env: command.env || {},
};
}
function parseCommand(command, parsers) {
return parsers.reduce(
(commands, parser) => _.flatMap(commands, command => parser.parse(command)),
_.castArray(command)
);
}
function maybeRunMore(commandsLeft) {
const command = commandsLeft.shift();
if (!command) {
return;
}
command.start();
command.close.subscribe(() => {
maybeRunMore(commandsLeft);
});
}