-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
concurrently.js
116 lines (100 loc) · 3.51 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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,
timings: false
};
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()
];
let lastColor = '';
commands = _(commands)
.map(mapToCommandInfo)
.flatMap(command => parseCommand(command, commandParsers))
.map((command, index) => {
// Use documented behaviour of repeating last color when specifying more commands than colors
lastColor = options.prefixColors && options.prefixColors[index] || lastColor;
return new Command(
Object.assign({
index,
spawnOpts: getSpawnOpts({
raw: options.raw,
env: command.env,
cwd: command.cwd || options.cwd,
}),
prefixColor: lastColor,
killProcess: options.kill,
spawn: options.spawn,
timings: options.timings,
}, command)
);
})
.value();
const handleResult = options.controllers.reduce(
({ commands: prevCommands, onFinishCallbacks }, controller) => {
const { commands, onFinish } = controller.handle(prevCommands);
return {
commands,
onFinishCallbacks: _.concat(onFinishCallbacks, onFinish ? [onFinish] : [])
};
},
{ commands, onFinishCallbacks: [] }
);
commands = handleResult.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)
.finally(() => {
handleResult.onFinishCallbacks.forEach((onFinish) => onFinish());
});
};
function mapToCommandInfo(command) {
return Object.assign({
command: command.command || command,
name: command.name || '',
env: command.env || {},
cwd: command.cwd || '',
}, command.prefixColor ? {
prefixColor: command.prefixColor,
} : {});
}
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);
});
}