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

Warn about async calls in .parse() #1940

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,11 @@ Expecting one of '${allowedValues.join("', '")}'`);

parse(argv, parseOptions) {
const userArgs = this._prepareUserArgs(argv, parseOptions);
this._parseCommand([], userArgs);
const result = this._parseCommand([], userArgs);
if (isThenable(result)) {
console.warn(`.parse() is incompatible with async hooks and actions.
Use .parseAsync() instead.`);
}

return this;
}
Expand Down Expand Up @@ -1188,8 +1192,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

_chainOrCall(promise, fn) {
// thenable
if (promise && promise.then && typeof promise.then === 'function') {
if (isThenable(promise)) {
// already have a promise, chain callback
return promise.then(() => fn());
}
Expand Down Expand Up @@ -2193,4 +2196,14 @@ function getCommandAndParents(startCommand) {
return result;
}

/**
* @param {*} value
* @returns {boolean}
* @api private
*/

function isThenable(value) {
return typeof value?.then === 'function';
}

exports.Command = Command;