Skip to content

Commit

Permalink
feat(@angular/cli): remove --help-json and add format to help
Browse files Browse the repository at this point in the history
--help now accepts a value which can be a boolean or a string. If the value
is not understood we simply show a message to the user that it was invalid.
  • Loading branch information
hansl committed Sep 19, 2018
1 parent 9648aa3 commit fcbc7db
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
9 changes: 3 additions & 6 deletions packages/angular/cli/commands/definitions.json
Expand Up @@ -30,12 +30,9 @@
"type": "object",
"properties": {
"help": {
"type": "boolean",
"description": "Shows a help message."
},
"helpJson": {
"type": "boolean",
"description": "Shows the metadata associated with each flags, in JSON format."
"type": ["boolean", "string"],
"description": "Shows a help message. Use '--help=json' as a value to output the help in JSON format.",
"default": false
}
}
},
Expand Down
15 changes: 11 additions & 4 deletions packages/angular/cli/models/command.ts
Expand Up @@ -20,7 +20,7 @@ import {
} from './interface';

export interface BaseCommandOptions {
help?: boolean;
help?: boolean | string;
helpJson?: boolean;
}

Expand Down Expand Up @@ -144,15 +144,22 @@ export abstract class Command<T extends BaseCommandOptions = BaseCommandOptions>
abstract async run(options: T & Arguments): Promise<number | void>;

async validateAndRun(options: T & Arguments): Promise<number | void> {
if (!options.help && !options.helpJson) {
if (!(options.help === true || options.help === 'json' || options.help === 'JSON')) {
await this.validateScope();
}
await this.initialize(options);

if (options.help) {
if (options.help === true) {
return this.printHelp(options);
} else if (options.helpJson) {
} else if (options.help === 'json' || options.help === 'JSON') {
return this.printJsonHelp(options);
} else if (options.help !== false && options.help !== undefined) {
// The user entered an invalid type of help, maybe?
this.logger.fatal(
`--help was provided, but format ${JSON.stringify(options.help)} was not understood.`,
);

return 1;
} else {
return await this.run(options);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/models/parser_spec.ts
Expand Up @@ -28,7 +28,7 @@ describe('parseArguments', () => {
{ name: 'e1', aliases: [], type: OptionType.String, enum: ['hello', 'world'], description: '' },
{ name: 'e2', aliases: [], type: OptionType.String, enum: ['hello', ''], description: '' },
{ name: 'e3', aliases: [], type: OptionType.Boolean,
types: [OptionType.String, OptionType.Boolean], enum: ['json', true, false],
types: [OptionType.Boolean, OptionType.String], enum: ['json', true, false],
description: '' },
];

Expand Down
18 changes: 17 additions & 1 deletion packages/angular/cli/utilities/json-schema.ts
Expand Up @@ -14,7 +14,9 @@ import {
CommandDescription,
CommandScope,
Option,
OptionType, SubCommandDescription,
OptionType,
SubCommandDescription,
Value,
} from '../models/interface';

function _getEnumFromValue<E, T extends string>(v: json.JsonValue, e: E, d: T): T {
Expand Down Expand Up @@ -177,6 +179,19 @@ export async function parseJsonSchemaToOptions(
return;
}

// Only keep enum values we support (booleans, numbers and strings).
const enumValues = (json.isJsonArray(current.enum) && current.enum || []).filter(x => {
switch (typeof x) {
case 'boolean':
case 'number':
case 'string':
return true;

default:
return false;
}
}) as Value[];

let defaultValue: string | number | boolean | undefined = undefined;
if (current.default !== undefined) {
switch (types[0]) {
Expand Down Expand Up @@ -218,6 +233,7 @@ export async function parseJsonSchemaToOptions(
description: '' + (current.description === undefined ? '' : current.description),
...types.length == 1 ? { type } : { type, types },
...defaultValue !== undefined ? { default: defaultValue } : {},
...enumValues && enumValues.length > 0 ? { enum: enumValues } : {},
required,
aliases,
...format !== undefined ? { format } : {},
Expand Down

0 comments on commit fcbc7db

Please sign in to comment.