Skip to content

Commit f31d65d

Browse files
committed
feat(cli): use a 'ModulePrefix' constant to define components and directives prefix
This is not the final commit ! We still need to find an agreement on which solution to use between exported constants and comments, then, if the first one is choosed, move the associated code to utilities (see TODOs) and make the associated UTs. see angular#3452
1 parent bafeb16 commit f31d65d

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const getFiles = Blueprint.prototype.files;
77
const stringUtils = require('ember-cli-string-utils');
88
const astUtils = require('../../utilities/ast-utils');
99
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
10+
const ts = require('typescript');
1011

1112
module.exports = {
1213
description: '',
@@ -37,14 +38,29 @@ module.exports = {
3738

3839
this.dynamicPath = parsedPath;
3940

41+
var modulePrefix = '';
42+
43+
// TODO : make it generic and move it to utilities
44+
try {
45+
let pathToModule = findParentModule(this.project, this.dynamicPath.dir);
46+
astUtils.getSourceNodes(astUtils.getSource(pathToModule))
47+
.last(node => (node.flags & ts.NodeFlags.Export) !== 0 && node.getText().indexOf('ModulePrefix') > -1)
48+
.subscribe(node => {
49+
modulePrefix = /= ?['"]([\w-]+)["']/.exec(node.getText())[1];
50+
});
51+
} catch(e) {
52+
console.log(`there isn't any module for this component\n\t`);
53+
}
54+
4055
var defaultPrefix = '';
4156
if (this.project.ngConfig &&
4257
this.project.ngConfig.apps[0] &&
4358
this.project.ngConfig.apps[0].prefix) {
4459
defaultPrefix = this.project.ngConfig.apps[0].prefix;
4560
}
4661

47-
var prefix = (this.options.prefix === 'false' || this.options.prefix === '') ? '' : (this.options.prefix || defaultPrefix);
62+
var prefix = (this.options.prefix === 'false' || this.options.prefix === '')
63+
? '' : (this.options.prefix || modulePrefix || defaultPrefix);
4864
prefix = prefix && `${prefix}-`;
4965

5066
this.selector = stringUtils.dasherize(prefix + parsedPath.name);

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const findParentModule = require('../../utilities/find-parent-module').default;
66
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
77
const Blueprint = require('../../ember-cli/lib/models/blueprint');
88
const getFiles = Blueprint.prototype.files;
9+
const ts = require('typescript');
10+
911

1012
module.exports = {
1113
description: '',
@@ -32,14 +34,29 @@ module.exports = {
3234

3335
this.dynamicPath = parsedPath;
3436

37+
var modulePrefix = '';
38+
39+
// TODO : make it generic and move it to utilities
40+
try {
41+
let pathToModule = findParentModule(this.project, this.dynamicPath.dir);
42+
astUtils.getSourceNodes(astUtils.getSource(pathToModule))
43+
.last(node => (node.flags & ts.NodeFlags.Export) !== 0 && node.getText().indexOf('ModulePrefix') > -1)
44+
.subscribe(node => {
45+
modulePrefix = /= ?['"]([\w-]+)["']/.exec(node.getText())[1];
46+
});
47+
} catch(e) {
48+
console.log(`there isn't any module for this directive\n\t`);
49+
}
50+
3551
var defaultPrefix = '';
3652
if (this.project.ngConfig &&
3753
this.project.ngConfig.apps[0] &&
3854
this.project.ngConfig.apps[0].prefix) {
3955
defaultPrefix = this.project.ngConfig.apps[0].prefix;
4056
}
4157

42-
var prefix = (this.options.prefix === 'false' || this.options.prefix === '') ? '' : (this.options.prefix || defaultPrefix);
58+
var prefix = (this.options.prefix === 'false' || this.options.prefix === '')
59+
? '' : (this.options.prefix || modulePrefix || defaultPrefix);
4360
prefix = prefix && `${prefix}-`;
4461

4562

0 commit comments

Comments
 (0)