diff --git a/__tests__/index.js b/__tests__/index.js index f0f5a7b494..911d9c1756 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -192,6 +192,11 @@ test.concurrent('should run -h command with add option', async () => { expectHelpOutputAsSubcommand(stdout); }); +test.concurrent('should show version of yarn with -v', async () => { + const stdout = await execCommand('-v', [], 'run-version'); + expect(stdout[0]).toEqual(pkg.version); +}); + test.concurrent('should run version command', async () => { await expectAnErrorMessage(execCommand('version', [], 'run-version'), "Can't answer a question unless a user TTY"); }); diff --git a/src/cli/index.js b/src/cli/index.js index 73d8a8cc8a..cec1547f4f 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -33,7 +33,7 @@ const endArgs = doubleDashIndex === -1 ? [] : process.argv.slice(doubleDashIndex // set global options commander.version(version, '--version'); commander.usage('[command] [flags]'); -commander.option('--verbose', 'output verbose messages on internal operations'); +commander.option('-v, --verbose', 'output verbose messages on internal operations'); commander.option('--offline', 'trigger an error if any required dependencies are not available in local cache'); commander.option('--prefer-offline', 'use network only if dependencies are not available in local cache'); commander.option('--strict-semver'); @@ -73,6 +73,12 @@ commander.option('--scripts-prepend-node-path [bool]', 'prepend the node executa // get command name let commandName: string = args.shift() || 'install'; +// if -v is the first command, then always exit after returning the version +if (commandName === '-v') { + console.log(version.trim()); + process.exit(0); +} + if (commandName === '--help' || commandName === '-h') { commandName = 'help'; }