Skip to content

Commit

Permalink
Expose commands via API before they finish running
Browse files Browse the repository at this point in the history
Closes #209
  • Loading branch information
gustavohenke committed Jan 2, 2022
1 parent 75d6a5d commit d8c7ae2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/concurrently.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ concurrently(args._.map((command, index) => ({
successCondition: args.success,
timestampFormat: args['timestamp-format'],
timings: args.timings
}).then(
}).result.then(
() => process.exit(0),
() => process.exit(1)
);
34 changes: 27 additions & 7 deletions src/concurrently.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import spawn from 'spawn-command';
import { Writable } from 'stream';
import treeKill from 'tree-kill';
import { Command, CommandInfo, KillProcess, SpawnCommand } from './command';
import { CloseEvent, Command, CommandInfo, KillProcess, SpawnCommand } from './command';
import { CommandParser } from './command-parser/command-parser';
import { ExpandNpmShortcut } from './command-parser/expand-npm-shortcut';
import { ExpandNpmWildcard } from './command-parser/expand-npm-wildcard';
Expand All @@ -29,6 +29,21 @@ const defaults: ConcurrentlyOptions = {
*/
export type ConcurrentlyCommandInput = string | Partial<CommandInfo>;

export type ConcurrentlyResult = {
/**
* All commands created and ran by concurrently.
*/
commands: Command[],

/**
* A promise that resolves when concurrently ran successfully according to the specified
* success condition, or reject otherwise.
*
* Both the resolved and rejected value is the list of all command's close events.
*/
result: Promise<CloseEvent[]>,
};

export type ConcurrentlyOptions = {
logger?: Logger,

Expand Down Expand Up @@ -85,12 +100,14 @@ export type ConcurrentlyOptions = {

/**
* Core concurrently functionality -- spawns the given commands concurrently and
* returns a promise that will await for them to finish.
* returns the commands themselves + the result according to the specified success condition.
*
* @see CompletionListener
* @returns A promise that resolves when the commands ran successfully, or rejects otherwise.
*/
export function concurrently(baseCommands: ConcurrentlyCommandInput[], baseOptions?: Partial<ConcurrentlyOptions>) {
export function concurrently(
baseCommands: ConcurrentlyCommandInput[],
baseOptions?: Partial<ConcurrentlyOptions>,
): ConcurrentlyResult {
assert.ok(Array.isArray(baseCommands), '[concurrently] commands should be an array');
assert.notStrictEqual(baseCommands.length, 0, '[concurrently] no commands provided');

Expand Down Expand Up @@ -148,13 +165,16 @@ export function concurrently(baseCommands: ConcurrentlyCommandInput[], baseOptio
maybeRunMore(commandsLeft);
}

return new CompletionListener({
successCondition: options.successCondition,
})
const result = new CompletionListener({ successCondition: options.successCondition })
.listen(commands)
.finally(() => {
handleResult.onFinishCallbacks.forEach((onFinish) => onFinish());
});

return {
result,
commands,
};
};

function mapToCommandInfo(command: ConcurrentlyCommandInput): CommandInfo {
Expand Down

0 comments on commit d8c7ae2

Please sign in to comment.