Skip to content

Commit

Permalink
feat: ngmodules and insert components based on the AST (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl authored Aug 10, 2016
1 parent 4fd8e9c commit 5bcb7be
Show file tree
Hide file tree
Showing 15 changed files with 558 additions and 213 deletions.
27 changes: 11 additions & 16 deletions addon/ng2/blueprints/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,23 @@ module.exports = {
return;
}

var returns = [];
var modulePath = path.resolve(process.env.PWD, this.dynamicPath.appRoot, 'app.module.ts');
var classifiedName =
stringUtils.classify(`${options.entity.name}-${options.originBlueprintName}`);
var importPath = `'./${options.entity.name}/` +
stringUtils.dasherize(`${options.entity.name}.component';`);
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.flat) {
returns.push(function() {
return addBarrelRegistration(this, this.generatePath)
});
returns.push(addBarrelRegistration(this, componentDir));
} else {
returns.push(function() {
return addBarrelRegistration(
this,
this.generatePath,
options.entity.name + '.component')
});
returns.push(addBarrelRegistration(this, componentDir, fileName));
}

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

return Promise.all(returns);
Expand Down
30 changes: 15 additions & 15 deletions addon/ng2/blueprints/directive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ module.exports = {
},

afterInstall: function(options) {
var returns = [];
var modulePath = path.resolve(process.env.PWD, this.dynamicPath.appRoot, 'app.module.ts');
var classifiedName =
stringUtils.classify(options.entity.name);
var importPath = '\'./' + stringUtils.dasherize(`${options.entity.name}.directive';`);
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}`);
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.flat) {
returns.push(function() {
return addBarrelRegistration(this, this.generatePath)
});
returns.push(addBarrelRegistration(this, componentDir));
} else {
returns.push(function() {
return addBarrelRegistration(
this,
this.generatePath,
options.entity.name + '.directive')
});
returns.push(addBarrelRegistration(this, componentDir, fileName));
}

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

return Promise.all(returns);
Expand Down
9 changes: 4 additions & 5 deletions addon/ng2/blueprints/ng2/files/__path__/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';<% if (isMobile) { %>
import { AppShellModule } from '../app-shell-module';<% } %>
import { AppComponent } from './app.component';

@NgModule({
declarations: [
Expand All @@ -12,12 +11,12 @@ import { AppShellModule } from '../app-shell-module';<% } %>
imports: [
BrowserModule,
CommonModule,
FormsModule<% if (isMobile) { %>,
AppShellModule<% } %>
FormsModule
],
providers: [],
entryComponents: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {

}
}
16 changes: 8 additions & 8 deletions addon/ng2/blueprints/ng2/files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
},
"private": true,
"dependencies": {
"@angular/common": "github:angular/common-builds",
"@angular/compiler": "github:angular/compiler-builds",
"@angular/core": "github:angular/core-builds",
"@angular/forms": "github:angular/forms-builds",
"@angular/http": "github:angular/http-builds",
"@angular/platform-browser": "github:angular/platform-browser-builds",
"@angular/platform-browser-dynamic": "github:angular/platform-browser-dynamic-builds",
"@angular/router": "github:angular/router-builds",
"@angular/common": "2.0.0-rc.5",
"@angular/compiler": "2.0.0-rc.5",
"@angular/core": "2.0.0-rc.5",
"@angular/forms": "0.3.0",
"@angular/http": "2.0.0-rc.5",
"@angular/platform-browser": "2.0.0-rc.5",
"@angular/platform-browser-dynamic": "2.0.0-rc.5",
"@angular/router": "3.0.0-rc.1",
"core-js": "^2.4.0",
"reflect-metadata": "0.1.3",
"rxjs": "5.0.0-beta.6",
Expand Down
30 changes: 15 additions & 15 deletions addon/ng2/blueprints/pipe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ module.exports = {
},

afterInstall: function(options) {
var returns = [];
var modulePath = path.resolve(process.env.PWD, this.dynamicPath.appRoot, 'app.module.ts');
var classifiedName =
stringUtils.classify(`${options.entity.name}-${options.originBlueprintName}`);
var importPath = '\'./' + stringUtils.dasherize(`${options.entity.name}.pipe';`);
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.flat) {
returns.push(function() {
return addBarrelRegistration(this, this.generatePath)
});
returns.push(addBarrelRegistration(this, componentDir));
} else {
returns.push(function() {
return addBarrelRegistration(
this,
this.generatePath,
options.entity.name + '.pipe')
});
returns.push(addBarrelRegistration(this, componentDir, fileName));
}

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

return Promise.all(returns);
Expand Down
23 changes: 12 additions & 11 deletions addon/ng2/blueprints/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ var Blueprint = require('ember-cli/lib/models/blueprint');
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
var addBarrelRegistration = require('../../utilities/barrel-management');
var getFiles = Blueprint.prototype.files;
const stringUtils = require('ember-cli-string-utils');

module.exports = {
description: '',

availableOptions: [
{ name: 'flat', type: Boolean, default: true }
],
Expand All @@ -24,10 +25,10 @@ module.exports = {
flat: options.flat
};
},

files: function() {
var fileList = getFiles.call(this);

if (this.options && this.options.flat) {
fileList = fileList.filter(p => p.indexOf('index.ts') <= 0);
}
Expand All @@ -48,17 +49,17 @@ module.exports = {
}
};
},

afterInstall: function(options) {
const returns = [];
const fileName = stringUtils.dasherize(`${options.entity.name}.service`);

if (!options.flat) {
return addBarrelRegistration(
this,
this.generatePath);
returns.push(addBarrelRegistration(this, this.generatePath));
} else {
return addBarrelRegistration(
this,
this.generatePath,
options.entity.name + '.service');
returns.push(addBarrelRegistration(this, this.generatePath, fileName));
}

return Promise.all(returns);
}
};
Loading

0 comments on commit 5bcb7be

Please sign in to comment.