Skip to content

Commit

Permalink
Warn about async calls in parse()
Browse files Browse the repository at this point in the history
Cherry-picked from tj#1917.
  • Loading branch information
aweebit committed Aug 5, 2023
1 parent d038570 commit 5b06aaa
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { suggestSimilar } = require('./suggestSimilar');

// @ts-check

const PRODUCTION = process.env.NODE_ENV === 'production';

class Command extends EventEmitter {
/**
* Initialize a new `Command`.
Expand Down Expand Up @@ -906,7 +908,11 @@ Expecting one of '${allowedValues.join("', '")}'`);

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

return this;
}
Expand Down Expand Up @@ -1188,8 +1194,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 +2198,14 @@ function getCommandAndParents(startCommand) {
return result;
}

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

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

exports.Command = Command;

0 comments on commit 5b06aaa

Please sign in to comment.