Skip to content

Commit 5cf395c

Browse files
Charles Lydingfilipesilva
Charles Lyding
authored andcommitted
refactor(@angular/cli): simplify blueprint generation command
1 parent 8035f54 commit 5cf395c

File tree

10 files changed

+98
-43
lines changed

10 files changed

+98
-43
lines changed

packages/@angular/cli/blueprints/class/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
77
const getFiles = Blueprint.prototype.files;
88

99
export default Blueprint.extend({
10+
name: 'class',
1011
description: '',
1112
aliases: ['cl'],
1213

packages/@angular/cli/blueprints/component/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function correctCase(options: any) {
3636
}
3737

3838
export default Blueprint.extend({
39+
name: 'component',
3940
description: '',
4041
aliases: ['c'],
4142

packages/@angular/cli/blueprints/directive/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
1414
const getFiles = Blueprint.prototype.files;
1515

1616
export default Blueprint.extend({
17+
name: 'directive',
1718
description: '',
1819
aliases: ['d'],
1920

packages/@angular/cli/blueprints/enum/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const stringUtils = require('ember-cli-string-utils');
55
const Blueprint = require('../../ember-cli/lib/models/blueprint');
66

77
export default Blueprint.extend({
8+
name: 'enum',
89
description: '',
910
aliases: ['e'],
1011

packages/@angular/cli/blueprints/guard/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const astUtils = require('../../utilities/ast-utils');
1313
const getFiles = Blueprint.prototype.files;
1414

1515
export default Blueprint.extend({
16+
name: 'guard',
1617
description: '',
1718
aliases: ['g'],
1819

packages/@angular/cli/blueprints/interface/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const stringUtils = require('ember-cli-string-utils');
66
const Blueprint = require('../../ember-cli/lib/models/blueprint');
77

88
export default Blueprint.extend({
9+
name: 'interface',
910
description: '',
1011
aliases: ['i'],
1112

packages/@angular/cli/blueprints/module/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const astUtils = require('../../utilities/ast-utils');
1313
const getFiles = Blueprint.prototype.files;
1414

1515
export default Blueprint.extend({
16+
name: 'module',
1617
description: '',
1718
aliases: ['m'],
1819

packages/@angular/cli/blueprints/pipe/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const Blueprint = require('../../ember-cli/lib/models/blueprint');
1313
const getFiles = Blueprint.prototype.files;
1414

1515
export default Blueprint.extend({
16+
name: 'pipe',
1617
description: '',
1718
aliases: ['p'],
1819

packages/@angular/cli/blueprints/service/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const astUtils = require('../../utilities/ast-utils');
1313
const getFiles = Blueprint.prototype.files;
1414

1515
export default Blueprint.extend({
16+
name: 'service',
1617
description: '',
1718
aliases: ['s'],
1819

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

+89-43
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,115 @@
1+
import * as chalk from 'chalk';
12
import * as fs from 'fs';
2-
import * as path from 'path';
33
import * as os from 'os';
4+
import * as path from 'path';
5+
import { oneLine } from 'common-tags';
46

5-
const chalk = require('chalk');
6-
const EmberGenerateCommand = require('../ember-cli/lib/commands/generate');
7+
const Command = require('../ember-cli/lib/models/command');
78
const Blueprint = require('../ember-cli/lib/models/blueprint');
9+
const parseOptions = require('../ember-cli/lib/utilities/parse-options');
810
const SilentError = require('silent-error');
911

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)));
1518

16-
const GenerateCommand = EmberGenerateCommand.extend({
19+
return blueprints;
20+
}
21+
22+
export default Command.extend({
1723
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+
],
1843

19-
blueprints: blueprints,
44+
anonymousOptions: [
45+
'<blueprint>'
46+
],
2047

2148
beforeRun: function (rawArgs: string[]) {
2249
if (!rawArgs.length) {
2350
return;
2451
}
2552

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)));
2863

29-
const isHelp: boolean = ['--help', '-h'].indexOf(rawArgs[0]) > -1;
30-
if (!isHelp && !fs.existsSync(path.join(__dirname, '..', 'blueprints', rawArgs[0]))) {
64+
if (!blueprint) {
3165
SilentError.debugOrThrow('@angular/cli/commands/generate',
32-
`Invalid blueprint: ${rawArgs[0]}`);
66+
`Invalid blueprint: ${name}`);
3367
}
3468

35-
if (!isHelp && !rawArgs[1]) {
69+
if (!rawArgs[1]) {
3670
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.`);
3872
}
3973

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
44111
};
45112

46-
return EmberGenerateCommand.prototype.beforeRun.apply(this, arguments);
113+
return blueprint.install(blueprintOptions);
47114
}
48115
});
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

Comments
 (0)