Skip to content

Commit 0255a09

Browse files
committed
feat(ng-add): respect project default inlineStyle, inlineTemplate and spec option value
If developers create a new Angular CLI project, options like `--skipTests`, `--inlineTemplate` or `--inlineStyle` can be specified. These options will be stored in the workspace config file and will be respected if someone generates another component on the existing project. Since we technically also generate components, we should respect the default option value for those options if _not_ explicitly specified. Closes #11874.
1 parent db1d51f commit 0255a09

File tree

16 files changed

+241
-99
lines changed

16 files changed

+241
-99
lines changed

src/lib/schematics/address-form/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
1818
export default function(options: Schema): Rule {
1919
return chain([
2020
buildComponent({...options}, {
21-
template: options.inlineTemplate &&
22-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
23-
stylesheet: options.inlineStyle &&
24-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
21+
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
22+
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
2523
}),
2624
options.skipImport ? noop() : addFormModulesToModule(options)
2725
]);

src/lib/schematics/address-form/schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
"inlineStyle": {
2929
"description": "Specifies if the style will be in the ts file.",
3030
"type": "boolean",
31-
"default": false,
3231
"alias": "s"
3332
},
3433
"inlineTemplate": {
3534
"description": "Specifies if the template will be in the ts file.",
3635
"type": "boolean",
37-
"default": false,
3836
"alias": "t"
3937
},
4038
"viewEncapsulation": {
@@ -62,8 +60,7 @@
6260
},
6361
"spec": {
6462
"type": "boolean",
65-
"description": "Specifies if a spec file is generated.",
66-
"default": true
63+
"description": "Specifies if a spec file is generated."
6764
},
6865
"flat": {
6966
"type": "boolean",

src/lib/schematics/dashboard/index.spec.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,65 @@ describe('material-dashboard-schematic', () => {
4444
`import { MatGridListModule, MatCardModule, MatMenuModule, MatIconModule, MatButtonModule } from '@angular/material';`);
4545
});
4646

47-
it('should support passing the style extension option', () => {
48-
const tree = runner.runSchematic(
49-
'dashboard', {styleext: 'scss', ...baseOptions}, createTestApp());
47+
describe('styleext option', () => {
48+
it('should respect the option value', () => {
49+
const tree = runner.runSchematic(
50+
'dashboard', {styleext: 'scss', ...baseOptions}, createTestApp());
5051

51-
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
52+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
53+
});
54+
55+
it('should fallback to the @schematics/angular:component option value', () => {
56+
const tree = runner.runSchematic('dashboard', baseOptions, createTestApp({style: 'less'}));
57+
58+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
59+
});
5260
});
5361

54-
it('should fallback to the default angular:component style extension', () => {
55-
const tree = runner.runSchematic('dashboard', baseOptions, createTestApp({style: 'less'}));
62+
describe('inlineStyle option', () => {
63+
it('should respect the option value', () => {
64+
const tree = runner.runSchematic(
65+
'dashboard', {inlineStyle: true, ...baseOptions}, createTestApp());
66+
67+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
68+
});
69+
70+
it('should fallback to the @schematics/angular:component option value', () => {
71+
const tree = runner.runSchematic(
72+
'dashboard', baseOptions, createTestApp({inlineStyle: true}));
5673

57-
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
74+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
75+
});
5876
});
5977

78+
describe('inlineTemplate option', () => {
79+
it('should respect the option value', () => {
80+
const tree = runner.runSchematic(
81+
'dashboard', {inlineTemplate: true, ...baseOptions}, createTestApp());
82+
83+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
84+
});
85+
86+
it('should fallback to the @schematics/angular:component option value', () => {
87+
const tree = runner.runSchematic(
88+
'dashboard', baseOptions, createTestApp({inlineTemplate: true}));
89+
90+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
91+
});
92+
});
93+
94+
describe('spec option', () => {
95+
it('should respect the option value', () => {
96+
const tree = runner.runSchematic(
97+
'dashboard', {spec: false, ...baseOptions}, createTestApp());
98+
99+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
100+
});
101+
102+
it('should fallback to the @schematics/angular:component option value', () => {
103+
const tree = runner.runSchematic('dashboard', baseOptions, createTestApp({skipTests: true}));
104+
105+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
106+
});
107+
});
60108
});

src/lib/schematics/dashboard/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {Schema} from './schema';
1818
export default function(options: Schema): Rule {
1919
return chain([
2020
buildComponent({...options}, {
21-
template: options.inlineTemplate &&
22-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
23-
stylesheet: options.inlineStyle &&
24-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
21+
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
22+
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
2523
}),
2624
options.skipImport ? noop() : addNavModulesToModule(options)
2725
]);

src/lib/schematics/dashboard/schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
"inlineStyle": {
2929
"description": "Specifies if the style will be in the ts file.",
3030
"type": "boolean",
31-
"default": false,
3231
"alias": "s"
3332
},
3433
"inlineTemplate": {
3534
"description": "Specifies if the template will be in the ts file.",
3635
"type": "boolean",
37-
"default": false,
3836
"alias": "t"
3937
},
4038
"viewEncapsulation": {
@@ -62,8 +60,7 @@
6260
},
6361
"spec": {
6462
"type": "boolean",
65-
"description": "Specifies if a spec file is generated.",
66-
"default": true
63+
"description": "Specifies if a spec file is generated."
6764
},
6865
"flat": {
6966
"type": "boolean",

src/lib/schematics/nav/index.spec.ts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,61 @@ describe('material-nav-schematic', () => {
4646
`MatListModule } from '@angular/material';`);
4747
});
4848

49-
it('should support passing the style extension option', () => {
50-
const tree = runner.runSchematic('nav', {styleext: 'scss', ...baseOptions}, createTestApp());
49+
describe('styleext option', () => {
50+
it('should respect the option value', () => {
51+
const tree = runner.runSchematic('nav', {styleext: 'scss', ...baseOptions}, createTestApp());
5152

52-
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
53+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
54+
});
55+
56+
it('should fallback to the @schematics/angular:component option value', () => {
57+
const tree = runner.runSchematic('nav', baseOptions, createTestApp({style: 'less'}));
58+
59+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
60+
});
61+
});
62+
63+
describe('inlineStyle option', () => {
64+
it('should respect the option value', () => {
65+
const tree = runner.runSchematic(
66+
'nav', {inlineStyle: true, ...baseOptions}, createTestApp());
67+
68+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
69+
});
70+
71+
it('should fallback to the @schematics/angular:component option value', () => {
72+
const tree = runner.runSchematic('nav', baseOptions, createTestApp({inlineStyle: true}));
73+
74+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
75+
});
5376
});
5477

55-
it('should fallback to the default angular:component style extension', () => {
56-
const tree = runner.runSchematic('nav', baseOptions, createTestApp({style: 'less'}));
78+
describe('inlineTemplate option', () => {
79+
it('should respect the option value', () => {
80+
const tree = runner.runSchematic(
81+
'nav', {inlineTemplate: true, ...baseOptions}, createTestApp());
82+
83+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
84+
});
85+
86+
it('should fallback to the @schematics/angular:component option value', () => {
87+
const tree = runner.runSchematic('nav', baseOptions, createTestApp({inlineTemplate: true}));
88+
89+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
90+
});
91+
});
92+
93+
describe('spec option', () => {
94+
it('should respect the option value', () => {
95+
const tree = runner.runSchematic('nav', {spec: false, ...baseOptions}, createTestApp());
96+
97+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
98+
});
99+
100+
it('should fallback to the @schematics/angular:component option value', () => {
101+
const tree = runner.runSchematic('nav', baseOptions, createTestApp({skipTests: true}));
57102

58-
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
103+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
104+
});
59105
});
60106
});

src/lib/schematics/nav/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
1818
export default function(options: Schema): Rule {
1919
return chain([
2020
buildComponent({...options}, {
21-
template: options.inlineTemplate &&
22-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
23-
stylesheet: options.inlineStyle &&
24-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
21+
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
22+
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
2523
}),
2624
options.skipImport ? noop() : addNavModulesToModule(options)
2725
]);

src/lib/schematics/nav/schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
"inlineStyle": {
2929
"description": "Specifies if the style will be in the ts file.",
3030
"type": "boolean",
31-
"default": false,
3231
"alias": "s"
3332
},
3433
"inlineTemplate": {
3534
"description": "Specifies if the template will be in the ts file.",
3635
"type": "boolean",
37-
"default": false,
3836
"alias": "t"
3937
},
4038
"viewEncapsulation": {
@@ -62,8 +60,7 @@
6260
},
6361
"spec": {
6462
"type": "boolean",
65-
"description": "Specifies if a spec file is generated.",
66-
"default": true
63+
"description": "Specifies if a spec file is generated."
6764
},
6865
"flat": {
6966
"type": "boolean",

src/lib/schematics/table/index.spec.ts

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@ describe('material-table-schematic', () => {
4141
expect(componentContent).toContain('FooDataSource');
4242
});
4343

44-
it('should support passing the style extension option', () => {
45-
const tree = runner.runSchematic('table', {styleext: 'scss', ...baseOptions}, createTestApp());
46-
47-
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
48-
});
49-
50-
it('should fallback to the default angular:component style extension', () => {
51-
const tree = runner.runSchematic('table', baseOptions, createTestApp({style: 'less'}));
52-
53-
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
54-
});
55-
5644
it('should add table imports to module', () => {
5745
const tree = runner.runSchematic('table', baseOptions, createTestApp());
5846
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
@@ -65,4 +53,63 @@ describe('material-table-schematic', () => {
6553
`import { MatTableModule, MatPaginatorModule, MatSortModule } from '@angular/material';`);
6654
});
6755

56+
describe('styleext option', () => {
57+
it('should respect the option value', () => {
58+
const tree = runner.runSchematic(
59+
'table', {styleext: 'scss', ...baseOptions}, createTestApp());
60+
61+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
62+
});
63+
64+
it('should fallback to the @schematics/angular:component option value', () => {
65+
const tree = runner.runSchematic('table', baseOptions, createTestApp({style: 'less'}));
66+
67+
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
68+
});
69+
});
70+
71+
describe('inlineStyle option', () => {
72+
it('should respect the option value', () => {
73+
const tree = runner.runSchematic(
74+
'table', {inlineStyle: true, ...baseOptions}, createTestApp());
75+
76+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
77+
});
78+
79+
it('should fallback to the @schematics/angular:component option value', () => {
80+
const tree = runner.runSchematic('table', baseOptions, createTestApp({inlineStyle: true}));
81+
82+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
83+
});
84+
});
85+
86+
describe('inlineTemplate option', () => {
87+
it('should respect the option value', () => {
88+
const tree = runner.runSchematic(
89+
'table', {inlineTemplate: true, ...baseOptions}, createTestApp());
90+
91+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
92+
});
93+
94+
it('should fallback to the @schematics/angular:component option value', () => {
95+
const tree = runner.runSchematic(
96+
'table', baseOptions, createTestApp({inlineTemplate: true}));
97+
98+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
99+
});
100+
});
101+
102+
describe('spec option', () => {
103+
it('should respect the option value', () => {
104+
const tree = runner.runSchematic('table', {spec: false, ...baseOptions}, createTestApp());
105+
106+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
107+
});
108+
109+
it('should fallback to the @schematics/angular:component option value', () => {
110+
const tree = runner.runSchematic('table', baseOptions, createTestApp({skipTests: true}));
111+
112+
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
113+
});
114+
});
68115
});

src/lib/schematics/table/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
1818
export default function(options: Schema): Rule {
1919
return chain([
2020
buildComponent({...options}, {
21-
template: options.inlineTemplate &&
22-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html'
21+
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html'
2322
}),
2423
options.skipImport ? noop() : addTableModulesToModule(options)
2524
]);

src/lib/schematics/table/schema.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@
2828
"inlineStyle": {
2929
"description": "Specifies if the style will be in the ts file.",
3030
"type": "boolean",
31-
"default": false,
3231
"alias": "s"
3332
},
3433
"inlineTemplate": {
3534
"description": "Specifies if the template will be in the ts file.",
3635
"type": "boolean",
37-
"default": false,
3836
"alias": "t"
3937
},
4038
"viewEncapsulation": {
@@ -62,8 +60,7 @@
6260
},
6361
"spec": {
6462
"type": "boolean",
65-
"description": "Specifies if a spec file is generated.",
66-
"default": true
63+
"description": "Specifies if a spec file is generated."
6764
},
6865
"flat": {
6966
"type": "boolean",

src/lib/schematics/tree/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {Schema} from './schema';
1818
export default function(options: Schema): Rule {
1919
return chain([
2020
buildComponent({...options}, {
21-
template: options.inlineTemplate &&
22-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
23-
stylesheet: options.inlineStyle &&
24-
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
21+
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
22+
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
2523
}),
2624
options.skipImport ? noop() : addTreeModulesToModule(options)
2725
]);

0 commit comments

Comments
 (0)