Skip to content

Commit

Permalink
Return exit codes (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
testerez authored and gustavohenke committed Jan 27, 2020
1 parent 84ba2de commit 2774187
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -247,7 +247,7 @@ concurrently can be used programmatically by using the API documented below:
to use when prefixing with `time`. Default: `yyyy-MM-dd HH:mm:ss.ZZZ`

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

Expand Down
48 changes: 26 additions & 22 deletions src/completion-listener.js
Expand Up @@ -7,29 +7,33 @@ module.exports = class CompletionListener {
this.scheduler = scheduler;
}

listen(commands) {
const closeStreams = commands.map(command => command.close);
const allClosed = Rx.zip(...closeStreams);
return Rx.merge(...closeStreams).pipe(
bufferCount(closeStreams.length),
map(exitCodes => {
switch (this.successCondition) {
/* eslint-disable indent */
case 'first':
return exitCodes[0] === 0;
isSuccess(exitCodes) {
switch (this.successCondition) {
/* eslint-disable indent */
case 'first':
return exitCodes[0] === 0;

case 'last':
return exitCodes[exitCodes.length - 1] === 0;

case 'last':
return exitCodes[exitCodes.length - 1] === 0;
default:
return exitCodes.every(exitCode => exitCode === 0);
/* eslint-enable indent */
}
}

default:
return exitCodes.every(exitCode => exitCode === 0);
/* eslint-enable indent */
}
}),
switchMap(success => success
? Rx.of(null, this.scheduler)
: Rx.throwError(new Error(), this.scheduler)),
take(1)
).toPromise();
listen(commands) {
const closeStreams = commands.map(command => command.close);
return Rx.merge(...closeStreams)
.pipe(
bufferCount(closeStreams.length),
switchMap(exitCodes =>
this.isSuccess(exitCodes)
? Rx.of(exitCodes, this.scheduler)
: Rx.throwError(exitCodes, this.scheduler)
),
take(1)
)
.toPromise();
}
};
22 changes: 11 additions & 11 deletions src/completion-listener.spec.js
Expand Up @@ -9,10 +9,11 @@ beforeEach(() => {
scheduler = new TestScheduler();
});

const createController = successCondition => new CompletionListener({
successCondition,
scheduler
});
const createController = successCondition =>
new CompletionListener({
successCondition,
scheduler
});

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

scheduler.flush();

return expect(result).resolves.toBeNull();
return expect(result).resolves.toEqual([0, 0]);
});

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

scheduler.flush();

expect(result).rejects.toThrowError();
expect(result).rejects.toEqual([0, 1]);
});
});


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

scheduler.flush();

return expect(result).resolves.toBeNull();
return expect(result).resolves.toEqual([0, 1]);
});

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

scheduler.flush();

return expect(result).rejects.toThrowError();
return expect(result).rejects.toEqual([1, 0]);
});
});

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

scheduler.flush();

return expect(result).resolves.toBeNull();
return expect(result).resolves.toEqual([1, 0]);
});

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

scheduler.flush();

return expect(result).rejects.toThrowError();
return expect(result).rejects.toEqual([0, 1]);
});
});

0 comments on commit 2774187

Please sign in to comment.