Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parseAsync #1118

Merged
merged 4 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function Command(name) {
this._optionValues = {};
this._storeOptionsAsProperties = true; // backwards compatible by default
this._passCommandToAction = true; // backwards compatible by default
this._actionResults = [];

this._helpFlags = '-h, --help';
this._helpDescription = 'output usage information';
Expand Down Expand Up @@ -366,7 +367,13 @@ Command.prototype.action = function(fn) {
actionArgs.push(args.slice(expectedArgsCount));
}

fn.apply(self, actionArgs);
const actionResult = fn.apply(self, actionArgs);
// Remember result in case it is async. Assume parseAsync getting called on root.
let rootCommand = self;
while (rootCommand.parent) {
rootCommand = rootCommand.parent;
}
rootCommand._actionResults.push(actionResult);
};
var parent = this.parent || this;
var name = parent === this ? '*' : this._name;
Expand Down Expand Up @@ -604,7 +611,7 @@ Command.prototype._getOptionValue = function(key) {
};

/**
* Parse `argv`, settings options and invoking commands when defined.
* Parse `argv`, setting options and invoking commands when defined.
*
* @param {Array} argv
* @return {Command} for chaining
Expand Down Expand Up @@ -688,6 +695,20 @@ Command.prototype.parse = function(argv) {
return result;
};

/**
* Parse `argv`, setting options and invoking commands when defined.
*
* Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
*
* @param {Array} argv
* @return {Promise} Promise
* @api public
*/
Command.prototype.parseAsync = function(argv) {
this.parse(argv);
return Promise.all(this._actionResults);
};

/**
* Execute a sub-command executable.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/command.action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@ test('when .action on program with subcommand and program argument then program

expect(actionMock).toHaveBeenCalledWith('a', program);
});

test('when action is async then can await parseAsync', async() => {
let asyncFinished = false;
async function delay() {
await new Promise(resolve => setTimeout(resolve, 100));
asyncFinished = true;
};
const program = new commander.Command();
program
.action(delay);

const later = program.parseAsync(['node', 'test']);
expect(asyncFinished).toBe(false);
await later;
expect(asyncFinished).toBe(true);
});
6 changes: 6 additions & 0 deletions typings/commander-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,10 @@ program.exitOverride((err):void => {

program.parse(process.argv);

program.parseAsync(process.argv).then(() => {
console.log('parseAsync success');
}).catch(err => {
console.log('parseAsync failed');
});

console.log('stuff');
13 changes: 11 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,21 @@ declare namespace commander {
allowUnknownOption(arg?: boolean): Command;

/**
* Parse `argv`, settings options and invoking commands when defined.
* Parse `argv`, setting options and invoking commands when defined.
*
* @returns {Command} for chaining
* @returns Command for chaining
*/
parse(argv: string[]): Command;

/**
* Parse `argv`, setting options and invoking commands when defined.
*
* Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
*
* @returns Promise
*/
parseAsync(argv: string[]): Promise<any>;

/**
* Parse options from `argv` returning `argv` void of these options.
*/
Expand Down