Skip to content

Commit 2547b55

Browse files
nlm-proMRHarrison
authored andcommitted
feat(generate): change generate --prefix option type from Boolean to string (angular#3457)
use ``ng g c foo --prefix='bar'`` or ``ng g d foo --prefix='bar'`` in order to use another prefix than the one defined by default in angular-cli.json apps[0].prefix tslint rules "directive-selector" and "component-selector" can accept any array of prefix, and is therefore compatible with this approach this is a temporary solution pending angular#3452 closure
1 parent 37f0028 commit 2547b55

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

packages/angular-cli/blueprints/component/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
{ name: 'flat', type: Boolean, default: false },
1616
{ name: 'inline-template', type: Boolean, aliases: ['it'] },
1717
{ name: 'inline-style', type: Boolean, aliases: ['is'] },
18-
{ name: 'prefix', type: Boolean, default: true },
18+
{ name: 'prefix', type: String, default: null },
1919
{ name: 'spec', type: Boolean },
2020
{ name: 'view-encapsulation', type: String, aliases: ['ve'] },
2121
{ name: 'change-detection', type: String, aliases: ['cd'] },
@@ -41,9 +41,12 @@ module.exports = {
4141
if (this.project.ngConfig &&
4242
this.project.ngConfig.apps[0] &&
4343
this.project.ngConfig.apps[0].prefix) {
44-
defaultPrefix = this.project.ngConfig.apps[0].prefix + '-';
44+
defaultPrefix = this.project.ngConfig.apps[0].prefix;
4545
}
46-
var prefix = this.options.prefix ? defaultPrefix : '';
46+
47+
var prefix = (this.options.prefix === 'false' || this.options.prefix === '') ? '' : (this.options.prefix || defaultPrefix);
48+
prefix = prefix && `${prefix}-`;
49+
4750
this.selector = stringUtils.dasherize(prefix + parsedPath.name);
4851

4952
if (this.selector.indexOf('-') === -1) {

packages/angular-cli/blueprints/directive/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212

1313
availableOptions: [
1414
{ name: 'flat', type: Boolean, default: true },
15-
{ name: 'prefix', type: Boolean, default: true },
15+
{ name: 'prefix', type: String, default: null },
1616
{ name: 'spec', type: Boolean },
1717
{ name: 'skip-import', type: Boolean, default: false }
1818
],
@@ -38,7 +38,10 @@ module.exports = {
3838
this.project.ngConfig.apps[0].prefix) {
3939
defaultPrefix = this.project.ngConfig.apps[0].prefix;
4040
}
41-
var prefix = this.options.prefix ? `${defaultPrefix}-` : '';
41+
42+
var prefix = (this.options.prefix === 'false' || this.options.prefix === '') ? '' : (this.options.prefix || defaultPrefix);
43+
prefix = prefix && `${prefix}-`;
44+
4245

4346
this.selector = stringUtils.camelize(prefix + parsedPath.name);
4447
return parsedPath.name;

packages/angular-cli/blueprints/module/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = {
6868
options.route = false;
6969
options.inlineTemplate = false;
7070
options.inlineStyle = false;
71-
options.prefix = true;
71+
options.prefix = null;
7272
options.spec = true;
7373
return Blueprint.load(path.join(__dirname, '../component')).install(options);
7474
}

tests/acceptance/generate-component.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ describe('Acceptance: ng generate component', function () {
150150
});
151151
});
152152

153+
it('mycomp --prefix= will not prefix selector', () => {
154+
return ng(['generate', 'component', 'mycomp', '--prefix='])
155+
.then(() => {
156+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'mycomp', 'mycomp.component.ts');
157+
expect(existsSync(testPath)).to.equal(true);
158+
var contents = fs.readFileSync(testPath, 'utf8');
159+
expect(contents.indexOf('selector: \'mycomp\'') === -1).to.equal(false);
160+
});
161+
});
162+
163+
it('mycomp --prefix=test will prefix selector with \'test-\'', () => {
164+
return ng(['generate', 'component', 'mycomp', '--prefix=test'])
165+
.then(() => {
166+
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'mycomp', 'mycomp.component.ts');
167+
expect(existsSync(testPath)).to.equal(true);
168+
var contents = fs.readFileSync(testPath, 'utf8');
169+
expect(contents.indexOf('selector: \'test-mycomp\'') === -1).to.equal(false);
170+
});
171+
});
172+
153173
it('myComp will succeed', () => {
154174
return ng(['generate', 'component', 'myComp'])
155175
.then(() => {

0 commit comments

Comments
 (0)