Skip to content

Commit

Permalink
#3818 Add -v command to show version when specified without other com…
Browse files Browse the repository at this point in the history
…mands (#3913)

* add -v command

* Add -v command

* reorder tests
  • Loading branch information
kaylieEB authored and arcanis committed Jul 12, 2017
1 parent 1de6ccc commit 21566dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down
8 changes: 7 additions & 1 deletion src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
}
Expand Down

0 comments on commit 21566dd

Please sign in to comment.