Skip to content

Commit

Permalink
refactor(utilities): add a getConstConfig utility
Browse files Browse the repository at this point in the history
getConstConfig permit to get an exported const string value from a module.ts file

see angular#3452
  • Loading branch information
nlm-pro committed Dec 14, 2016
1 parent f31d65d commit e9f88fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
16 changes: 2 additions & 14 deletions packages/angular-cli/blueprints/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getFiles = Blueprint.prototype.files;
const stringUtils = require('ember-cli-string-utils');
const astUtils = require('../../utilities/ast-utils');
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
const ts = require('typescript');
const getConstConfig = require('../../utilities/get-const-config').default;

module.exports = {
description: '',
Expand Down Expand Up @@ -38,20 +38,8 @@ module.exports = {

this.dynamicPath = parsedPath;

var modulePrefix = '';
var modulePrefix = getConstConfig(this.project, this.dynamicPath.dir, 'ModulePrefix');

// TODO : make it generic and move it to utilities
try {
let pathToModule = findParentModule(this.project, this.dynamicPath.dir);
astUtils.getSourceNodes(astUtils.getSource(pathToModule))
.last(node => (node.flags & ts.NodeFlags.Export) !== 0 && node.getText().indexOf('ModulePrefix') > -1)
.subscribe(node => {
modulePrefix = /= ?['"]([\w-]+)["']/.exec(node.getText())[1];
});
} catch(e) {
console.log(`there isn't any module for this component\n\t`);
}

var defaultPrefix = '';
if (this.project.ngConfig &&
this.project.ngConfig.apps[0] &&
Expand Down
16 changes: 2 additions & 14 deletions packages/angular-cli/blueprints/directive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const findParentModule = require('../../utilities/find-parent-module').default;
const NodeHost = require('@angular-cli/ast-tools').NodeHost;
const Blueprint = require('../../ember-cli/lib/models/blueprint');
const getFiles = Blueprint.prototype.files;
const ts = require('typescript');
const getConstConfig = require('../../utilities/get-const-config').default;


module.exports = {
Expand Down Expand Up @@ -34,19 +34,7 @@ module.exports = {

this.dynamicPath = parsedPath;

var modulePrefix = '';

// TODO : make it generic and move it to utilities
try {
let pathToModule = findParentModule(this.project, this.dynamicPath.dir);
astUtils.getSourceNodes(astUtils.getSource(pathToModule))
.last(node => (node.flags & ts.NodeFlags.Export) !== 0 && node.getText().indexOf('ModulePrefix') > -1)
.subscribe(node => {
modulePrefix = /= ?['"]([\w-]+)["']/.exec(node.getText())[1];
});
} catch(e) {
console.log(`there isn't any module for this directive\n\t`);
}
var modulePrefix = getConstConfig(this.project, this.dynamicPath.dir, 'ModulePrefix');

var defaultPrefix = '';
if (this.project.ngConfig &&
Expand Down
24 changes: 24 additions & 0 deletions packages/angular-cli/utilities/get-const-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import findParentModule from './find-parent-module';
import * as astUtils from './ast-utils';
import * as ts from 'typescript';

export default function getConstConfig(project: any, dir: string, identifier: string): string {
let modulePrefix = '';
try {
let pathToModule = findParentModule(project, dir);
console.log(pathToModule);

astUtils.getSourceNodes(astUtils.getSource(pathToModule))
.last((node: ts.Node) => {
// tslint:disable-next-line:no-bitwise
return (node.flags & ts.NodeFlags.Export) !== 0
&& node.getText().includes(identifier);
})
.subscribe((node: ts.Node) => {
modulePrefix = /= ?['"]([\w-]+)["']/.exec(node.getText())[1];
});
} catch (e) {
console.log(`no const configuration found for ${identifier} in the parent module\n\t`);
}
return modulePrefix;
}

0 comments on commit e9f88fb

Please sign in to comment.