Skip to content

Commit da8ef74

Browse files
committed
refactor(@angular/cli): simplify default arguments in angular-cli.json
Fixes #4137
1 parent 3b62a93 commit da8ef74

File tree

14 files changed

+167
-63
lines changed

14 files changed

+167
-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
@@ -13,7 +13,7 @@ export default Blueprint.extend({
1313
description: '',
1414

1515
availableOptions: [
16-
{ name: 'flat', type: Boolean, default: false },
16+
{ name: 'flat', type: Boolean },
1717
{ name: 'inline-template', type: Boolean, aliases: ['it'] },
1818
{ name: 'inline-style', type: Boolean, aliases: ['is'] },
1919
{ name: 'prefix', type: String, default: null },
@@ -81,23 +81,27 @@ export default Blueprint.extend({
8181

8282
options.inlineStyle = options.inlineStyle !== undefined ?
8383
options.inlineStyle :
84-
this.project.ngConfigObj.get('defaults.inline.style');
84+
this.project.ngConfigObj.get('defaults.component.inlineStyle');
8585

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

9094
options.spec = options.spec !== undefined ?
9195
options.spec :
92-
this.project.ngConfigObj.get('defaults.spec.component');
96+
this.project.ngConfigObj.get('defaults.component.spec');
9397

9498
options.viewEncapsulation = options.viewEncapsulation !== undefined ?
9599
options.viewEncapsulation :
96-
this.project.ngConfigObj.get('defaults.viewEncapsulation');
100+
this.project.ngConfigObj.get('defaults.component.viewEncapsulation');
97101

98102
options.changeDetection = options.changeDetection !== undefined ?
99103
options.changeDetection :
100-
this.project.ngConfigObj.get('defaults.changeDetection');
104+
this.project.ngConfigObj.get('defaults.component.changeDetection');
101105

102106
return {
103107
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
@@ -13,7 +13,7 @@ export default Blueprint.extend({
1313
description: '',
1414

1515
availableOptions: [
16-
{ name: 'flat', type: Boolean, default: true },
16+
{ name: 'flat', type: Boolean },
1717
{ name: 'prefix', type: String, default: null },
1818
{ name: 'spec', type: Boolean },
1919
{ name: 'skip-import', type: Boolean, default: false },
@@ -66,7 +66,11 @@ export default Blueprint.extend({
6666
locals: function (options: any) {
6767
options.spec = options.spec !== undefined ?
6868
options.spec :
69-
this.project.ngConfigObj.get('defaults.spec.directive');
69+
this.project.ngConfigObj.get('defaults.directive.spec');
70+
71+
options.flat = options.flat !== undefined ?
72+
options.flat :
73+
this.project.ngConfigObj.get('defaults.directive.flat');
7074

7175
return {
7276
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
@@ -50,18 +50,6 @@
5050
},
5151
"defaults": {
5252
"styleExt": "<%= styleExt %>",
53-
"prefixInterfaces": false,
54-
"inline": {
55-
"style": false,
56-
"template": false
57-
},
58-
"spec": {
59-
"class": false,
60-
"component": <%= tests %>,
61-
"directive": <%= tests %>,
62-
"module": false,
63-
"pipe": <%= tests %>,
64-
"service": <%= tests %>
65-
}
53+
"component": {}
6654
}
6755
}

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

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

1515
availableOptions: [
16-
{ name: 'flat', type: Boolean, default: true },
16+
{ name: 'flat', type: Boolean },
1717
{ name: 'spec', type: Boolean },
1818
{ name: 'skip-import', type: Boolean, default: false },
1919
{ name: 'module', type: String, aliases: ['m'] },
@@ -49,9 +49,13 @@ export default Blueprint.extend({
4949
},
5050

5151
locals: function (options: any) {
52+
options.flat = options.flat !== undefined ?
53+
options.flat :
54+
this.project.ngConfigObj.get('defaults.pipe.flat');
55+
5256
options.spec = options.spec !== undefined ?
5357
options.spec :
54-
this.project.ngConfigObj.get('defaults.spec.pipe');
58+
this.project.ngConfigObj.get('defaults.pipe.spec');
5559

5660
return {
5761
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
@@ -219,55 +219,101 @@
219219
"styleExt": {
220220
"type": "string"
221221
},
222-
"prefixInterfaces": {
223-
"type": "boolean"
224-
},
225222
"poll": {
226223
"type": "number"
227224
},
228-
"viewEncapsulation": {
229-
"type": "string"
230-
},
231-
"changeDetection": {
232-
"type": "string"
225+
"class": {
226+
"type": "object",
227+
"properties": {
228+
"spec": {
229+
"type": "boolean",
230+
"default": false
231+
}
232+
}
233233
},
234-
"inline": {
234+
"component": {
235235
"type": "object",
236236
"properties": {
237-
"style": {
237+
"flat": {
238238
"type": "boolean",
239239
"default": false
240240
},
241-
"template": {
241+
"spec": {
242+
"type": "boolean",
243+
"default": true
244+
},
245+
"inlineStyle": {
242246
"type": "boolean",
243247
"default": false
248+
},
249+
"inlineTemplate": {
250+
"type": "boolean",
251+
"default": false
252+
},
253+
"viewEncapsulation": {
254+
"type": "string"
255+
},
256+
"changeDetection": {
257+
"type": "string"
244258
}
245259
}
246260
},
247-
"spec": {
261+
"directive": {
248262
"type": "object",
249263
"properties": {
250-
"class": {
251-
"type": "boolean",
252-
"default": false
253-
},
254-
"component": {
264+
"flat": {
255265
"type": "boolean",
256266
"default": true
257267
},
258-
"directive": {
268+
"spec": {
259269
"type": "boolean",
260270
"default": true
271+
}
272+
}
273+
},
274+
"interface": {
275+
"type": "object",
276+
"properties": {
277+
"prefix": {
278+
"type": "string",
279+
"default": ""
280+
}
281+
}
282+
},
283+
"module": {
284+
"type": "object",
285+
"properties": {
286+
"flat": {
287+
"type": "boolean",
288+
"default": false
261289
},
262-
"module": {
290+
"spec": {
263291
"type": "boolean",
264292
"default": false
293+
}
294+
}
295+
},
296+
"pipe": {
297+
"type": "object",
298+
"properties": {
299+
"flat": {
300+
"type": "boolean",
301+
"default": true
265302
},
266-
"pipe": {
303+
"spec": {
304+
"type": "boolean",
305+
"default": true
306+
}
307+
}
308+
},
309+
"service": {
310+
"type": "object",
311+
"properties": {
312+
"flat": {
267313
"type": "boolean",
268314
"default": true
269315
},
270-
"service": {
316+
"spec": {
271317
"type": "boolean",
272318
"default": true
273319
}

packages/@ngtools/json-schema/src/schema-class-factory.ts

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ class SchemaClassBase<T> implements SchemaClass<T> {
117117
/** Get a value from a JSON path. */
118118
$$get(path: string): any {
119119
const node = _getSchemaNodeForPath(this.$$schema(), path);
120+
if (path === 'defaults.component.viewEncapsulation') {
121+
console.log('node');
122+
console.log(node);
123+
}
120124
return node ? node.get() : undefined;
121125
}
122126

packages/@ngtools/json-schema/src/schema-tree.ts

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
407407

408408
get() {
409409
if (!this.defined && this._forward) {
410+
console.log('fwd');
410411
return this._forward.get();
411412
}
412413
if (!this.defined) {

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)