From e9f88fb021c7f2eb9d05d4f6d6cee8c2b1b45755 Mon Sep 17 00:00:00 2001 From: Noel Mace Date: Wed, 14 Dec 2016 18:27:04 +0100 Subject: [PATCH] refactor(utilities): add a getConstConfig utility getConstConfig permit to get an exported const string value from a module.ts file see #3452 --- .../angular-cli/blueprints/component/index.js | 16 ++----------- .../angular-cli/blueprints/directive/index.js | 16 ++----------- .../angular-cli/utilities/get-const-config.ts | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 28 deletions(-) create mode 100644 packages/angular-cli/utilities/get-const-config.ts diff --git a/packages/angular-cli/blueprints/component/index.js b/packages/angular-cli/blueprints/component/index.js index 43d54d59d918..326c8c624e54 100644 --- a/packages/angular-cli/blueprints/component/index.js +++ b/packages/angular-cli/blueprints/component/index.js @@ -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: '', @@ -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] && diff --git a/packages/angular-cli/blueprints/directive/index.js b/packages/angular-cli/blueprints/directive/index.js index 0f0277e4eac1..f01ccb88f2a5 100644 --- a/packages/angular-cli/blueprints/directive/index.js +++ b/packages/angular-cli/blueprints/directive/index.js @@ -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 = { @@ -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 && diff --git a/packages/angular-cli/utilities/get-const-config.ts b/packages/angular-cli/utilities/get-const-config.ts new file mode 100644 index 000000000000..65d34a3a29c2 --- /dev/null +++ b/packages/angular-cli/utilities/get-const-config.ts @@ -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; +}