forked from open-cli-tools/concurrently
/
completion-listener.js
43 lines (37 loc) · 1.35 KB
/
completion-listener.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
const Rx = require('rxjs');
const { bufferCount, switchMap, take } = require('rxjs/operators');
module.exports = class CompletionListener {
constructor({ successCondition, scheduler }) {
this.successCondition = successCondition;
this.scheduler = scheduler;
}
isSuccess(exitCodes) {
switch (this.successCondition) {
/* eslint-disable indent */
case 'first':
return exitCodes[0] === 0;
case 'last':
return exitCodes[exitCodes.length - 1] === 0;
case 'all-but-first':
const [, ...allButFirst] = exitCodes;
return allButFirst.every(code => code === 0);
default:
return exitCodes.every(exitCode => exitCode === 0);
/* eslint-enable indent */
}
}
listen(commands) {
const closeStreams = commands.map(command => command.close);
return Rx.merge(...closeStreams)
.pipe(
bufferCount(closeStreams.length),
switchMap(exitInfos =>
this.isSuccess(exitInfos.map(({ exitCode }) => exitCode))
? Rx.of(exitInfos, this.scheduler)
: Rx.throwError(exitInfos, this.scheduler)
),
take(1)
)
.toPromise();
}
};