Skip to content

Commit 07b87b3

Browse files
BroccoZhicheng Wang
authored and
Zhicheng Wang
committed
refactor(@angular/cli): simplify default arguments in angular-cli.json (angular#4389)
Fixes angular#4137
1 parent bcf0c2b commit 07b87b3

File tree

13 files changed

+173
-63
lines changed

13 files changed

+173
-63
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default Blueprint.extend({
3030

3131
options.spec = options.spec !== undefined ?
3232
options.spec :
33-
this.project.ngConfigObj.get('defaults.spec.class');
33+
this.project.ngConfigObj.get('defaults.class.spec');
3434

3535
return {
3636
dynamicPath: this.dynamicPath.dir,

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default Blueprint.extend({
1414
description: '',
1515

1616
availableOptions: [
17-
{ name: 'flat', type: Boolean, default: false },
17+
{ name: 'flat', type: Boolean },
1818
{ name: 'inline-template', type: Boolean, aliases: ['it'] },
1919
{ name: 'inline-style', type: Boolean, aliases: ['is'] },
2020
{ name: 'prefix', type: String, default: null },
@@ -82,23 +82,27 @@ export default Blueprint.extend({
8282

8383
options.inlineStyle = options.inlineStyle !== undefined ?
8484
options.inlineStyle :
85-
this.project.ngConfigObj.get('defaults.inline.style');
85+
this.project.ngConfigObj.get('defaults.component.inlineStyle');
8686

8787
options.inlineTemplate = options.inlineTemplate !== undefined ?
8888
options.inlineTemplate :
89-
this.project.ngConfigObj.get('defaults.inline.template');
89+
this.project.ngConfigObj.get('defaults.component.inlineTemplate');
90+
91+
options.flat = options.flat !== undefined ?
92+
options.flat :
93+
this.project.ngConfigObj.get('defaults.component.flat');
9094

9195
options.spec = options.spec !== undefined ?
9296
options.spec :
93-
this.project.ngConfigObj.get('defaults.spec.component');
97+
this.project.ngConfigObj.get('defaults.component.spec');
9498

9599
options.viewEncapsulation = options.viewEncapsulation !== undefined ?
96100
options.viewEncapsulation :
97-
this.project.ngConfigObj.get('defaults.viewEncapsulation');
101+
this.project.ngConfigObj.get('defaults.component.viewEncapsulation');
98102

99103
options.changeDetection = options.changeDetection !== undefined ?
100104
options.changeDetection :
101-
this.project.ngConfigObj.get('defaults.changeDetection');
105+
this.project.ngConfigObj.get('defaults.component.changeDetection');
102106

103107
return {
104108
dynamicPath: this.dynamicPath.dir.replace(this.dynamicPath.appRoot, ''),

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default Blueprint.extend({
1414
description: '',
1515

1616
availableOptions: [
17-
{ name: 'flat', type: Boolean, default: true },
17+
{ name: 'flat', type: Boolean },
1818
{ name: 'prefix', type: String, default: null },
1919
{ name: 'spec', type: Boolean },
2020
{ name: 'skip-import', type: Boolean, default: false },
@@ -67,7 +67,11 @@ export default Blueprint.extend({
6767
locals: function (options: any) {
6868
options.spec = options.spec !== undefined ?
6969
options.spec :
70-
this.project.ngConfigObj.get('defaults.spec.directive');
70+
this.project.ngConfigObj.get('defaults.directive.spec');
71+
72+
options.flat = options.flat !== undefined ?
73+
options.flat :
74+
this.project.ngConfigObj.get('defaults.directive.flat');
7175

7276
return {
7377
dynamicPath: this.dynamicPath.dir,

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ export default Blueprint.extend({
2222
if (interfaceType) {
2323
this.fileName += '.' + interfaceType;
2424
}
25-
let prefix = '';
26-
if (this.project.ngConfig &&
27-
this.project.ngConfig.defaults &&
28-
this.project.ngConfig.defaults.prefixInterfaces) {
29-
prefix = 'I';
30-
}
25+
const prefix = this.project.ngConfigObj.get('defaults.interface.prefix');
3126
return {
3227
dynamicPath: this.dynamicPath.dir,
3328
flat: options.flat,

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default Blueprint.extend({
88

99
availableOptions: [
1010
{ name: 'spec', type: Boolean },
11+
{ name: 'flat', type: Boolean },
1112
{ name: 'routing', type: Boolean, default: false }
1213
],
1314

@@ -20,9 +21,13 @@ export default Blueprint.extend({
2021
},
2122

2223
locals: function (options: any) {
24+
options.flat = options.flat !== undefined ?
25+
options.flat :
26+
this.project.ngConfigObj.get('defaults.module.flat');
27+
2328
options.spec = options.spec !== undefined ?
2429
options.spec :
25-
this.project.ngConfigObj.get('defaults.spec.module');
30+
this.project.ngConfigObj.get('defaults.module.spec');
2631

2732
return {
2833
dynamicPath: this.dynamicPath.dir,
@@ -49,9 +54,10 @@ export default Blueprint.extend({
4954
this.dasherizedModuleName = options.dasherizedModuleName;
5055
return {
5156
__path__: () => {
52-
this.generatePath = this.dynamicPath.dir
53-
+ path.sep
54-
+ options.dasherizedModuleName;
57+
this.generatePath = this.dynamicPath.dir;
58+
if (!options.locals.flat) {
59+
this.generatePath += path.sep + options.dasherizedModuleName;
60+
}
5561
return this.generatePath;
5662
}
5763
};

packages/@angular/cli/blueprints/ng2/files/angular-cli.json

+1-13
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@
5151
},
5252
"defaults": {
5353
"styleExt": "<%= styleExt %>",
54-
"prefixInterfaces": false,
55-
"inline": {
56-
"style": false,
57-
"template": false
58-
},
59-
"spec": {
60-
"class": false,
61-
"component": <%= tests %>,
62-
"directive": <%= tests %>,
63-
"module": false,
64-
"pipe": <%= tests %>,
65-
"service": <%= tests %>
66-
}
54+
"component": {}
6755
}
6856
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default Blueprint.extend({
1414
description: '',
1515

1616
availableOptions: [
17-
{ name: 'flat', type: Boolean, default: true },
17+
{ name: 'flat', type: Boolean },
1818
{ name: 'spec', type: Boolean },
1919
{ name: 'skip-import', type: Boolean, default: false },
2020
{ name: 'module', type: String, aliases: ['m'] },
@@ -50,9 +50,13 @@ export default Blueprint.extend({
5050
},
5151

5252
locals: function (options: any) {
53+
options.flat = options.flat !== undefined ?
54+
options.flat :
55+
this.project.ngConfigObj.get('defaults.pipe.flat');
56+
5357
options.spec = options.spec !== undefined ?
5458
options.spec :
55-
this.project.ngConfigObj.get('defaults.spec.pipe');
59+
this.project.ngConfigObj.get('defaults.pipe.spec');
5660

5761
return {
5862
dynamicPath: this.dynamicPath.dir,

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default Blueprint.extend({
1414
description: '',
1515

1616
availableOptions: [
17-
{ name: 'flat', type: Boolean, default: true },
17+
{ name: 'flat', type: Boolean },
1818
{ name: 'spec', type: Boolean },
1919
{ name: 'module', type: String, aliases: ['m'] }
2020
],
@@ -40,9 +40,13 @@ export default Blueprint.extend({
4040
},
4141

4242
locals: function (options: any) {
43+
options.flat = options.flat !== undefined ?
44+
options.flat :
45+
this.project.ngConfigObj.get('defaults.service.flat');
46+
4347
options.spec = options.spec !== undefined ?
4448
options.spec :
45-
this.project.ngConfigObj.get('defaults.spec.service');
49+
this.project.ngConfigObj.get('defaults.service.spec');
4650

4751
return {
4852
dynamicPath: this.dynamicPath.dir,

packages/@angular/cli/lib/config/schema.json

+67-21
Original file line numberDiff line numberDiff line change
@@ -263,55 +263,101 @@
263263
"styleExt": {
264264
"type": "string"
265265
},
266-
"prefixInterfaces": {
267-
"type": "boolean"
268-
},
269266
"poll": {
270267
"type": "number"
271268
},
272-
"viewEncapsulation": {
273-
"type": "string"
274-
},
275-
"changeDetection": {
276-
"type": "string"
269+
"class": {
270+
"type": "object",
271+
"properties": {
272+
"spec": {
273+
"type": "boolean",
274+
"default": false
275+
}
276+
}
277277
},
278-
"inline": {
278+
"component": {
279279
"type": "object",
280280
"properties": {
281-
"style": {
281+
"flat": {
282+
"type": "boolean",
283+
"default": false
284+
},
285+
"spec": {
286+
"type": "boolean",
287+
"default": true
288+
},
289+
"inlineStyle": {
282290
"type": "boolean",
283291
"default": false
284292
},
285-
"template": {
293+
"inlineTemplate": {
286294
"type": "boolean",
287295
"default": false
296+
},
297+
"viewEncapsulation": {
298+
"enum": ["Emulated", "Native", "None"]
299+
},
300+
"changeDetection": {
301+
"enum": ["Default", "OnPush"]
288302
}
289303
}
290304
},
291-
"spec": {
305+
"directive": {
292306
"type": "object",
293307
"properties": {
294-
"class": {
295-
"type": "boolean",
296-
"default": false
297-
},
298-
"component": {
308+
"flat": {
299309
"type": "boolean",
300310
"default": true
301311
},
302-
"directive": {
312+
"spec": {
303313
"type": "boolean",
304314
"default": true
315+
}
316+
}
317+
},
318+
"interface": {
319+
"type": "object",
320+
"properties": {
321+
"prefix": {
322+
"type": "string",
323+
"default": ""
324+
}
325+
}
326+
},
327+
"module": {
328+
"type": "object",
329+
"properties": {
330+
"flat": {
331+
"type": "boolean",
332+
"default": false
305333
},
306-
"module": {
334+
"spec": {
307335
"type": "boolean",
308336
"default": false
337+
}
338+
}
339+
},
340+
"pipe": {
341+
"type": "object",
342+
"properties": {
343+
"flat": {
344+
"type": "boolean",
345+
"default": true
309346
},
310-
"pipe": {
347+
"spec": {
348+
"type": "boolean",
349+
"default": true
350+
}
351+
}
352+
},
353+
"service": {
354+
"type": "object",
355+
"properties": {
356+
"flat": {
311357
"type": "boolean",
312358
"default": true
313359
},
314-
"service": {
360+
"spec": {
315361
"type": "boolean",
316362
"default": true
317363
}

packages/@angular/cli/models/config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ export class CliConfig extends CliConfigBase<ConfigInterface> {
5252
cliConfig.alias('apps.0.prefix', 'defaults.prefix')
5353
];
5454

55+
// Additional aliases which do not emit any messages.
56+
cliConfig.alias('defaults.interface.prefix', 'defaults.inline.prefixInterfaces');
57+
cliConfig.alias('defaults.component.inlineStyle', 'defaults.inline.style');
58+
cliConfig.alias('defaults.component.inlineTemplate', 'defaults.inline.template');
59+
cliConfig.alias('defaults.component.spec', 'defaults.spec.component');
60+
cliConfig.alias('defaults.class.spec', 'defaults.spec.class');
61+
cliConfig.alias('defaults.component.directive', 'defaults.spec.directive');
62+
cliConfig.alias('defaults.component.module', 'defaults.spec.module');
63+
cliConfig.alias('defaults.component.pipe', 'defaults.spec.pipe');
64+
cliConfig.alias('defaults.component.service', 'defaults.spec.service');
65+
5566
// If any of them returned true, output a deprecation warning.
5667
if (aliases.some(x => !!x)) {
5768
console.error(chalk.yellow(oneLine`

tests/e2e/tests/commands/config/get.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import {expectToFail} from '../../../utils/utils';
55
export default function() {
66
return Promise.resolve()
77
.then(() => process.chdir('/'))
8-
.then(() => expectToFail(() => ng('get', 'defaults.inline.style')))
9-
.then(() => ng('get', '--global', 'defaults.inline.style'))
8+
.then(() => expectToFail(() => ng('get', 'defaults.component.inlineStyle')))
9+
.then(() => ng('get', '--global', 'defaults.component.inlineStyle'))
1010
.then(output => {
1111
if (!output.match(/false\n?/)) {
1212
throw new Error(`Expected "false", received "${JSON.stringify(output)}".`);
1313
}
1414
})
1515
.then(() => expectToFail(() => {
16-
return ng('set', '--global', 'defaults.inline.style', 'INVALID_BOOLEAN');
16+
return ng('set', '--global', 'defaults.component.inlineStyle', 'INVALID_BOOLEAN');
1717
}))
18-
.then(() => ng('set', '--global', 'defaults.inline.style', 'true'))
19-
.then(() => ng('get', '--global', 'defaults.inline.style'))
18+
.then(() => ng('set', '--global', 'defaults.component.inlineStyle', 'true'))
19+
.then(() => ng('get', '--global', 'defaults.component.inlineStyle'))
2020
.then(output => {
2121
if (!output.match(/true\n?/)) {
2222
throw new Error(`Expected "true", received "${JSON.stringify(output)}".`);
2323
}
2424
})
25-
.then(() => ng('set', '--global', 'defaults.inline.style', 'false'));
25+
.then(() => ng('set', '--global', 'defaults.component.inlineStyle', 'false'));
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {join} from 'path';
2+
import {ng} from '../../../utils/process';
3+
import {expectFileToExist} from '../../../utils/fs';
4+
import {updateJsonFile} from '../../../utils/project';
5+
6+
7+
export default function() {
8+
const appDir = join('src', 'app');
9+
10+
return Promise.resolve()
11+
.then(() => updateJsonFile('angular-cli.json', configJson => {
12+
const comp = configJson.defaults.component;
13+
comp.flat = true;
14+
}))
15+
.then(() => ng('generate', 'component', 'test-component'))
16+
.then(() => expectFileToExist(appDir))
17+
.then(() => expectFileToExist(join(appDir, 'test-component.component.ts')))
18+
.then(() => expectFileToExist(join(appDir, 'test-component.component.spec.ts')))
19+
.then(() => expectFileToExist(join(appDir, 'test-component.component.html')))
20+
.then(() => expectFileToExist(join(appDir, 'test-component.component.css')))
21+
22+
// Try to run the unit tests.
23+
.then(() => ng('test', '--single-run'));
24+
}

0 commit comments

Comments
 (0)