Skip to content

Commit 2774187

Browse files
testerezgustavohenke
authored andcommittedJan 27, 2020
Return exit codes (#213)
1 parent 84ba2de commit 2774187

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ concurrently can be used programmatically by using the API documented below:
247247
to use when prefixing with `time`. Default: `yyyy-MM-dd HH:mm:ss.ZZZ`
248248

249249
> Returns: a `Promise` that resolves if the run was successful (according to `successCondition` option),
250-
> or rejects otherwise.
250+
> or rejects, containing an array with the exit codes of each command that has been run.
251251
252252
Example:
253253

‎src/completion-listener.js

+26-22
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,33 @@ module.exports = class CompletionListener {
77
this.scheduler = scheduler;
88
}
99

10-
listen(commands) {
11-
const closeStreams = commands.map(command => command.close);
12-
const allClosed = Rx.zip(...closeStreams);
13-
return Rx.merge(...closeStreams).pipe(
14-
bufferCount(closeStreams.length),
15-
map(exitCodes => {
16-
switch (this.successCondition) {
17-
/* eslint-disable indent */
18-
case 'first':
19-
return exitCodes[0] === 0;
10+
isSuccess(exitCodes) {
11+
switch (this.successCondition) {
12+
/* eslint-disable indent */
13+
case 'first':
14+
return exitCodes[0] === 0;
15+
16+
case 'last':
17+
return exitCodes[exitCodes.length - 1] === 0;
2018

21-
case 'last':
22-
return exitCodes[exitCodes.length - 1] === 0;
19+
default:
20+
return exitCodes.every(exitCode => exitCode === 0);
21+
/* eslint-enable indent */
22+
}
23+
}
2324

24-
default:
25-
return exitCodes.every(exitCode => exitCode === 0);
26-
/* eslint-enable indent */
27-
}
28-
}),
29-
switchMap(success => success
30-
? Rx.of(null, this.scheduler)
31-
: Rx.throwError(new Error(), this.scheduler)),
32-
take(1)
33-
).toPromise();
25+
listen(commands) {
26+
const closeStreams = commands.map(command => command.close);
27+
return Rx.merge(...closeStreams)
28+
.pipe(
29+
bufferCount(closeStreams.length),
30+
switchMap(exitCodes =>
31+
this.isSuccess(exitCodes)
32+
? Rx.of(exitCodes, this.scheduler)
33+
: Rx.throwError(exitCodes, this.scheduler)
34+
),
35+
take(1)
36+
)
37+
.toPromise();
3438
}
3539
};

‎src/completion-listener.spec.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ beforeEach(() => {
99
scheduler = new TestScheduler();
1010
});
1111

12-
const createController = successCondition => new CompletionListener({
13-
successCondition,
14-
scheduler
15-
});
12+
const createController = successCondition =>
13+
new CompletionListener({
14+
successCondition,
15+
scheduler
16+
});
1617

1718
describe('with default success condition set', () => {
1819
it('succeeds if all processes exited with code 0', () => {
@@ -23,7 +24,7 @@ describe('with default success condition set', () => {
2324

2425
scheduler.flush();
2526

26-
return expect(result).resolves.toBeNull();
27+
return expect(result).resolves.toEqual([0, 0]);
2728
});
2829

2930
it('fails if one of the processes exited with non-0 code', () => {
@@ -34,11 +35,10 @@ describe('with default success condition set', () => {
3435

3536
scheduler.flush();
3637

37-
expect(result).rejects.toThrowError();
38+
expect(result).rejects.toEqual([0, 1]);
3839
});
3940
});
4041

41-
4242
describe('with success condition set to first', () => {
4343
it('succeeds if first process to exit has code 0', () => {
4444
const result = createController('first').listen(commands);
@@ -48,7 +48,7 @@ describe('with success condition set to first', () => {
4848

4949
scheduler.flush();
5050

51-
return expect(result).resolves.toBeNull();
51+
return expect(result).resolves.toEqual([0, 1]);
5252
});
5353

5454
it('fails if first process to exit has non-0 code', () => {
@@ -59,7 +59,7 @@ describe('with success condition set to first', () => {
5959

6060
scheduler.flush();
6161

62-
return expect(result).rejects.toThrowError();
62+
return expect(result).rejects.toEqual([1, 0]);
6363
});
6464
});
6565

@@ -72,7 +72,7 @@ describe('with success condition set to last', () => {
7272

7373
scheduler.flush();
7474

75-
return expect(result).resolves.toBeNull();
75+
return expect(result).resolves.toEqual([1, 0]);
7676
});
7777

7878
it('fails if last process to exit has non-0 code', () => {
@@ -83,6 +83,6 @@ describe('with success condition set to last', () => {
8383

8484
scheduler.flush();
8585

86-
return expect(result).rejects.toThrowError();
86+
return expect(result).rejects.toEqual([0, 1]);
8787
});
8888
});

0 commit comments

Comments
 (0)
Please sign in to comment.