Skip to content

Commit e89d6f4

Browse files
jkurihansl
authored andcommitted
refactor(NgModule): update to RC5 (angular#1579)
Merging this and will clean up manually myself.
1 parent b4f8b7e commit e89d6f4

File tree

8 files changed

+173
-32
lines changed

8 files changed

+173
-32
lines changed

addon/ng2/blueprints/component/index.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var dynamicPathParser = require('../../utilities/dynamic-path-parser');
55
var addBarrelRegistration = require('../../utilities/barrel-management');
66
var getFiles = Blueprint.prototype.files;
77
const stringUtils = require('ember-cli-string-utils');
8+
const astUtils = require('../../utilities/ast-utils');
89

910
module.exports = {
1011
description: '',
@@ -119,13 +120,30 @@ module.exports = {
119120
return;
120121
}
121122

123+
var returns = [];
124+
var modulePath = path.resolve(process.env.PWD, this.dynamicPath.appRoot, 'app.module.ts');
125+
var classifiedName =
126+
stringUtils.classify(`${options.entity.name}-${options.originBlueprintName}`);
127+
var importPath = `'./${options.entity.name}/` +
128+
stringUtils.dasherize(`${options.entity.name}.component';`);
129+
122130
if (!options.flat) {
123-
return addBarrelRegistration(this, this.generatePath);
131+
returns.push(function() {
132+
return addBarrelRegistration(this, this.generatePath)
133+
});
124134
} else {
125-
return addBarrelRegistration(
126-
this,
127-
this.generatePath,
128-
options.entity.name + '.component');
135+
returns.push(function() {
136+
return addBarrelRegistration(
137+
this,
138+
this.generatePath,
139+
options.entity.name + '.component')
140+
});
141+
}
142+
143+
if (!options['skip-import']) {
144+
returns.push(astUtils.importComponent(modulePath, classifiedName, importPath));
129145
}
146+
147+
return Promise.all(returns);
130148
}
131149
};

addon/ng2/blueprints/directive/index.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var Blueprint = require('ember-cli/lib/models/blueprint');
33
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
44
var addBarrelRegistration = require('../../utilities/barrel-management');
55
var getFiles = Blueprint.prototype.files;
6+
const stringUtils = require('ember-cli-string-utils');
7+
const astUtils = require('../../utilities/ast-utils');
68

79
module.exports = {
810
description: '',
@@ -52,15 +54,29 @@ module.exports = {
5254
},
5355

5456
afterInstall: function(options) {
57+
var returns = [];
58+
var modulePath = path.resolve(process.env.PWD, this.dynamicPath.appRoot, 'app.module.ts');
59+
var classifiedName =
60+
stringUtils.classify(options.entity.name);
61+
var importPath = '\'./' + stringUtils.dasherize(`${options.entity.name}.directive';`);
62+
5563
if (!options.flat) {
56-
return addBarrelRegistration(
57-
this,
58-
this.generatePath);
64+
returns.push(function() {
65+
return addBarrelRegistration(this, this.generatePath)
66+
});
5967
} else {
60-
return addBarrelRegistration(
61-
this,
62-
this.generatePath,
63-
options.entity.name + '.directive');
68+
returns.push(function() {
69+
return addBarrelRegistration(
70+
this,
71+
this.generatePath,
72+
options.entity.name + '.directive')
73+
});
74+
}
75+
76+
if (!options['skip-import']) {
77+
returns.push(astUtils.importComponent(modulePath, classifiedName, importPath));
6478
}
79+
80+
return Promise.all(returns);
6581
}
6682
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { BrowserModule } from '@angular/platform-browser';
2+
import { NgModule, ApplicationRef } from '@angular/core';
3+
import { CommonModule } from '@angular/common';
4+
import { FormsModule } from '@angular/forms';
5+
import { AppComponent } from './app.component';<% if (isMobile) { %>
6+
import { AppShellModule } from '../app-shell-module';<% } %>
7+
8+
@NgModule({
9+
declarations: [
10+
AppComponent
11+
],
12+
imports: [
13+
BrowserModule,
14+
CommonModule,
15+
FormsModule<% if (isMobile) { %>,
16+
AppShellModule<% } %>
17+
],
18+
entryComponents: [AppComponent],
19+
bootstrap: [AppComponent]
20+
})
21+
export class AppModule {
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './environments/environment';
22
export * from './app.component';
3+
export * from './app.module';
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { bootstrap } from '@angular/platform-browser-dynamic';
1+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
22
import { enableProdMode } from '@angular/core';
3-
import { AppComponent, environment } from './app/';<% if(isMobile) { %>
4-
import { APP_SHELL_RUNTIME_PROVIDERS } from '@angular/app-shell';<% } %>
3+
import { AppModule, environment } from './app/';
54

65
if (environment.production) {
76
enableProdMode();
87
}
98

10-
bootstrap(AppComponent<% if(isMobile) { %>, [ APP_SHELL_RUNTIME_PROVIDERS ]<% } %>);
9+
platformBrowserDynamic().bootstrapModule(AppModule);

addon/ng2/blueprints/ng2/files/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
},
1313
"private": true,
1414
"dependencies": {
15-
"@angular/common": "2.0.0-rc.4",
16-
"@angular/compiler": "2.0.0-rc.4",
17-
"@angular/core": "2.0.0-rc.4",
18-
"@angular/forms": "0.2.0",
19-
"@angular/http": "2.0.0-rc.4",
20-
"@angular/platform-browser": "2.0.0-rc.4",
21-
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
22-
"@angular/router": "3.0.0-beta.2",
15+
"@angular/common": "github:angular/common-builds",
16+
"@angular/compiler": "github:angular/compiler-builds",
17+
"@angular/core": "github:angular/core-builds",
18+
"@angular/forms": "github:angular/forms-builds",
19+
"@angular/http": "github:angular/http-builds",
20+
"@angular/platform-browser": "github:angular/platform-browser-builds",
21+
"@angular/platform-browser-dynamic": "github:angular/platform-browser-dynamic-builds",
22+
"@angular/router": "github:angular/router-builds",
2323
"core-js": "^2.4.0",
2424
"reflect-metadata": "0.1.3",
2525
"rxjs": "5.0.0-beta.6",

addon/ng2/blueprints/pipe/index.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var Blueprint = require('ember-cli/lib/models/blueprint');
33
var dynamicPathParser = require('../../utilities/dynamic-path-parser');
44
var addBarrelRegistration = require('../../utilities/barrel-management');
55
var getFiles = Blueprint.prototype.files;
6+
const stringUtils = require('ember-cli-string-utils');
7+
const astUtils = require('../../utilities/ast-utils');
68

79
module.exports = {
810
description: '',
@@ -50,15 +52,29 @@ module.exports = {
5052
},
5153

5254
afterInstall: function(options) {
55+
var returns = [];
56+
var modulePath = path.resolve(process.env.PWD, this.dynamicPath.appRoot, 'app.module.ts');
57+
var classifiedName =
58+
stringUtils.classify(`${options.entity.name}-${options.originBlueprintName}`);
59+
var importPath = '\'./' + stringUtils.dasherize(`${options.entity.name}.pipe';`);
60+
5361
if (!options.flat) {
54-
return addBarrelRegistration(
55-
this,
56-
this.generatePath);
62+
returns.push(function() {
63+
return addBarrelRegistration(this, this.generatePath)
64+
});
5765
} else {
58-
return addBarrelRegistration(
59-
this,
60-
this.generatePath,
61-
options.entity.name + '.pipe');
66+
returns.push(function() {
67+
return addBarrelRegistration(
68+
this,
69+
this.generatePath,
70+
options.entity.name + '.pipe')
71+
});
72+
}
73+
74+
if (!options['skip-import']) {
75+
returns.push(astUtils.importComponent(modulePath, classifiedName, importPath));
6276
}
77+
78+
return Promise.all(returns);
6379
}
6480
};

addon/ng2/utilities/ast-utils.ts

+69-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import * as ts from 'typescript';
2+
import * as fs from 'fs';
23
import { InsertChange } from './change';
34

5+
/**
6+
* Get TS source file based on path.
7+
* @param filePath
8+
* @return source file of ts.SourceFile kind
9+
*/
10+
export function getSource(filePath: string): ts.SourceFile {
11+
return ts.createSourceFile(filePath, fs.readFileSync(filePath).toString(),
12+
ts.ScriptTarget.ES6, true);
13+
}
14+
415
/**
516
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
617
* @param node
718
* @param kind
8-
* @return all nodes of kind kind, or [] if none is found
19+
* @return all nodes of kind, or [] if none is found
920
*/
1021
export function findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] {
1122
if (!node) {
@@ -19,6 +30,26 @@ export function findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] {
1930
foundNodes.concat(findNodes(child, kind)), arr);
2031
}
2132

33+
/**
34+
* Find all nodes from the AST in the subtree based on text.
35+
* @param node
36+
* @param text
37+
* @return all nodes of text, or [] if none is found
38+
*/
39+
export function findNodesByText(node: ts.Node, text: string): ts.Node[] {
40+
if (!node) {
41+
return [];
42+
}
43+
let arr: ts.Node[] = [];
44+
if (node.getText() === text) {
45+
arr.push(node);
46+
}
47+
48+
return node.getChildren().reduce((foundNodes, child) => {
49+
return foundNodes.concat(findNodesByText(child, text));
50+
}, arr);
51+
}
52+
2253
/**
2354
* Helper for sorting nodes.
2455
* @return function to sort nodes in increasing order of position in sourceFile
@@ -52,3 +83,40 @@ export function insertAfterLastOccurrence(nodes: ts.Node[], toInsert: string,
5283
let lastItemPosition: number = lastItem ? lastItem.end : fallbackPos;
5384
return new InsertChange(file, lastItemPosition, toInsert);
5485
}
86+
87+
/**
88+
* Custom function to insert component (component, pipe, directive)
89+
* into NgModule declarations. It also imports the component.
90+
* @param modulePath
91+
* @param classifiedName
92+
* @param importPath
93+
* @return Promise
94+
*/
95+
export function importComponent(modulePath: string, classifiedName: string,
96+
importPath: string): Promise<void> {
97+
let source: ts.SourceFile = this.getSource(modulePath);
98+
99+
let importNode: ts.Node =
100+
this.findNodesByText(source, 'import').pop();
101+
let iPos: ts.LineAndCharacter =
102+
source.getLineAndCharacterOfPosition(importNode.getEnd());
103+
let iLine: number = iPos.line + 1;
104+
let iStart: number = source.getPositionOfLineAndCharacter(iLine, 0);
105+
let iStr: string = `import { ${classifiedName} } from ${importPath}\n`;
106+
let changeImport: InsertChange = new InsertChange(modulePath, iStart, iStr);
107+
108+
return changeImport.apply().then(() => {
109+
source = this.getSource(modulePath);
110+
let declarationsNode: ts.Node =
111+
this.findNodesByText(source, 'declarations').shift();
112+
let dPos: ts.LineAndCharacter =
113+
source.getLineAndCharacterOfPosition(declarationsNode.getEnd());
114+
let dStart: number =
115+
source.getPositionOfLineAndCharacter(dPos.line + 1, -1);
116+
let dStr: string = `\n ${classifiedName},`;
117+
let changeDeclarations: InsertChange = new InsertChange(modulePath, dStart, dStr);
118+
119+
return changeDeclarations.apply();
120+
});
121+
}
122+

0 commit comments

Comments
 (0)