Skip to content

Commit 7c88001

Browse files
committed
feat(@schematics/angular/component): add type option
the type option allows you to change the default .component.ts suffix to new types. such as Route Dialog UI Container angular#15680
1 parent 1bddf8b commit 7c88001

8 files changed

+51
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { <%= classify(name) %><%= classify(type) %> } from './<%= dasherize(name) %>.<%= dasherize(type) %>';
4+
5+
describe('<%= classify(name) %><%= classify(type) %>', () => {
6+
let component: <%= classify(name) %><%= classify(type) %>;
7+
let fixture: ComponentFixture<<%= classify(name) %><%= classify(type) %>>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ <%= classify(name) %><%= classify(type) %> ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(<%= classify(name) %><%= classify(type) %>);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
77
<%= dasherize(name) %> works!
88
</p>
99
`,<% } else { %>
10-
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
10+
templateUrl: './<%= dasherize(name) %>.<%= dasherize(type) %>.html',<% } if(inlineStyle) { %>
1111
styles: []<% } else { %>
12-
styleUrls: ['./<%= dasherize(name) %>.component.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
12+
styleUrls: ['./<%= dasherize(name) %>.<%= dasherize(type) %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
1313
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1414
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
1515
})
16-
export class <%= classify(name) %>Component implements OnInit {
16+
export class <%= classify(name) %><%= classify(type) %> implements OnInit {
1717

1818
constructor() { }
1919

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts.template

-25
This file was deleted.

packages/schematics/angular/component/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
5555
const componentPath = `/${options.path}/`
5656
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
5757
+ strings.dasherize(options.name)
58-
+ '.component';
58+
+ '.'
59+
+ strings.dasherize(options.type);
5960
const relativePath = buildRelativePath(modulePath, componentPath);
60-
const classifiedName = strings.classify(`${options.name}Component`);
61+
const classifiedName = strings.classify(options.name) + strings.classify(options.type);
6162
const declarationChanges = addDeclarationToModule(source,
6263
modulePath,
6364
classifiedName,
@@ -77,7 +78,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
7778

7879
const exportRecorder = host.beginUpdate(modulePath);
7980
const exportChanges = addExportToModule(source, modulePath,
80-
strings.classify(`${options.name}Component`),
81+
strings.classify(options.name) + strings.classify(options.type),
8182
relativePath);
8283

8384
for (const change of exportChanges) {
@@ -95,7 +96,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
9596
const entryComponentRecorder = host.beginUpdate(modulePath);
9697
const entryComponentChanges = addEntryComponentToModule(
9798
source, modulePath,
98-
strings.classify(`${options.name}Component`),
99+
strings.classify(options.name) + strings.classify(options.type),
99100
relativePath);
100101

101102
for (const change of entryComponentChanges) {

packages/schematics/angular/component/index_spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Component Schematic', () => {
2424
inlineTemplate: false,
2525
changeDetection: ChangeDetection.Default,
2626
style: Style.Css,
27+
type: 'Component',
2728
skipTests: false,
2829
module: undefined,
2930
export: false,
@@ -42,6 +43,7 @@ describe('Component Schematic', () => {
4243
inlineTemplate: false,
4344
routing: false,
4445
style: Style.Css,
46+
type: 'Component',
4547
skipTests: false,
4648
skipPackageJson: false,
4749
};
@@ -256,6 +258,17 @@ describe('Component Schematic', () => {
256258
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
257259
});
258260

261+
it('should respect the type option', async () => {
262+
const options = { ...defaultOptions, type: 'Route' };
263+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
264+
const content = tree.readContent('/projects/bar/src/app/foo/foo.route.ts');
265+
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.route.spec.ts');
266+
expect(content).toContain('export class FooRoute implements OnInit');
267+
expect(testContent).toContain('describe(\'FooRoute\'');
268+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.css');
269+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
270+
});
271+
259272
it('should use the module flag even if the module is a routing module', async () => {
260273
const routingFileName = 'app-routing.module.ts';
261274
const routingModulePath = `/projects/bar/src/app/${routingFileName}`;

packages/schematics/angular/component/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
],
8989
"x-user-analytics": 5
9090
},
91+
"type": {
92+
"type": "string",
93+
"description": "Adds a developer-defined type to the filename, in the format \"name.type.ts\".",
94+
"default": "Component"
95+
},
9196
"spec": {
9297
"type": "boolean",
9398
"description": "When true (the default), generates a \"spec.ts\" test file for the new component.",

0 commit comments

Comments
 (0)