Skip to content

Commit fcbc7db

Browse files
committed
feat(@angular/cli): remove --help-json and add format to help
--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.
1 parent 9648aa3 commit fcbc7db

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

packages/angular/cli/commands/definitions.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
"type": "object",
3131
"properties": {
3232
"help": {
33-
"type": "boolean",
34-
"description": "Shows a help message."
35-
},
36-
"helpJson": {
37-
"type": "boolean",
38-
"description": "Shows the metadata associated with each flags, in JSON format."
33+
"type": ["boolean", "string"],
34+
"description": "Shows a help message. Use '--help=json' as a value to output the help in JSON format.",
35+
"default": false
3936
}
4037
}
4138
},

packages/angular/cli/models/command.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from './interface';
2121

2222
export interface BaseCommandOptions {
23-
help?: boolean;
23+
help?: boolean | string;
2424
helpJson?: boolean;
2525
}
2626

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

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

152-
if (options.help) {
152+
if (options.help === true) {
153153
return this.printHelp(options);
154-
} else if (options.helpJson) {
154+
} else if (options.help === 'json' || options.help === 'JSON') {
155155
return this.printJsonHelp(options);
156+
} else if (options.help !== false && options.help !== undefined) {
157+
// The user entered an invalid type of help, maybe?
158+
this.logger.fatal(
159+
`--help was provided, but format ${JSON.stringify(options.help)} was not understood.`,
160+
);
161+
162+
return 1;
156163
} else {
157164
return await this.run(options);
158165
}

packages/angular/cli/models/parser_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('parseArguments', () => {
2828
{ name: 'e1', aliases: [], type: OptionType.String, enum: ['hello', 'world'], description: '' },
2929
{ name: 'e2', aliases: [], type: OptionType.String, enum: ['hello', ''], description: '' },
3030
{ name: 'e3', aliases: [], type: OptionType.Boolean,
31-
types: [OptionType.String, OptionType.Boolean], enum: ['json', true, false],
31+
types: [OptionType.Boolean, OptionType.String], enum: ['json', true, false],
3232
description: '' },
3333
];
3434

packages/angular/cli/utilities/json-schema.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {
1414
CommandDescription,
1515
CommandScope,
1616
Option,
17-
OptionType, SubCommandDescription,
17+
OptionType,
18+
SubCommandDescription,
19+
Value,
1820
} from '../models/interface';
1921

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

182+
// Only keep enum values we support (booleans, numbers and strings).
183+
const enumValues = (json.isJsonArray(current.enum) && current.enum || []).filter(x => {
184+
switch (typeof x) {
185+
case 'boolean':
186+
case 'number':
187+
case 'string':
188+
return true;
189+
190+
default:
191+
return false;
192+
}
193+
}) as Value[];
194+
180195
let defaultValue: string | number | boolean | undefined = undefined;
181196
if (current.default !== undefined) {
182197
switch (types[0]) {
@@ -218,6 +233,7 @@ export async function parseJsonSchemaToOptions(
218233
description: '' + (current.description === undefined ? '' : current.description),
219234
...types.length == 1 ? { type } : { type, types },
220235
...defaultValue !== undefined ? { default: defaultValue } : {},
236+
...enumValues && enumValues.length > 0 ? { enum: enumValues } : {},
221237
required,
222238
aliases,
223239
...format !== undefined ? { format } : {},

0 commit comments

Comments
 (0)