Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

refactor(@schematics/angular): Add prefix handling #685

Merged
merged 1 commit into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"type": "string",
"description": "Root of the project sourcefiles."
},
"prefix": {
"type": "string",
"description": "The prefix to apply to generated selectors."
},
"cli": {
"$ref": "#/definitions/tool",
"default": {}
Expand Down Expand Up @@ -91,4 +95,4 @@
"additionalProperties": true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export interface Project {
* Root of the project sourcefiles.
*/
root: string;
/**
* The prefix to apply to generated selectors."
*/
prefix: string;
/**
* Tool options.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Workspace', () => {
app: {
root: 'projects/app',
projectType: 'application',
prefix: 'app',
cli: {},
schematics: {
'@schematics/angular': {
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/angular/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function addAppToWorkspaceFile(options: ApplicationOptions, workspace: Workspace
const project: any = {
root: projectRoot,
projectType: 'application',
prefix: options.prefix || 'app',
architect: {
build: {
builder: '@angular-devkit/build-angular:browser',
Expand Down
16 changes: 16 additions & 0 deletions packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ describe('Application Schematic', () => {
expect(workspace.projects.foo).toBeDefined();
});

it('should set the prefix to app if none is set', () => {
const options = { ...defaultOptions };

const tree = schematicRunner.runSchematic('application', options, workspaceTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('app');
});

it('should set the prefix correctly', () => {
const options = { ...defaultOptions, prefix: 'pre' };

const tree = schematicRunner.runSchematic('application', options, workspaceTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('pre');
});

it('should handle the routing flag', () => {
const options = { ...defaultOptions, routing: true };

Expand Down
6 changes: 4 additions & 2 deletions packages/schematics/angular/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
}


function buildSelector(options: ComponentOptions) {
function buildSelector(options: ComponentOptions, projectPrefix: string) {
let selector = strings.dasherize(options.name);
if (options.prefix) {
selector = `${options.prefix}-${selector}`;
} else if (projectPrefix) {
selector = `${projectPrefix}-${selector}`;
}

return selector;
Expand All @@ -119,7 +121,7 @@ export default function(options: ComponentOptions): Rule {
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
options.selector = options.selector || buildSelector(options);
options.selector = options.selector || buildSelector(options, project.prefix);

validateName(options.name);
validateHtmlSelector(options.selector);
Expand Down
5 changes: 2 additions & 3 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('Component Schematic', () => {
spec: true,
module: undefined,
export: false,
prefix: 'app',
};


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

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

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

it('should respect the inlineTemplate option', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/schematics/angular/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ function addDeclarationToNgModule(options: DirectiveOptions): Rule {
}


function buildSelector(options: DirectiveOptions) {
function buildSelector(options: DirectiveOptions, projectPrefix: string) {
let selector = options.name;
if (options.prefix) {
selector = `${options.prefix}-${selector}`;
} else if (projectPrefix) {
selector = `${projectPrefix}-${selector}`;
}

return strings.camelize(selector);
Expand All @@ -116,7 +118,7 @@ export default function (options: DirectiveOptions): Rule {
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
options.selector = options.selector || buildSelector(options);
options.selector = options.selector || buildSelector(options, project.prefix);

validateHtmlSelector(options.selector);

Expand Down
16 changes: 16 additions & 0 deletions packages/schematics/angular/directive/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,20 @@ describe('Directive Schematic', () => {
const content = appTree.readContent('/projects/bar/src/app/sub/test.directive.ts');
expect(content).toMatch(/selector: '\[appTest\]'/);
});

it('should use the prefix', () => {
const options = { ...defaultOptions, prefix: 'pre' };
const tree = schematicRunner.runSchematic('directive', options, appTree);

const content = tree.readContent('/projects/bar/src/app/foo.directive.ts');
expect(content).toMatch(/selector: '\[preFoo\]'/);
});

it('should use the default project prefix if none is passed', () => {
const options = { ...defaultOptions, prefix: undefined };
const tree = schematicRunner.runSchematic('directive', options, appTree);

const content = tree.readContent('/projects/bar/src/app/foo.directive.ts');
expect(content).toMatch(/selector: '\[appFoo\]'/);
});
});
1 change: 1 addition & 0 deletions packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function addAppToWorkspaceFile(options: LibraryOptions, workspace: WorkspaceSche
const project: any = {
root: `${projectRoot}`,
projectType: 'library',
prefix: options.prefix || 'lib',
architect: {
build: {
builder: '@angular-devkit/build-ng-packagr:build',
Expand Down
15 changes: 15 additions & 0 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ describe('Library Schematic', () => {
expect(workspace.projects.foo).toBeDefined();
});

it('should set the prefix to lib if none is set', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);

const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('lib');
});

it('should set the prefix correctly', () => {
const options = { ...defaultOptions, prefix: 'pre' };
const tree = schematicRunner.runSchematic('application', options, workspaceTree);

const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('pre');
});

it('should handle a pascalCasedName', () => {
const options = {...defaultOptions, name: 'pascalCasedName'};
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/angular/ng-new/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function (options: NgNewOptions): Rule {
name: options.name,
inlineStyle: options.inlineStyle,
inlineTemplate: options.inlineTemplate,
prefix: options.prefix,
viewEncapsulation: options.viewEncapsulation,
routing: options.routing,
style: options.style,
Expand Down
8 changes: 8 additions & 0 deletions packages/schematics/angular/ng-new/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ describe('Ng New Schematic', () => {
expect(files.indexOf('/bar/src/main.ts')).toBeGreaterThanOrEqual(0);
expect(files.indexOf('/bar/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
});

it('should should set the prefix in angular.json and in app.component.ts', () => {
const options = { ...defaultOptions, prefix: 'pre' };

const tree = schematicRunner.runSchematic('ng-new', options);
const content = tree.readContent('/bar/angular.json');
expect(content).toMatch(/"prefix": "pre"/);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"app": {
"root": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
Expand Down