From fd52c2090e366cd0610cf9ec99d19cd875ab31f8 Mon Sep 17 00:00:00 2001 From: Mike Brocchi Date: Mon, 29 Aug 2016 22:56:20 -0400 Subject: [PATCH] feat(module): select module to add generations to for declaration Note: this also displays a warning stating that services are not provided by default --- addon/ng2/blueprints/component/index.js | 12 +++++-- addon/ng2/blueprints/directive/index.js | 14 ++++++-- .../module/files/__path__/__name__.module.ts | 9 ++--- addon/ng2/blueprints/module/index.js | 12 +++---- addon/ng2/blueprints/pipe/index.js | 18 +++++++--- addon/ng2/blueprints/service/index.js | 6 ++++ addon/ng2/utilities/find-parent-module.ts | 33 +++++++++++++++++++ 7 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 addon/ng2/utilities/find-parent-module.ts diff --git a/addon/ng2/blueprints/component/index.js b/addon/ng2/blueprints/component/index.js index bed33cd23da4..7987f8f13c18 100644 --- a/addon/ng2/blueprints/component/index.js +++ b/addon/ng2/blueprints/component/index.js @@ -2,6 +2,7 @@ var path = require('path'); var chalk = require('chalk'); var Blueprint = require('ember-cli/lib/models/blueprint'); var dynamicPathParser = require('../../utilities/dynamic-path-parser'); +const findParentModule = require('../../utilities/find-parent-module'); var getFiles = Blueprint.prototype.files; const stringUtils = require('ember-cli-string-utils'); const astUtils = require('../../utilities/ast-utils'); @@ -17,6 +18,14 @@ module.exports = { { name: 'spec', type: Boolean, default: true } ], + beforeInstall: function() { + try { + this.pathToModule = findParentModule(this.project, this.dynamicPath.dir); + } catch(e) { + throw `Error locating module for declaration\n\t${e}`; + } + }, + normalizeEntityName: function (entityName) { var parsedPath = dynamicPathParser(this.project, entityName); @@ -100,7 +109,6 @@ module.exports = { } const returns = []; - const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts'); const className = stringUtils.classify(`${options.entity.name}Component`); const fileName = stringUtils.dasherize(`${options.entity.name}.component`); const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath); @@ -108,7 +116,7 @@ module.exports = { if (!options['skip-import']) { returns.push( - astUtils.addComponentToModule(modulePath, className, importPath) + astUtils.addComponentToModule(this.pathToModule, className, importPath) .then(change => change.apply())); } diff --git a/addon/ng2/blueprints/directive/index.js b/addon/ng2/blueprints/directive/index.js index 6b19947731d1..38336aef2b3c 100644 --- a/addon/ng2/blueprints/directive/index.js +++ b/addon/ng2/blueprints/directive/index.js @@ -2,6 +2,7 @@ var path = require('path'); var dynamicPathParser = require('../../utilities/dynamic-path-parser'); const stringUtils = require('ember-cli-string-utils'); const astUtils = require('../../utilities/ast-utils'); +const findParentModule = require('../../utilities/find-parent-module'); module.exports = { description: '', @@ -11,11 +12,19 @@ module.exports = { { name: 'prefix', type: Boolean, default: true } ], + beforeInstall: function() { + try { + this.pathToModule = findParentModule(this.project, this.dynamicPath.dir); + } catch(e) { + throw `Error locating module for declaration\n\t${e}`; + } + }, + normalizeEntityName: function (entityName) { var parsedPath = dynamicPathParser(this.project, entityName); this.dynamicPath = parsedPath; - + var defaultPrefix = ''; if (this.project.ngConfig && this.project.ngConfig.apps[0] && @@ -56,7 +65,6 @@ module.exports = { } const returns = []; - const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts'); const className = stringUtils.classify(`${options.entity.name}Directive`); const fileName = stringUtils.dasherize(`${options.entity.name}.directive`); const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath); @@ -64,7 +72,7 @@ module.exports = { if (!options['skip-import']) { returns.push( - astUtils.addComponentToModule(modulePath, className, importPath) + astUtils.addComponentToModule(this.pathToModule, className, importPath) .then(change => change.apply())); } diff --git a/addon/ng2/blueprints/module/files/__path__/__name__.module.ts b/addon/ng2/blueprints/module/files/__path__/__name__.module.ts index 822b4c57379b..dbd299520ab7 100644 --- a/addon/ng2/blueprints/module/files/__path__/__name__.module.ts +++ b/addon/ng2/blueprints/module/files/__path__/__name__.module.ts @@ -1,15 +1,10 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { routing } from './<%= dasherizedModuleName %>.routes'; -import { <%= classifiedModuleName %>Component } from './<%= dasherizedModuleName %>.component'; @NgModule({ imports: [ - CommonModule, - routing + CommonModule ], - declarations: [ - <%= classifiedModuleName %>Component - ] + declarations: [] }) export class <%= classifiedModuleName %>Module { } diff --git a/addon/ng2/blueprints/module/index.js b/addon/ng2/blueprints/module/index.js index 8d636dd1638b..f8285ab748d7 100644 --- a/addon/ng2/blueprints/module/index.js +++ b/addon/ng2/blueprints/module/index.js @@ -9,7 +9,7 @@ module.exports = { availableOptions: [ { name: 'spec', type: Boolean, default: false } ], - + normalizeEntityName: function (entityName) { this.entityName = entityName; var parsedPath = dynamicPathParser(this.project, entityName); @@ -19,7 +19,7 @@ module.exports = { }, locals: function (options) { - return { + return { dynamicPath: this.dynamicPath.dir, spec: options.spec }; @@ -40,8 +40,8 @@ module.exports = { this.dasherizedModuleName = options.dasherizedModuleName; return { __path__: () => { - this.generatePath = this.dynamicPath.dir - + path.sep + this.generatePath = this.dynamicPath.dir + + path.sep + options.dasherizedModuleName; return this.generatePath; } @@ -49,8 +49,8 @@ module.exports = { }, afterInstall: function (options) { - options.entity.name = this.entityName; - options.flat = false; + options.entity.name = path.join(this.entityName, this.dasherizedModuleName); + options.flat = true; options.route = false; options.inlineTemplate = false; options.inlineStyle = false; diff --git a/addon/ng2/blueprints/pipe/index.js b/addon/ng2/blueprints/pipe/index.js index d6fe6ce7f2b7..b420a21c3736 100644 --- a/addon/ng2/blueprints/pipe/index.js +++ b/addon/ng2/blueprints/pipe/index.js @@ -2,14 +2,23 @@ var path = require('path'); var dynamicPathParser = require('../../utilities/dynamic-path-parser'); const stringUtils = require('ember-cli-string-utils'); const astUtils = require('../../utilities/ast-utils'); +const findParentModule = require('../../utilities/find-parent-module'); module.exports = { description: '', - + availableOptions: [ { name: 'flat', type: Boolean, default: true } ], + beforeInstall: function() { + try { + this.pathToModule = findParentModule(this.project, this.dynamicPath.dir); + } catch(e) { + throw `Error locating module for declaration\n\t${e}`; + } + }, + normalizeEntityName: function (entityName) { var parsedPath = dynamicPathParser(this.project, entityName); @@ -18,7 +27,7 @@ module.exports = { }, locals: function (options) { - return { + return { dynamicPath: this.dynamicPath.dir, flat: options.flat }; @@ -37,14 +46,13 @@ module.exports = { } }; }, - + afterInstall: function(options) { if (options.dryRun) { return; } const returns = []; - const modulePath = path.join(this.project.root, this.dynamicPath.appRoot, 'app.module.ts'); const className = stringUtils.classify(`${options.entity.name}Pipe`); const fileName = stringUtils.dasherize(`${options.entity.name}.pipe`); const componentDir = path.relative(this.dynamicPath.appRoot, this.generatePath); @@ -52,7 +60,7 @@ module.exports = { if (!options['skip-import']) { returns.push( - astUtils.addComponentToModule(modulePath, className, importPath) + astUtils.addComponentToModule(this.pathToModule, className, importPath) .then(change => change.apply())); } diff --git a/addon/ng2/blueprints/service/index.js b/addon/ng2/blueprints/service/index.js index d29dae56eb33..ea535fd44d2b 100644 --- a/addon/ng2/blueprints/service/index.js +++ b/addon/ng2/blueprints/service/index.js @@ -1,4 +1,5 @@ var path = require('path'); +const chalk = require('chalk'); var dynamicPathParser = require('../../utilities/dynamic-path-parser'); module.exports = { @@ -34,5 +35,10 @@ module.exports = { return dir; } }; + }, + + afterInstall() { + const warningMessage = 'Service is generated but not provided, it must be provided to be used'; + this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage); } }; diff --git a/addon/ng2/utilities/find-parent-module.ts b/addon/ng2/utilities/find-parent-module.ts new file mode 100644 index 000000000000..bc7ffea25195 --- /dev/null +++ b/addon/ng2/utilities/find-parent-module.ts @@ -0,0 +1,33 @@ +import * as fs from 'fs'; +import * as path from 'path'; +const SilentError = require('silent-error'); + +module.exports = function findParentModule(project: any, currentDir: string): string { + + const sourceRoot = path.join(project.root, project.ngConfig.apps[0].root, 'app'); + + // trim currentDir + currentDir = currentDir.replace(path.join(project.ngConfig.apps[0].root, 'app'), ''); + + let pathToCheck = path.join(sourceRoot, currentDir); + + while (pathToCheck.length >= sourceRoot.length) { + // let files: string[] = fs.readdirSync(pathToCheck); + + // files = files.filter(file => file.indexOf('.module.ts') > 0); + const files = fs.readdirSync(pathToCheck) + .filter(fileName => fileName.endsWith('.module.ts')) + .filter(fileName => fs.statSync(path.join(pathToCheck, fileName)).isFile()); + + if (files.length === 1) { + return path.join(pathToCheck, files[0]); + } else if (files.length > 1) { + throw new SilentError(`Multiple module files found: ${pathToCheck.replace(sourceRoot, '')}`); + } + + // move to parent directory + pathToCheck = path.dirname(pathToCheck); + } + + throw new SilentError('No module files found'); +};