Skip to content

Commit 9849d48

Browse files
chrste90hansl
authored andcommitted
refactor(@schematics/angular): Add prefix handling
1 parent 3fa72b6 commit 9849d48

File tree

14 files changed

+79
-8
lines changed

14 files changed

+79
-8
lines changed

packages/angular_devkit/core/src/workspace/workspace-schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
"type": "string",
6161
"description": "Root of the project sourcefiles."
6262
},
63+
"prefix": {
64+
"type": "string",
65+
"description": "The prefix to apply to generated selectors."
66+
},
6367
"cli": {
6468
"$ref": "#/definitions/tool",
6569
"default": {}
@@ -91,4 +95,4 @@
9195
"additionalProperties": true
9296
}
9397
}
94-
}
98+
}

packages/angular_devkit/core/src/workspace/workspace-schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export interface Project {
6868
* Root of the project sourcefiles.
6969
*/
7070
root: string;
71+
/**
72+
* The prefix to apply to generated selectors."
73+
*/
74+
prefix: string;
7175
/**
7276
* Tool options.
7377
*/

packages/angular_devkit/core/src/workspace/workspace_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('Workspace', () => {
5151
app: {
5252
root: 'projects/app',
5353
projectType: 'application',
54+
prefix: 'app',
5455
cli: {},
5556
schematics: {
5657
'@schematics/angular': {

packages/schematics/angular/application/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
110110
const project: any = {
111111
root: projectRoot,
112112
projectType: 'application',
113+
prefix: options.prefix || 'app',
113114
architect: {
114115
build: {
115116
builder: '@angular-devkit/build-angular:browser',

packages/schematics/angular/application/index_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ describe('Application Schematic', () => {
7171
expect(workspace.projects.foo).toBeDefined();
7272
});
7373

74+
it('should set the prefix to app if none is set', () => {
75+
const options = { ...defaultOptions };
76+
77+
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
78+
const workspace = JSON.parse(tree.readContent('/angular.json'));
79+
expect(workspace.projects.foo.prefix).toEqual('app');
80+
});
81+
82+
it('should set the prefix correctly', () => {
83+
const options = { ...defaultOptions, prefix: 'pre' };
84+
85+
const tree = schematicRunner.runSchematic('application', options, workspaceTree);
86+
const workspace = JSON.parse(tree.readContent('/angular.json'));
87+
expect(workspace.projects.foo.prefix).toEqual('pre');
88+
});
89+
7490
it('should handle the routing flag', () => {
7591
const options = { ...defaultOptions, routing: true };
7692

packages/schematics/angular/component/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
9292
}
9393

9494

95-
function buildSelector(options: ComponentOptions) {
95+
function buildSelector(options: ComponentOptions, projectPrefix: string) {
9696
let selector = strings.dasherize(options.name);
9797
if (options.prefix) {
9898
selector = `${options.prefix}-${selector}`;
99+
} else if (projectPrefix) {
100+
selector = `${projectPrefix}-${selector}`;
99101
}
100102

101103
return selector;
@@ -119,7 +121,7 @@ export default function(options: ComponentOptions): Rule {
119121
const parsedPath = parseName(options.path, options.name);
120122
options.name = parsedPath.name;
121123
options.path = parsedPath.path;
122-
options.selector = options.selector || buildSelector(options);
124+
options.selector = options.selector || buildSelector(options, project.prefix);
123125

124126
validateName(options.name);
125127
validateHtmlSelector(options.selector);

packages/schematics/angular/component/index_spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('Component Schematic', () => {
2828
spec: true,
2929
module: undefined,
3030
export: false,
31-
prefix: 'app',
3231
};
3332

3433

@@ -197,12 +196,12 @@ describe('Component Schematic', () => {
197196
expect(content).toMatch(/selector: 'pre-foo'/);
198197
});
199198

200-
it('should not use a prefix if none is passed', () => {
199+
it('should use the default project prefix if none is passed', () => {
201200
const options = { ...defaultOptions, prefix: undefined };
202201

203202
const tree = schematicRunner.runSchematic('component', options, appTree);
204203
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
205-
expect(content).toMatch(/selector: 'foo'/);
204+
expect(content).toMatch(/selector: 'app-foo'/);
206205
});
207206

208207
it('should respect the inlineTemplate option', () => {

packages/schematics/angular/directive/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ function addDeclarationToNgModule(options: DirectiveOptions): Rule {
9090
}
9191

9292

93-
function buildSelector(options: DirectiveOptions) {
93+
function buildSelector(options: DirectiveOptions, projectPrefix: string) {
9494
let selector = options.name;
9595
if (options.prefix) {
9696
selector = `${options.prefix}-${selector}`;
97+
} else if (projectPrefix) {
98+
selector = `${projectPrefix}-${selector}`;
9799
}
98100

99101
return strings.camelize(selector);
@@ -116,7 +118,7 @@ export default function (options: DirectiveOptions): Rule {
116118
const parsedPath = parseName(options.path, options.name);
117119
options.name = parsedPath.name;
118120
options.path = parsedPath.path;
119-
options.selector = options.selector || buildSelector(options);
121+
options.selector = options.selector || buildSelector(options, project.prefix);
120122

121123
validateHtmlSelector(options.selector);
122124

packages/schematics/angular/directive/index_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,20 @@ describe('Directive Schematic', () => {
129129
const content = appTree.readContent('/projects/bar/src/app/sub/test.directive.ts');
130130
expect(content).toMatch(/selector: '\[appTest\]'/);
131131
});
132+
133+
it('should use the prefix', () => {
134+
const options = { ...defaultOptions, prefix: 'pre' };
135+
const tree = schematicRunner.runSchematic('directive', options, appTree);
136+
137+
const content = tree.readContent('/projects/bar/src/app/foo.directive.ts');
138+
expect(content).toMatch(/selector: '\[preFoo\]'/);
139+
});
140+
141+
it('should use the default project prefix if none is passed', () => {
142+
const options = { ...defaultOptions, prefix: undefined };
143+
const tree = schematicRunner.runSchematic('directive', options, appTree);
144+
145+
const content = tree.readContent('/projects/bar/src/app/foo.directive.ts');
146+
expect(content).toMatch(/selector: '\[appFoo\]'/);
147+
});
132148
});

packages/schematics/angular/library/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ function addAppToWorkspaceFile(options: LibraryOptions, workspace: WorkspaceSche
127127
const project: any = {
128128
root: `${projectRoot}`,
129129
projectType: 'library',
130+
prefix: options.prefix || 'lib',
130131
architect: {
131132
build: {
132133
builder: '@angular-devkit/build-ng-packagr:build',

0 commit comments

Comments
 (0)