Skip to content

Commit

Permalink
feat(ng-add): respect project default inlineStyle, inlineTemplate and…
Browse files Browse the repository at this point in the history
… spec option value (#12888)

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
  • Loading branch information
devversion authored and jelbourn committed Aug 29, 2018
1 parent e4ca3cf commit 8831a7a
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 99 deletions.
6 changes: 2 additions & 4 deletions src/lib/schematics/address-form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
export default function(options: Schema): Rule {
return chain([
buildComponent({...options}, {
template: options.inlineTemplate &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: options.inlineStyle &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
}),
options.skipImport ? noop() : addFormModulesToModule(options)
]);
Expand Down
5 changes: 1 addition & 4 deletions src/lib/schematics/address-form/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
"inlineStyle": {
"description": "Specifies if the style will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "s"
},
"inlineTemplate": {
"description": "Specifies if the template will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "t"
},
"viewEncapsulation": {
Expand Down Expand Up @@ -62,8 +60,7 @@
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
"default": true
"description": "Specifies if a spec file is generated."
},
"flat": {
"type": "boolean",
Expand Down
62 changes: 55 additions & 7 deletions src/lib/schematics/dashboard/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,65 @@ describe('material-dashboard-schematic', () => {
`import { MatGridListModule, MatCardModule, MatMenuModule, MatIconModule, MatButtonModule } from '@angular/material';`);
});

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

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('dashboard', baseOptions, createTestApp({style: 'less'}));

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
});
});

it('should fallback to the default angular:component style extension', () => {
const tree = runner.runSchematic('dashboard', baseOptions, createTestApp({style: 'less'}));
describe('inlineStyle option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'dashboard', {inlineStyle: true, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic(
'dashboard', baseOptions, createTestApp({inlineStyle: true}));

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
});
});

describe('inlineTemplate option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'dashboard', {inlineTemplate: true, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic(
'dashboard', baseOptions, createTestApp({inlineTemplate: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
});
});

describe('spec option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'dashboard', {spec: false, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('dashboard', baseOptions, createTestApp({skipTests: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
});
});
});
6 changes: 2 additions & 4 deletions src/lib/schematics/dashboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import {Schema} from './schema';
export default function(options: Schema): Rule {
return chain([
buildComponent({...options}, {
template: options.inlineTemplate &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: options.inlineStyle &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
}),
options.skipImport ? noop() : addNavModulesToModule(options)
]);
Expand Down
5 changes: 1 addition & 4 deletions src/lib/schematics/dashboard/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
"inlineStyle": {
"description": "Specifies if the style will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "s"
},
"inlineTemplate": {
"description": "Specifies if the template will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "t"
},
"viewEncapsulation": {
Expand Down Expand Up @@ -62,8 +60,7 @@
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
"default": true
"description": "Specifies if a spec file is generated."
},
"flat": {
"type": "boolean",
Expand Down
58 changes: 52 additions & 6 deletions src/lib/schematics/nav/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,61 @@ describe('material-nav-schematic', () => {
`MatListModule } from '@angular/material';`);
});

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

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('nav', baseOptions, createTestApp({style: 'less'}));

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
});
});

describe('inlineStyle option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'nav', {inlineStyle: true, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('nav', baseOptions, createTestApp({inlineStyle: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
});
});

it('should fallback to the default angular:component style extension', () => {
const tree = runner.runSchematic('nav', baseOptions, createTestApp({style: 'less'}));
describe('inlineTemplate option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'nav', {inlineTemplate: true, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('nav', baseOptions, createTestApp({inlineTemplate: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
});
});

describe('spec option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic('nav', {spec: false, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('nav', baseOptions, createTestApp({skipTests: true}));

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
});
});
});
6 changes: 2 additions & 4 deletions src/lib/schematics/nav/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
export default function(options: Schema): Rule {
return chain([
buildComponent({...options}, {
template: options.inlineTemplate &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: options.inlineStyle &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
}),
options.skipImport ? noop() : addNavModulesToModule(options)
]);
Expand Down
5 changes: 1 addition & 4 deletions src/lib/schematics/nav/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
"inlineStyle": {
"description": "Specifies if the style will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "s"
},
"inlineTemplate": {
"description": "Specifies if the template will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "t"
},
"viewEncapsulation": {
Expand Down Expand Up @@ -62,8 +60,7 @@
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
"default": true
"description": "Specifies if a spec file is generated."
},
"flat": {
"type": "boolean",
Expand Down
71 changes: 59 additions & 12 deletions src/lib/schematics/table/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ describe('material-table-schematic', () => {
expect(componentContent).toContain('FooDataSource');
});

it('should support passing the style extension option', () => {
const tree = runner.runSchematic('table', {styleext: 'scss', ...baseOptions}, createTestApp());

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
});

it('should fallback to the default angular:component style extension', () => {
const tree = runner.runSchematic('table', baseOptions, createTestApp({style: 'less'}));

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
});

it('should add table imports to module', () => {
const tree = runner.runSchematic('table', baseOptions, createTestApp());
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
Expand All @@ -65,4 +53,63 @@ describe('material-table-schematic', () => {
`import { MatTableModule, MatPaginatorModule, MatSortModule } from '@angular/material';`);
});

describe('styleext option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'table', {styleext: 'scss', ...baseOptions}, createTestApp());

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.scss');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('table', baseOptions, createTestApp({style: 'less'}));

expect(tree.files).toContain('/projects/material/src/app/foo/foo.component.less');
});
});

describe('inlineStyle option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'table', {inlineStyle: true, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('table', baseOptions, createTestApp({inlineStyle: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.css');
});
});

describe('inlineTemplate option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic(
'table', {inlineTemplate: true, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic(
'table', baseOptions, createTestApp({inlineTemplate: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.html');
});
});

describe('spec option', () => {
it('should respect the option value', () => {
const tree = runner.runSchematic('table', {spec: false, ...baseOptions}, createTestApp());

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
});

it('should fallback to the @schematics/angular:component option value', () => {
const tree = runner.runSchematic('table', baseOptions, createTestApp({skipTests: true}));

expect(tree.files).not.toContain('/projects/material/src/app/foo/foo.component.spec.ts');
});
});
});
3 changes: 1 addition & 2 deletions src/lib/schematics/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
export default function(options: Schema): Rule {
return chain([
buildComponent({...options}, {
template: options.inlineTemplate &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html'
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html'
}),
options.skipImport ? noop() : addTableModulesToModule(options)
]);
Expand Down
5 changes: 1 addition & 4 deletions src/lib/schematics/table/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
"inlineStyle": {
"description": "Specifies if the style will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "s"
},
"inlineTemplate": {
"description": "Specifies if the template will be in the ts file.",
"type": "boolean",
"default": false,
"alias": "t"
},
"viewEncapsulation": {
Expand Down Expand Up @@ -62,8 +60,7 @@
},
"spec": {
"type": "boolean",
"description": "Specifies if a spec file is generated.",
"default": true
"description": "Specifies if a spec file is generated."
},
"flat": {
"type": "boolean",
Expand Down
6 changes: 2 additions & 4 deletions src/lib/schematics/tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import {Schema} from './schema';
export default function(options: Schema): Rule {
return chain([
buildComponent({...options}, {
template: options.inlineTemplate &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: options.inlineStyle &&
'./__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
template: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html',
stylesheet: './__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__styleext__',
}),
options.skipImport ? noop() : addTreeModulesToModule(options)
]);
Expand Down
Loading

0 comments on commit 8831a7a

Please sign in to comment.