From f3e84184ea967a6a1a218f4a2f808aa7d44c33e8 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Fri, 27 Oct 2017 16:33:09 -0400 Subject: [PATCH] fix(@angular/cli): Show detailed help for generate Fixes #7723 --- packages/@angular/cli/commands/generate.ts | 58 ++++++++++++++++--- packages/@angular/cli/commands/help.ts | 9 ++- .../cli/tasks/schematic-get-options.ts | 9 ++- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/packages/@angular/cli/commands/generate.ts b/packages/@angular/cli/commands/generate.ts index 5acef463d6..598a97375c 100644 --- a/packages/@angular/cli/commands/generate.ts +++ b/packages/@angular/cli/commands/generate.ts @@ -17,7 +17,7 @@ import { SchematicAvailableOptions } from '../tasks/schematic-get-options'; const Command = require('../ember-cli/lib/models/command'); const SilentError = require('silent-error'); -const { cyan, yellow } = chalk; +const { cyan, grey, yellow } = chalk; const separatorRegEx = /[\/\\]/g; @@ -186,15 +186,57 @@ export default Command.extend({ }); }, - printDetailedHelp: function () { + printDetailedHelp: function (_options: any, rawArgs: any): string | Promise { const engineHost = getEngineHost(); const collectionName = this.getCollectionName(); const collection = getCollection(collectionName); - const schematicNames: string[] = engineHost.listSchematics(collection); - this.ui.writeLine(cyan('Available schematics:')); - schematicNames.forEach(schematicName => { - this.ui.writeLine(yellow(` ${schematicName}`)); - }); - this.ui.writeLine(''); + const schematicName = rawArgs[1]; + if (schematicName) { + const SchematicGetOptionsTask = require('../tasks/schematic-get-options').default; + const getOptionsTask = new SchematicGetOptionsTask({ + ui: this.ui, + project: this.project + }); + return getOptionsTask.run({ + schematicName, + collectionName + }) + .then((availableOptions: SchematicAvailableOptions[]) => { + const output: string[] = []; + output.push(cyan(`ng generate ${schematicName} ${cyan('[name]')} ${cyan('')}`)); + availableOptions + .filter(opt => opt.name !== 'name') + .forEach(opt => { + let text = cyan(` --${opt.name}`); + if (opt.schematicType) { + text += cyan(` (${opt.schematicType})`); + } + if (opt.schematicDefault) { + text += cyan(` (Default: ${opt.schematicDefault})`); + } + if (opt.description) { + text += ` ${opt.description}`; + } + output.push(text); + if (opt.aliases && opt.aliases.length > 0) { + const aliasText = opt.aliases.reduce( + (acc, curr) => { + return acc + ` -${curr}`; + }, + ''); + output.push(grey(` aliases: ${aliasText}`)); + } + }); + return output.join('\n'); + }); + } else { + const schematicNames: string[] = engineHost.listSchematics(collection); + const output: string[] = []; + output.push(cyan('Available schematics:')); + schematicNames.forEach(schematicName => { + output.push(yellow(` ${schematicName}`)); + }); + return Promise.resolve(output.join('\n')); + } } }); diff --git a/packages/@angular/cli/commands/help.ts b/packages/@angular/cli/commands/help.ts index 0b8d73c235..b5f4946ebb 100644 --- a/packages/@angular/cli/commands/help.ts +++ b/packages/@angular/cli/commands/help.ts @@ -66,8 +66,13 @@ const HelpCommand = Command.extend({ if (cmd === commandInput) { if (commandOptions.short) { this.ui.writeLine(command.printShortHelp(commandOptions)); - } else if (command.printDetailedHelp(commandOptions)) { - this.ui.writeLine(command.printDetailedHelp(commandOptions)); + } else if (command.printDetailedHelp(commandOptions, rawArgs)) { + const result = command.printDetailedHelp(commandOptions, rawArgs); + if (result instanceof Promise) { + result.then(r => this.ui.writeLine(r)); + } else { + this.ui.writeLine(result); + } } else { this.ui.writeLine(command.printBasicHelp(commandOptions)); } diff --git a/packages/@angular/cli/tasks/schematic-get-options.ts b/packages/@angular/cli/tasks/schematic-get-options.ts index e42e6e041f..77c300150b 100644 --- a/packages/@angular/cli/tasks/schematic-get-options.ts +++ b/packages/@angular/cli/tasks/schematic-get-options.ts @@ -13,6 +13,8 @@ export interface SchematicAvailableOptions { description: string; aliases: string[]; type: any; + schematicType: any; + schematicDefault: any; } export default Task.extend({ @@ -30,6 +32,7 @@ export default Task.extend({ .map(key => ({...properties[key], ...{name: stringUtils.dasherize(key)}})) .map(opt => { let type; + const schematicType = opt.type; switch (opt.type) { case 'string': type = String; @@ -54,11 +57,15 @@ export default Task.extend({ aliases = [...aliases, ...opt.aliases]; } + const schematicDefault = opt.default; + return { ...opt, aliases, type, - default: undefined // do not carry over schematics defaults + schematicType, + default: undefined, // do not carry over schematics defaults + schematicDefault }; }) .filter(x => x);