Skip to content

Commit 875a743

Browse files
committed
feat: auto-prefix selectors based upon configuration
Fixes #539
1 parent 5d34c25 commit 875a743

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

addon/ng2/blueprints/component/files/__path__/__name__.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router'
33

44
@Component({
55
moduleId: __moduleName,
6-
selector: '<%= dasherizedModuleName %>',<% if(inlineTemplate) { %>
6+
selector: '<%= selector %>',<% if(inlineTemplate) { %>
77
template: `
88
<p>
99
<%= dasherizedModuleName %> Works!

addon/ng2/blueprints/component/index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
var path = require('path');
2+
var stringUtils = require('ember-cli-string-utils');
3+
var chalk = require('chalk');
24
var Blueprint = require('ember-cli/lib/models/blueprint');
35
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
46
var addBarrelRegistration = require('../../utilities/barrel-management');
57
var getFiles = Blueprint.prototype.files;
68

7-
function validateName(name) {
8-
if (name.indexOf('-') >= 0) {
9-
return true;
10-
} else if (name === name.toUpperCase()) {
11-
return false;
12-
} else if (name === name.toLowerCase()) {
13-
return false;
14-
}
15-
return true;
16-
}
17-
189
module.exports = {
1910
description: '',
2011

2112
availableOptions: [
2213
{ name: 'flat', type: Boolean, default: false },
2314
{ name: 'route', type: Boolean, default: false },
2415
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] },
25-
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] }
16+
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
17+
{ name: 'prefix', type: Boolean, default: true }
2618
],
2719

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

3123
this.dynamicPath = parsedPath;
3224

33-
if (!validateName(parsedPath.name)) {
34-
throw 'Names must contain a dash either include a dash or multiCase name. (i.e. multiCase -> multi-case)';
25+
var defaultPrefix = '';
26+
if (this.project.ngConfig &&
27+
this.project.ngConfig.defaults &&
28+
this.project.ngConfig.defaults.prefix) {
29+
defaultPrefix = this.project.ngConfig.defaults.prefix + '-';
30+
}
31+
var prefix = this.options.prefix ? defaultPrefix : '';
32+
this.selector = stringUtils.dasherize(prefix + parsedPath.name);
33+
34+
if (this.selector.indexOf('-') === -1) {
35+
this._writeStatusToUI(chalk.yellow, 'WARNING', 'selectors should contain a dash');
3536
}
3637

3738
return parsedPath.name;
@@ -49,7 +50,8 @@ module.exports = {
4950
route: options.route,
5051
styleExt: this.styleExt,
5152
isLazyRoute: !!options.isLazyRoute,
52-
isAppComponent: !!options.isAppComponent
53+
isAppComponent: !!options.isAppComponent,
54+
selector: this.selector
5355
};
5456
},
5557

addon/ng2/blueprints/ng2/files/angular-cli.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
"karma": {
1818
"config": "karma.conf.js"
1919
}
20+
},
21+
"defaults": {
22+
"prefix": "app"
2023
}
2124
}

addon/ng2/blueprints/route/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ module.exports = {
105105
{ name: 'default', type: Boolean, default: false },
106106
{ name: 'lazy', type: Boolean, default: true },
107107
{ name: 'inline-template', type: Boolean, default: false, aliases: ['it'] },
108-
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] }
108+
{ name: 'inline-style', type: Boolean, default: false, aliases: ['is'] },
109+
{ name: 'prefix', type: Boolean, default: true }
109110
],
110111

111112
beforeInstall: function(options) {

lib/config/schema.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@
7676
}
7777
},
7878
"additionalProperties": false
79+
},
80+
"defaults": {
81+
"type": "object",
82+
"properties": {
83+
"prefix": {
84+
"type": "string"
85+
}
86+
},
87+
"additionalProperties": false
7988
}
8089
},
8190
"additionalProperties": false

tests/acceptance/generate-component.spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,23 @@ describe('Acceptance: ng generate component', function () {
139139
});
140140
});
141141

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

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

@@ -174,4 +180,7 @@ describe('Acceptance: ng generate component', function () {
174180
expect(existsSync(testPath)).to.equal(false);
175181
});
176182
});
183+
184+
it('should ', () => {
185+
});
177186
});

0 commit comments

Comments
 (0)