From e98df6c668479ece0af88464517a4904ef171f07 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Mon, 31 Dec 2018 15:10:16 -0800 Subject: [PATCH 1/2] add --version flag --- src/cli/args.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/cli/args.ts b/src/cli/args.ts index a9c4bb12d..7755949ec 100644 --- a/src/cli/args.ts +++ b/src/cli/args.ts @@ -1,3 +1,6 @@ +import * as fs from 'fs'; +import * as path from 'path'; + import chalk from 'chalk'; import commandLineArgs from 'command-line-args'; import commandLineUsage from 'command-line-usage'; @@ -28,11 +31,18 @@ const help: commandLineUsage.OptionDefinition = { type: Boolean }; -const mainDefinitions: commandLineUsage.OptionDefinition[] = [ +const mainDefinitions = [ { name: 'command', type: String, defaultOption: true }, { ...help, description: 'Display the help output. Works on each command as well' + }, + { + name: 'version', + alias: 'V', + type: Boolean, + description: "Display auto-release-cli's version", + group: 'misc' } ]; @@ -457,6 +467,12 @@ function printCommandHelp(command: ICommand) { console.log(commandLineUsage(sections)); } +function printVersion() { + const packagePath = path.join(__dirname, '../../package.json'); + const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8')); + console.log(`v${packageJson.version}`); +} + function styleTypes( command: ICommand, option: commandLineUsage.OptionDefinition @@ -489,6 +505,10 @@ export default function parseArgs(testArgs?: string[]) { const argv = mainOptions._unknown || []; const command = commands.find(c => c.name === mainOptions.command); + if (!command && mainOptions.version) { + return printVersion(); + } + if (!command) { return printRootHelp(); } From 6db3b3facc4dbe85388af487e12dcab7f7e80d6d Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Mon, 31 Dec 2018 15:21:07 -0800 Subject: [PATCH 2/2] fix tests --- src/cli/args.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/cli/args.ts b/src/cli/args.ts index 7755949ec..d130ebcde 100644 --- a/src/cli/args.ts +++ b/src/cli/args.ts @@ -31,19 +31,20 @@ const help: commandLineUsage.OptionDefinition = { type: Boolean }; +const version = { + name: 'version', + alias: 'V', + type: Boolean, + description: "Display auto-release-cli's version" +}; + const mainDefinitions = [ { name: 'command', type: String, defaultOption: true }, { ...help, description: 'Display the help output. Works on each command as well' }, - { - name: 'version', - alias: 'V', - type: Boolean, - description: "Display auto-release-cli's version", - group: 'misc' - } + version ]; const defaultOptions = [ @@ -381,7 +382,11 @@ function filterCommands(allCommands: ICommand[], include: string[]) { } function printRootHelp() { - const options = [...mainDefinitions, ...defaultOptions]; + const options = [ + { ...version, group: 'misc' }, + ...mainDefinitions, + ...defaultOptions + ]; options.forEach(option => styleTypes({} as ICommand, option)); const usage = commandLineUsage([