Skip to content

feat: auto-prefix selectors based upon configuration #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2016
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 @@ -3,7 +3,7 @@ import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router'

@Component({
moduleId: __moduleName,
selector: '<%= dasherizedModuleName %>',<% if(inlineTemplate) { %>
selector: '<%= selector %>',<% if(inlineTemplate) { %>
template: `
<p>
<%= dasherizedModuleName %> Works!
Expand Down
32 changes: 17 additions & 15 deletions addon/ng2/blueprints/component/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
var path = require('path');
var stringUtils = require('ember-cli-string-utils');
var chalk = require('chalk');
var Blueprint = require('ember-cli/lib/models/blueprint');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
var addBarrelRegistration = require('../../utilities/barrel-management');
var getFiles = Blueprint.prototype.files;

function validateName(name) {
if (name.indexOf('-') >= 0) {
return true;
} else if (name === name.toUpperCase()) {
return false;
} else if (name === name.toLowerCase()) {
return false;
}
return true;
}

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: false },
{ name: 'route', type: Boolean, default: false },
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] },
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] }
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
{ name: 'prefix', type: Boolean, default: true }
],

normalizeEntityName: function (entityName) {
var parsedPath = dynamicPathParser(this.project, entityName);

this.dynamicPath = parsedPath;

if (!validateName(parsedPath.name)) {
throw 'Names must contain a dash either include a dash or multiCase name. (i.e. multiCase -> multi-case)';
var defaultPrefix = '';
if (this.project.ngConfig &&
this.project.ngConfig.defaults &&
this.project.ngConfig.defaults.prefix) {
defaultPrefix = this.project.ngConfig.defaults.prefix + '-';
}
var prefix = this.options.prefix ? defaultPrefix : '';
this.selector = stringUtils.dasherize(prefix + parsedPath.name);

if (this.selector.indexOf('-') === -1) {
this._writeStatusToUI(chalk.yellow, 'WARNING', 'selectors should contain a dash');
}

return parsedPath.name;
Expand All @@ -49,7 +50,8 @@ module.exports = {
route: options.route,
styleExt: this.styleExt,
isLazyRoute: !!options.isLazyRoute,
isAppComponent: !!options.isAppComponent
isAppComponent: !!options.isAppComponent,
selector: this.selector
};
},

Expand Down
3 changes: 3 additions & 0 deletions addon/ng2/blueprints/ng2/files/angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
"karma": {
"config": "karma.conf.js"
}
},
"defaults": {
"prefix": "app"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update pushed

}
}
3 changes: 2 additions & 1 deletion addon/ng2/blueprints/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ module.exports = {
{ name: 'default', type: Boolean, default: false },
{ name: 'lazy', type: Boolean, default: true },
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] },
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] }
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
{ name: 'prefix', type: Boolean, default: true }
],

beforeInstall: function(options) {
Expand Down
9 changes: 9 additions & 0 deletions lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
}
},
"additionalProperties": false
},
"defaults": {
"type": "object",
"properties": {
"prefix": {
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
Expand Down
23 changes: 16 additions & 7 deletions tests/acceptance/generate-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,23 @@ describe('Acceptance: ng generate component', function () {
});
});

it('ng generate component mycomp will fail: no dashes in name', () => {
it('ng generate component mycomp will prefix selector', () => {
return ng(['generate', 'component', 'mycomp'])
.then((exitCode) => {
expect(exitCode).to.equal(1);
.then(() => {
var testPath = path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'mycomp', 'mycomp.component.ts');
expect(existsSync(testPath)).to.equal(true);
var contents = fs.readFileSync(testPath, 'utf8');
expect(contents.indexOf('selector: \'app-mycomp\'') === -1).to.equal(false);
});
});

it('ng generate component MYCOMP will fail: no dashes in name', () => {
return ng(['generate', 'component', 'MYCOMP'])
.then((exitCode) => {
expect(exitCode).to.equal(1);
it('ng generate component mycomp --no-prefix will not prefix selector', () => {
return ng(['generate', 'component', 'mycomp', '--no-prefix'])
.then(() => {
var testPath = path.join(root, 'tmp', 'foo', 'src', 'client', 'app', 'mycomp', 'mycomp.component.ts');
expect(existsSync(testPath)).to.equal(true);
var contents = fs.readFileSync(testPath, 'utf8');
expect(contents.indexOf('selector: \'mycomp\'') === -1).to.equal(false);
});
});

Expand All @@ -174,4 +180,7 @@ describe('Acceptance: ng generate component', function () {
expect(existsSync(testPath)).to.equal(false);
});
});

it('should ', () => {
});
});