diff --git a/addon/ng2/blueprints/component/files/__path__/__name__.component.ts b/addon/ng2/blueprints/component/files/__path__/__name__.component.ts index ba7b0923eb39..91bd38361814 100644 --- a/addon/ng2/blueprints/component/files/__path__/__name__.component.ts +++ b/addon/ng2/blueprints/component/files/__path__/__name__.component.ts @@ -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: `

<%= dasherizedModuleName %> Works! diff --git a/addon/ng2/blueprints/component/index.js b/addon/ng2/blueprints/component/index.js index 9fa10decfa0b..3abc5ff0b3f5 100644 --- a/addon/ng2/blueprints/component/index.js +++ b/addon/ng2/blueprints/component/index.js @@ -1,20 +1,11 @@ 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: '', @@ -22,7 +13,8 @@ module.exports = { { 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) { @@ -30,8 +22,17 @@ module.exports = { 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; @@ -49,7 +50,8 @@ module.exports = { route: options.route, styleExt: this.styleExt, isLazyRoute: !!options.isLazyRoute, - isAppComponent: !!options.isAppComponent + isAppComponent: !!options.isAppComponent, + selector: this.selector }; }, diff --git a/addon/ng2/blueprints/ng2/files/angular-cli.json b/addon/ng2/blueprints/ng2/files/angular-cli.json index 2c258fabe5bc..858cae336cbd 100644 --- a/addon/ng2/blueprints/ng2/files/angular-cli.json +++ b/addon/ng2/blueprints/ng2/files/angular-cli.json @@ -17,5 +17,8 @@ "karma": { "config": "karma.conf.js" } + }, + "defaults": { + "prefix": "app" } } diff --git a/addon/ng2/blueprints/route/index.js b/addon/ng2/blueprints/route/index.js index 0bf20d16c52d..5e2293062e43 100644 --- a/addon/ng2/blueprints/route/index.js +++ b/addon/ng2/blueprints/route/index.js @@ -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) { diff --git a/lib/config/schema.json b/lib/config/schema.json index 323b5495cd02..06475848071c 100644 --- a/lib/config/schema.json +++ b/lib/config/schema.json @@ -76,6 +76,15 @@ } }, "additionalProperties": false + }, + "defaults": { + "type": "object", + "properties": { + "prefix": { + "type": "string" + } + }, + "additionalProperties": false } }, "additionalProperties": false diff --git a/tests/acceptance/generate-component.spec.js b/tests/acceptance/generate-component.spec.js index d6217bc3a5e6..866606a6b343 100644 --- a/tests/acceptance/generate-component.spec.js +++ b/tests/acceptance/generate-component.spec.js @@ -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); }); }); @@ -174,4 +180,7 @@ describe('Acceptance: ng generate component', function () { expect(existsSync(testPath)).to.equal(false); }); }); + + it('should ', () => { + }); });