Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --version flag #113

Merged
merged 2 commits into from
Dec 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,12 +31,20 @@ const help: commandLineUsage.OptionDefinition = {
type: Boolean
};

const mainDefinitions: commandLineUsage.OptionDefinition[] = [
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'
}
},
version
];

const defaultOptions = [
Expand Down Expand Up @@ -371,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([
Expand Down Expand Up @@ -457,6 +472,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
Expand Down Expand Up @@ -489,6 +510,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();
}
Expand Down