|
| 1 | +import * as chalk from 'chalk'; |
1 | 2 | import * as fs from 'fs';
|
2 |
| -import * as path from 'path'; |
3 | 3 | import * as os from 'os';
|
| 4 | +import * as path from 'path'; |
| 5 | +import { oneLine } from 'common-tags'; |
4 | 6 |
|
5 |
| -const chalk = require('chalk'); |
6 |
| -const EmberGenerateCommand = require('../ember-cli/lib/commands/generate'); |
| 7 | +const Command = require('../ember-cli/lib/models/command'); |
7 | 8 | const Blueprint = require('../ember-cli/lib/models/blueprint');
|
| 9 | +const parseOptions = require('../ember-cli/lib/utilities/parse-options'); |
8 | 10 | const SilentError = require('silent-error');
|
9 | 11 |
|
10 |
| -const blueprintList = fs.readdirSync(path.join(__dirname, '..', 'blueprints')); |
11 |
| -const blueprints = blueprintList |
12 |
| - .filter(bp => bp.indexOf('-test') === -1) |
13 |
| - .filter(bp => bp !== 'ng') |
14 |
| - .map(bp => Blueprint.load(path.join(__dirname, '..', 'blueprints', bp))); |
| 12 | +function loadBlueprints(): Array<any> { |
| 13 | + const blueprintList = fs.readdirSync(path.join(__dirname, '..', 'blueprints')); |
| 14 | + const blueprints = blueprintList |
| 15 | + .filter(bp => bp.indexOf('-test') === -1) |
| 16 | + .filter(bp => bp !== 'ng') |
| 17 | + .map(bp => Blueprint.load(path.join(__dirname, '..', 'blueprints', bp))); |
15 | 18 |
|
16 |
| -const GenerateCommand = EmberGenerateCommand.extend({ |
| 19 | + return blueprints; |
| 20 | +} |
| 21 | + |
| 22 | +export default Command.extend({ |
17 | 23 | name: 'generate',
|
| 24 | + description: 'Generates and/or modifies files based on a blueprint.', |
| 25 | + aliases: ['g'], |
| 26 | + |
| 27 | + availableOptions: [ |
| 28 | + { |
| 29 | + name: 'dry-run', |
| 30 | + type: Boolean, |
| 31 | + default: false, |
| 32 | + aliases: ['d'], |
| 33 | + description: 'Run through without making any changes.' |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'verbose', |
| 37 | + type: Boolean, |
| 38 | + default: false, |
| 39 | + aliases: ['v'], |
| 40 | + description: 'Adds more details to output logging.' |
| 41 | + } |
| 42 | + ], |
18 | 43 |
|
19 |
| - blueprints: blueprints, |
| 44 | + anonymousOptions: [ |
| 45 | + '<blueprint>' |
| 46 | + ], |
20 | 47 |
|
21 | 48 | beforeRun: function (rawArgs: string[]) {
|
22 | 49 | if (!rawArgs.length) {
|
23 | 50 | return;
|
24 | 51 | }
|
25 | 52 |
|
26 |
| - // map the blueprint name to allow for aliases |
27 |
| - rawArgs[0] = mapBlueprintName(rawArgs[0]); |
| 53 | + const isHelp = ['--help', '-h'].includes(rawArgs[0]); |
| 54 | + if (isHelp) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + this.blueprints = loadBlueprints(); |
| 59 | + |
| 60 | + const name = rawArgs[0]; |
| 61 | + const blueprint = this.blueprints.find((bp: any) => bp.name === name |
| 62 | + || (bp.aliases && bp.aliases.includes(name))); |
28 | 63 |
|
29 |
| - const isHelp: boolean = ['--help', '-h'].indexOf(rawArgs[0]) > -1; |
30 |
| - if (!isHelp && !fs.existsSync(path.join(__dirname, '..', 'blueprints', rawArgs[0]))) { |
| 64 | + if (!blueprint) { |
31 | 65 | SilentError.debugOrThrow('@angular/cli/commands/generate',
|
32 |
| - `Invalid blueprint: ${rawArgs[0]}`); |
| 66 | + `Invalid blueprint: ${name}`); |
33 | 67 | }
|
34 | 68 |
|
35 |
| - if (!isHelp && !rawArgs[1]) { |
| 69 | + if (!rawArgs[1]) { |
36 | 70 | SilentError.debugOrThrow('@angular/cli/commands/generate',
|
37 |
| - `The \`ng generate ${rawArgs[0]}\` command requires a name to be specified.`); |
| 71 | + `The \`ng generate ${name}\` command requires a name to be specified.`); |
38 | 72 | }
|
39 | 73 |
|
40 |
| - // Override default help to hide ember blueprints |
41 |
| - EmberGenerateCommand.prototype.printDetailedHelp = function () { |
42 |
| - this.ui.writeLine(chalk.cyan(' Available blueprints')); |
43 |
| - this.ui.writeLine(blueprints.map(bp => bp.printBasicHelp(false)).join(os.EOL)); |
| 74 | + rawArgs[0] = blueprint.name; |
| 75 | + this.registerOptions(blueprint); |
| 76 | + }, |
| 77 | + |
| 78 | + printDetailedHelp: function () { |
| 79 | + if (!this.blueprints) { |
| 80 | + this.blueprints = loadBlueprints(); |
| 81 | + } |
| 82 | + this.ui.writeLine(chalk.cyan(' Available blueprints')); |
| 83 | + this.ui.writeLine(this.blueprints.map((bp: any) => bp.printBasicHelp(false)).join(os.EOL)); |
| 84 | + }, |
| 85 | + |
| 86 | + run: function (commandOptions: any, rawArgs: string[]) { |
| 87 | + const name = rawArgs[0]; |
| 88 | + if (!name) { |
| 89 | + return Promise.reject(new SilentError(oneLine` |
| 90 | + The "ng generate" command requires a |
| 91 | + blueprint name to be specified. |
| 92 | + For more details, use "ng help". |
| 93 | + `)); |
| 94 | + } |
| 95 | + |
| 96 | + const blueprint = this.blueprints.find((bp: any) => bp.name === name |
| 97 | + || (bp.aliases && bp.aliases.includes(name))); |
| 98 | + |
| 99 | + const blueprintOptions = { |
| 100 | + target: this.project.root, |
| 101 | + entity: { |
| 102 | + name: rawArgs[1], |
| 103 | + options: parseOptions(rawArgs.slice(2)) |
| 104 | + }, |
| 105 | + ui: this.ui, |
| 106 | + project: this.project, |
| 107 | + settings: this.settings, |
| 108 | + testing: this.testing, |
| 109 | + args: rawArgs, |
| 110 | + ...commandOptions |
44 | 111 | };
|
45 | 112 |
|
46 |
| - return EmberGenerateCommand.prototype.beforeRun.apply(this, arguments); |
| 113 | + return blueprint.install(blueprintOptions); |
47 | 114 | }
|
48 | 115 | });
|
49 |
| - |
50 |
| -function mapBlueprintName(name: string): string { |
51 |
| - let mappedName: string = aliasMap[name]; |
52 |
| - return mappedName ? mappedName : name; |
53 |
| -} |
54 |
| - |
55 |
| -const aliasMap: { [alias: string]: string } = { |
56 |
| - 'cl': 'class', |
57 |
| - 'c': 'component', |
58 |
| - 'd': 'directive', |
59 |
| - 'e': 'enum', |
60 |
| - 'g': 'guard', |
61 |
| - 'i': 'interface', |
62 |
| - 'm': 'module', |
63 |
| - 'p': 'pipe', |
64 |
| - 'r': 'route', |
65 |
| - 's': 'service' |
66 |
| -}; |
67 |
| - |
68 |
| -export default GenerateCommand; |
69 |
| -GenerateCommand.overrideCore = true; |
|
0 commit comments