From f86ff7f4a9a60ea23a3fd6245ed58a1d7cb1d250 Mon Sep 17 00:00:00 2001 From: Constantine Antonakos Date: Thu, 19 Jan 2017 20:41:05 -0500 Subject: [PATCH 1/2] Fix `yarn version` which yields same output as `yarn --version` (#2491) There was a conflict when commander attempts to parse the incoming args between the command executed and the options since the name `version` was shared. In other words, executing the command `yarn version` would yield the same output as `yarn --version`. This relates to PR #2268. --- src/cli/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/index.js b/src/cli/index.js index eb41c60428..ef98355354 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -170,8 +170,13 @@ if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h') process.exit(1); } +if (!command) { + args.unshift(commandName); + command = commands.run; +} +invariant(command, 'missing command'); + // parse flags -args.unshift(commandName); commander.parse(startArgs.concat(args)); commander.args = commander.args.concat(endArgs); From 5984b516405d55a96e0a70515641ac9fec8846ed Mon Sep 17 00:00:00 2001 From: Constantine Antonakos Date: Mon, 30 Jan 2017 12:01:36 -0500 Subject: [PATCH 2/2] Shift first arg that shares name with an option to circumvent conflicting name bug Relating to tj/commander.js#346, when an arg shares the same name as an option, it wrongly ignores the arg command and executes the option instead. Therefore, executing 'yarn version' would instead translate to 'yarn --version'. This logic can subsequently be removed once this issue is resolved. --- src/cli/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cli/index.js b/src/cli/index.js index ef98355354..679fd9c0dc 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -38,6 +38,12 @@ for (let i = 0; i < args.length; i++) { } } +// NOTE: Pending resolution of https://github.com/tj/commander.js/issues/346 +// Remove this (and subsequent use in the logic below) after bug is resolved and issue is closed +const ARGS_THAT_SHARE_NAMES_WITH_OPTIONS = [ + 'version', +]; + // set global options commander.version(pkg.version); commander.usage('[command] [flags]'); @@ -170,13 +176,13 @@ if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h') process.exit(1); } -if (!command) { - args.unshift(commandName); - command = commands.run; +// parse flags +args.unshift(commandName); + +if (ARGS_THAT_SHARE_NAMES_WITH_OPTIONS.indexOf(commandName) >= 0 && args[0] === commandName) { + args.shift(); } -invariant(command, 'missing command'); -// parse flags commander.parse(startArgs.concat(args)); commander.args = commander.args.concat(endArgs);