Skip to content

Commit e9f88fb

Browse files
committed
refactor(utilities): add a getConstConfig utility
getConstConfig permit to get an exported const string value from a module.ts file see angular#3452
1 parent f31d65d commit e9f88fb

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +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');
10+
const getConstConfig = require('../../utilities/get-const-config').default;
1111

1212
module.exports = {
1313
description: '',
@@ -38,20 +38,8 @@ module.exports = {
3838

3939
this.dynamicPath = parsedPath;
4040

41-
var modulePrefix = '';
41+
var modulePrefix = getConstConfig(this.project, this.dynamicPath.dir, 'ModulePrefix');
4242

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-
5543
var defaultPrefix = '';
5644
if (this.project.ngConfig &&
5745
this.project.ngConfig.apps[0] &&

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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');
9+
const getConstConfig = require('../../utilities/get-const-config').default;
1010

1111

1212
module.exports = {
@@ -34,19 +34,7 @@ module.exports = {
3434

3535
this.dynamicPath = parsedPath;
3636

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-
}
37+
var modulePrefix = getConstConfig(this.project, this.dynamicPath.dir, 'ModulePrefix');
5038

5139
var defaultPrefix = '';
5240
if (this.project.ngConfig &&
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import findParentModule from './find-parent-module';
2+
import * as astUtils from './ast-utils';
3+
import * as ts from 'typescript';
4+
5+
export default function getConstConfig(project: any, dir: string, identifier: string): string {
6+
let modulePrefix = '';
7+
try {
8+
let pathToModule = findParentModule(project, dir);
9+
console.log(pathToModule);
10+
11+
astUtils.getSourceNodes(astUtils.getSource(pathToModule))
12+
.last((node: ts.Node) => {
13+
// tslint:disable-next-line:no-bitwise
14+
return (node.flags & ts.NodeFlags.Export) !== 0
15+
&& node.getText().includes(identifier);
16+
})
17+
.subscribe((node: ts.Node) => {
18+
modulePrefix = /= ?['"]([\w-]+)["']/.exec(node.getText())[1];
19+
});
20+
} catch (e) {
21+
console.log(`no const configuration found for ${identifier} in the parent module\n\t`);
22+
}
23+
return modulePrefix;
24+
}

0 commit comments

Comments
 (0)