Skip to content

Commit 624b7b0

Browse files
leonLeon Radley
authored and
Leon Radley
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 624b7b0

8 files changed

+52
-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

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
4949
return host;
5050
}
5151

52+
options.type = !!options.type ? options.type : 'Component';
53+
5254
const modulePath = options.module;
5355
const source = readIntoSourceFile(host, modulePath);
5456

5557
const componentPath = `/${options.path}/`
5658
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
5759
+ strings.dasherize(options.name)
58-
+ '.component';
60+
+ '.'
61+
+ strings.dasherize(options.type);
5962
const relativePath = buildRelativePath(modulePath, componentPath);
60-
const classifiedName = strings.classify(`${options.name}Component`);
63+
const classifiedName = strings.classify(options.name) + strings.classify(options.type);
6164
const declarationChanges = addDeclarationToModule(source,
6265
modulePath,
6366
classifiedName,
@@ -77,7 +80,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
7780

7881
const exportRecorder = host.beginUpdate(modulePath);
7982
const exportChanges = addExportToModule(source, modulePath,
80-
strings.classify(`${options.name}Component`),
83+
strings.classify(options.name) + strings.classify(options.type),
8184
relativePath);
8285

8386
for (const change of exportChanges) {
@@ -95,7 +98,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
9598
const entryComponentRecorder = host.beginUpdate(modulePath);
9699
const entryComponentChanges = addEntryComponentToModule(
97100
source, modulePath,
98-
strings.classify(`${options.name}Component`),
101+
strings.classify(options.name) + strings.classify(options.type),
99102
relativePath);
100103

101104
for (const change of entryComponentChanges) {

packages/schematics/angular/component/index_spec.ts

+12
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,
@@ -256,6 +257,17 @@ describe('Component Schematic', () => {
256257
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.css');
257258
});
258259

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