/
concurrently.js
123 lines (106 loc) · 3.38 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
117
118
119
120
121
122
123
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 OutputWriter = require('./output-writer');
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);
}
if (options.logger) {
const outputWriter = new OutputWriter({
outputStream: options.outputStream,
group: options.group,
commands,
});
options.logger.observable.subscribe(({command, text}) => outputWriter.write(command, text));
}
return new CompletionListener({ successCondition: options.successCondition }).listen(commands);
};
/**
* @param {string | CommandInfo} command
* @returns {CommandInfo}
*/
function mapToCommandInfo(command) {
return {
command: command.command || command,
name: command.name || '',
prefixColor: command.prefixColor || '',
env: command.env || {},
cwd: command.cwd || '',
};
}
/**
* @param {CommandInfo} rawCommand
* @param {{ parse: (command: CommandInfo) => CommandInfo[]}[]} parsers
*/
function parseCommand(rawCommand, parsers) {
return parsers.reduce(
(commands, parser) => _.flatMap(commands, command => parser.parse(command)),
_.castArray(rawCommand)
);
}
/**
* @param {Command[]} commandsLeft
*/
function maybeRunMore(commandsLeft) {
const command = commandsLeft.shift();
if (!command) {
return;
}
command.start();
command.close.subscribe(() => {
maybeRunMore(commandsLeft);
});
}
/**
* @typedef {object} CommandInfo
* @property {string} command
* @property {string} name
* @property {string} prefixColor
* @property {object} env
* @property {string} cwd
*/