Skip to content

Commit

Permalink
refactor: do not prepend selector prefix if already supplied in name
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Jan 22, 2020
1 parent 0a2059f commit 904872a
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"prebuild": "npm ci && npm run generate:schemas && npm test",
"build": "tsc",
"postbuild": "rimraf node_modules",
"test": "jest",
"test": "jest --ci -i",
"generate:schemas": "node scripts/generate-schema-defs.js"
},
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion schematics/src/cms-component/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function createCMSComponent(options) {
if (!options.noCMSPrefixing) {
options.artifactName = 'CMS' + options.artifactName.replace('Cms', '');
}
options = common_1.generateSelector('component', host, options);
options = common_1.generateSelector(host, options);
options.module = 'shared/shared.module';
options = common_1.findDeclaringModule(host, options);
const operations = [];
Expand Down
2 changes: 1 addition & 1 deletion schematics/src/cms-component/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function createCMSComponent(options: Options): Rule {
if (!options.noCMSPrefixing) {
options.artifactName = 'CMS' + options.artifactName.replace('Cms', '');
}
options = generateSelector('component', host, options);
options = generateSelector(host, options);
options.module = 'shared/shared.module';
options = findDeclaringModule(host, options);

Expand Down
2 changes: 1 addition & 1 deletion schematics/src/component/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createComponent(options) {
// tslint:disable:no-parameter-reassignment
options = common_1.applyNameAndPath('component', host, options);
options = common_1.determineArtifactName('component', host, options);
options = common_1.generateSelector('component', host, options);
options = common_1.generateSelector(host, options);
options = common_1.findDeclaringModule(host, options);
const operations = [];
if (!options.skipImport) {
Expand Down
2 changes: 1 addition & 1 deletion schematics/src/component/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createComponent(options: Options): Rule {
// tslint:disable:no-parameter-reassignment
options = applyNameAndPath('component', host, options);
options = determineArtifactName('component', host, options);
options = generateSelector('component', host, options);
options = generateSelector(host, options);
options = findDeclaringModule(host, options);

const operations = [];
Expand Down
7 changes: 7 additions & 0 deletions schematics/src/component/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ describe('Component Schematic', () => {
expect(content).toMatch(/selector: 'ish-test'/);
});

it('should create include selector prefix in name but not duplicate in selector if requested', async () => {
const options = { ...defaultOptions, name: 'sub/ish-test' };
appTree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const content = appTree.readContent('/projects/bar/src/app/sub/ish-test/ish-test.component.ts');
expect(content).toMatch(/selector: 'ish-test'/);
});

it('should respect the sourceRoot value', async () => {
const config = JSON.parse(appTree.readContent('/angular.json'));
config.projects.bar.sourceRoot = 'projects/bar/custom';
Expand Down
4 changes: 2 additions & 2 deletions schematics/src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ function findDeclaringModule(host, options) {
return Object.assign({}, options, { module });
}
exports.findDeclaringModule = findDeclaringModule;
function generateSelector(artifact, host, options) {
function generateSelector(host, options) {
const project = project_1.getProject(host, options.project);
const selector = options.selector || selector_1.buildSelector(artifact, options, project.prefix);
const selector = options.selector || selector_1.buildSelector(options, project.prefix);
validation_1.validateHtmlSelector(selector);
return Object.assign({}, options, { selector });
}
Expand Down
3 changes: 1 addition & 2 deletions schematics/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ export function findDeclaringModule(host: Tree, options: { name?: string }) {
}

export function generateSelector(
artifact: string,
host: Tree,
options: { project?: string; selector?: string; name?: string; prefix?: string }
) {
const project = getProject(host, options.project);
const selector = options.selector || buildSelector(artifact, options, project.prefix);
const selector = options.selector || buildSelector(options, project.prefix);
validateHtmlSelector(selector);

return {
Expand Down
16 changes: 7 additions & 9 deletions schematics/src/utils/selector.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
function buildSelector(artifact, options, projectPrefix) {
let selector = core_1.strings.dasherize(options.name);
if (options.prefix) {
selector = `${options.prefix}-${selector}`;
function buildSelector(options, projectPrefix) {
const selector = core_1.strings.dasherize(options.name);
if (options.prefix === '') {
return selector;
}
else if (options.prefix === undefined && projectPrefix) {
selector = `${projectPrefix}-${selector}`;
}
if (artifact === 'container') {
selector += '-container';
const prefix = options.prefix || projectPrefix;
if (!selector.startsWith(`${prefix}-`)) {
return `${prefix}-${selector}`;
}
return selector;
}
Expand Down
16 changes: 8 additions & 8 deletions schematics/src/utils/selector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { strings } from '@angular-devkit/core';

export function buildSelector(artifact: string, options: { name?: string; prefix?: string }, projectPrefix: string) {
let selector = strings.dasherize(options.name);
if (options.prefix) {
selector = `${options.prefix}-${selector}`;
} else if (options.prefix === undefined && projectPrefix) {
selector = `${projectPrefix}-${selector}`;
export function buildSelector(options: { name?: string; prefix?: string }, projectPrefix: string) {
const selector = strings.dasherize(options.name);
if (options.prefix === '') {
return selector;
}

if (artifact === 'container') {
selector += '-container';
const prefix = options.prefix || projectPrefix;

if (!selector.startsWith(`${prefix}-`)) {
return `${prefix}-${selector}`;
}

return selector;
Expand Down

0 comments on commit 904872a

Please sign in to comment.