Skip to content

Commit f3e8418

Browse files
Broccohansl
authored andcommitted
fix(@angular/cli): Show detailed help for generate
Fixes #7723
1 parent 4f4ff9f commit f3e8418

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

packages/@angular/cli/commands/generate.ts

+50-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { SchematicAvailableOptions } from '../tasks/schematic-get-options';
1717
const Command = require('../ember-cli/lib/models/command');
1818
const SilentError = require('silent-error');
1919

20-
const { cyan, yellow } = chalk;
20+
const { cyan, grey, yellow } = chalk;
2121
const separatorRegEx = /[\/\\]/g;
2222

2323

@@ -186,15 +186,57 @@ export default Command.extend({
186186
});
187187
},
188188

189-
printDetailedHelp: function () {
189+
printDetailedHelp: function (_options: any, rawArgs: any): string | Promise<string> {
190190
const engineHost = getEngineHost();
191191
const collectionName = this.getCollectionName();
192192
const collection = getCollection(collectionName);
193-
const schematicNames: string[] = engineHost.listSchematics(collection);
194-
this.ui.writeLine(cyan('Available schematics:'));
195-
schematicNames.forEach(schematicName => {
196-
this.ui.writeLine(yellow(` ${schematicName}`));
197-
});
198-
this.ui.writeLine('');
193+
const schematicName = rawArgs[1];
194+
if (schematicName) {
195+
const SchematicGetOptionsTask = require('../tasks/schematic-get-options').default;
196+
const getOptionsTask = new SchematicGetOptionsTask({
197+
ui: this.ui,
198+
project: this.project
199+
});
200+
return getOptionsTask.run({
201+
schematicName,
202+
collectionName
203+
})
204+
.then((availableOptions: SchematicAvailableOptions[]) => {
205+
const output: string[] = [];
206+
output.push(cyan(`ng generate ${schematicName} ${cyan('[name]')} ${cyan('<options...>')}`));
207+
availableOptions
208+
.filter(opt => opt.name !== 'name')
209+
.forEach(opt => {
210+
let text = cyan(` --${opt.name}`);
211+
if (opt.schematicType) {
212+
text += cyan(` (${opt.schematicType})`);
213+
}
214+
if (opt.schematicDefault) {
215+
text += cyan(` (Default: ${opt.schematicDefault})`);
216+
}
217+
if (opt.description) {
218+
text += ` ${opt.description}`;
219+
}
220+
output.push(text);
221+
if (opt.aliases && opt.aliases.length > 0) {
222+
const aliasText = opt.aliases.reduce(
223+
(acc, curr) => {
224+
return acc + ` -${curr}`;
225+
},
226+
'');
227+
output.push(grey(` aliases: ${aliasText}`));
228+
}
229+
});
230+
return output.join('\n');
231+
});
232+
} else {
233+
const schematicNames: string[] = engineHost.listSchematics(collection);
234+
const output: string[] = [];
235+
output.push(cyan('Available schematics:'));
236+
schematicNames.forEach(schematicName => {
237+
output.push(yellow(` ${schematicName}`));
238+
});
239+
return Promise.resolve(output.join('\n'));
240+
}
199241
}
200242
});

packages/@angular/cli/commands/help.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ const HelpCommand = Command.extend({
6666
if (cmd === commandInput) {
6767
if (commandOptions.short) {
6868
this.ui.writeLine(command.printShortHelp(commandOptions));
69-
} else if (command.printDetailedHelp(commandOptions)) {
70-
this.ui.writeLine(command.printDetailedHelp(commandOptions));
69+
} else if (command.printDetailedHelp(commandOptions, rawArgs)) {
70+
const result = command.printDetailedHelp(commandOptions, rawArgs);
71+
if (result instanceof Promise) {
72+
result.then(r => this.ui.writeLine(r));
73+
} else {
74+
this.ui.writeLine(result);
75+
}
7176
} else {
7277
this.ui.writeLine(command.printBasicHelp(commandOptions));
7378
}

packages/@angular/cli/tasks/schematic-get-options.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface SchematicAvailableOptions {
1313
description: string;
1414
aliases: string[];
1515
type: any;
16+
schematicType: any;
17+
schematicDefault: any;
1618
}
1719

1820
export default Task.extend({
@@ -30,6 +32,7 @@ export default Task.extend({
3032
.map(key => ({...properties[key], ...{name: stringUtils.dasherize(key)}}))
3133
.map(opt => {
3234
let type;
35+
const schematicType = opt.type;
3336
switch (opt.type) {
3437
case 'string':
3538
type = String;
@@ -54,11 +57,15 @@ export default Task.extend({
5457
aliases = [...aliases, ...opt.aliases];
5558
}
5659

60+
const schematicDefault = opt.default;
61+
5762
return {
5863
...opt,
5964
aliases,
6065
type,
61-
default: undefined // do not carry over schematics defaults
66+
schematicType,
67+
default: undefined, // do not carry over schematics defaults
68+
schematicDefault
6269
};
6370
})
6471
.filter(x => x);

0 commit comments

Comments
 (0)