Skip to content

Commit

Permalink
feat(module): select module to add generations to for declaration
Browse files Browse the repository at this point in the history
Note: this also displays a warning stating that services are not provided by default
  • Loading branch information
Brocco committed Sep 4, 2016
1 parent f286c1c commit 1f7a7e6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 23 deletions.
12 changes: 10 additions & 2 deletions addon/ng2/blueprints/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);

Expand Down Expand Up @@ -100,15 +109,14 @@ 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);
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;

if (!options['skip-import']) {
returns.push(
astUtils.addComponentToModule(modulePath, className, importPath)
astUtils.addComponentToModule(this.pathToModule, className, importPath)
.then(change => change.apply()));
}

Expand Down
14 changes: 11 additions & 3 deletions addon/ng2/blueprints/directive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -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] &&
Expand Down Expand Up @@ -56,15 +65,14 @@ 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);
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;

if (!options['skip-import']) {
returns.push(
astUtils.addComponentToModule(modulePath, className, importPath)
astUtils.addComponentToModule(this.pathToModule, className, importPath)
.then(change => change.apply()));
}

Expand Down
9 changes: 2 additions & 7 deletions addon/ng2/blueprints/module/files/__path__/__name__.module.ts
Original file line number Diff line number Diff line change
@@ -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 { }
12 changes: 6 additions & 6 deletions addon/ng2/blueprints/module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -19,7 +19,7 @@ module.exports = {
},

locals: function (options) {
return {
return {
dynamicPath: this.dynamicPath.dir,
spec: options.spec
};
Expand All @@ -40,17 +40,17 @@ 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;
}
};
},

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;
Expand Down
18 changes: 13 additions & 5 deletions addon/ng2/blueprints/pipe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -18,7 +27,7 @@ module.exports = {
},

locals: function (options) {
return {
return {
dynamicPath: this.dynamicPath.dir,
flat: options.flat
};
Expand All @@ -37,22 +46,21 @@ 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);
const importPath = componentDir ? `./${componentDir}/${fileName}` : `./${fileName}`;

if (!options['skip-import']) {
returns.push(
astUtils.addComponentToModule(modulePath, className, importPath)
astUtils.addComponentToModule(this.pathToModule, className, importPath)
.then(change => change.apply()));
}

Expand Down
6 changes: 6 additions & 0 deletions addon/ng2/blueprints/service/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var path = require('path');
const chalk = require('chalk');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');

module.exports = {
Expand Down Expand Up @@ -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);
}
};
35 changes: 35 additions & 0 deletions addon/ng2/utilities/find-parent-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as fs from 'fs';
import * as path from 'path';
const SilentError = require('silent-error');

export 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(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.resolve(path.join(pathToCheck, '..'));
}

throw new SilentError('No module files found');
}

0 comments on commit 1f7a7e6

Please sign in to comment.