diff --git a/lib/command.js b/lib/command.js index 590a271dd..a800e6d0a 100644 --- a/lib/command.js +++ b/lib/command.js @@ -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`. @@ -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; } @@ -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()); } @@ -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;