-
Notifications
You must be signed in to change notification settings - Fork 12k
refactor(NgModule): update to RC5 #1579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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';<% } %> | ||
|
||
@NgModule({ | ||
declarations: [ | ||
AppComponent | ||
], | ||
imports: [ | ||
BrowserModule, | ||
CommonModule, | ||
FormsModule<% if (isMobile) { %>, | ||
AppShellModule<% } %> | ||
], | ||
entryComponents: [AppComponent], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './environments/environment'; | ||
export * from './app.component'; | ||
export * from './app.module'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import { bootstrap } from '@angular/platform-browser-dynamic'; | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
import { enableProdMode } from '@angular/core'; | ||
import { AppComponent, environment } from './app/';<% if(isMobile) { %> | ||
import { APP_SHELL_RUNTIME_PROVIDERS } from '@angular/app-shell';<% } %> | ||
import { AppModule, environment } from './app/'; | ||
|
||
if (environment.production) { | ||
enableProdMode(); | ||
} | ||
|
||
bootstrap(AppComponent<% if(isMobile) { %>, [ APP_SHELL_RUNTIME_PROVIDERS ]<% } %>); | ||
platformBrowserDynamic().bootstrapModule(AppModule); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import * as ts from 'typescript'; | ||
import * as fs from 'fs'; | ||
import { InsertChange } from './change'; | ||
|
||
/** | ||
* Get TS source file based on path. | ||
* @param filePath | ||
* @return source file of ts.SourceFile kind | ||
*/ | ||
export function getSource(filePath: string): ts.SourceFile { | ||
return ts.createSourceFile(filePath, fs.readFileSync(filePath).toString(), | ||
ts.ScriptTarget.ES6, true); | ||
} | ||
|
||
/** | ||
* Find all nodes from the AST in the subtree of node of SyntaxKind kind. | ||
* @param node | ||
* @param kind | ||
* @return all nodes of kind kind, or [] if none is found | ||
* @return all nodes of kind, or [] if none is found | ||
*/ | ||
export function findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] { | ||
if (!node) { | ||
|
@@ -19,6 +30,26 @@ export function findNodes(node: ts.Node, kind: ts.SyntaxKind): ts.Node[] { | |
foundNodes.concat(findNodes(child, kind)), arr); | ||
} | ||
|
||
/** | ||
* Find all nodes from the AST in the subtree based on text. | ||
* @param node | ||
* @param text | ||
* @return all nodes of text, or [] if none is found | ||
*/ | ||
export function findNodesByText(node: ts.Node, text: string): ts.Node[] { | ||
if (!node) { | ||
return []; | ||
} | ||
let arr: ts.Node[] = []; | ||
if (node.getText() === text) { | ||
arr.push(node); | ||
} | ||
|
||
return node.getChildren().reduce((foundNodes, child) => { | ||
return foundNodes.concat(findNodesByText(child, text)); | ||
}, arr); | ||
} | ||
|
||
/** | ||
* Helper for sorting nodes. | ||
* @return function to sort nodes in increasing order of position in sourceFile | ||
|
@@ -52,3 +83,40 @@ export function insertAfterLastOccurrence(nodes: ts.Node[], toInsert: string, | |
let lastItemPosition: number = lastItem ? lastItem.end : fallbackPos; | ||
return new InsertChange(file, lastItemPosition, toInsert); | ||
} | ||
|
||
/** | ||
* Custom function to insert component (component, pipe, directive) | ||
* into NgModule declarations. It also imports the component. | ||
* @param modulePath | ||
* @param classifiedName | ||
* @param importPath | ||
* @return Promise | ||
*/ | ||
export function importComponent(modulePath: string, classifiedName: string, | ||
importPath: string): Promise<void> { | ||
let source: ts.SourceFile = this.getSource(modulePath); | ||
|
||
let importNode: ts.Node = | ||
this.findNodesByText(source, 'import').pop(); | ||
let iPos: ts.LineAndCharacter = | ||
source.getLineAndCharacterOfPosition(importNode.getEnd()); | ||
let iLine: number = iPos.line + 1; | ||
let iStart: number = source.getPositionOfLineAndCharacter(iLine, 0); | ||
let iStr: string = `import { ${classifiedName} } from ${importPath}\n`; | ||
let changeImport: InsertChange = new InsertChange(modulePath, iStart, iStr); | ||
|
||
return changeImport.apply().then(() => { | ||
source = this.getSource(modulePath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No |
||
let declarationsNode: ts.Node = | ||
this.findNodesByText(source, 'declarations').shift(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this will fail on a number of things, including an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Thanks |
||
let dPos: ts.LineAndCharacter = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can redeclare |
||
source.getLineAndCharacterOfPosition(declarationsNode.getEnd()); | ||
let dStart: number = | ||
source.getPositionOfLineAndCharacter(dPos.line + 1, -1); | ||
let dStr: string = `\n ${classifiedName},`; | ||
let changeDeclarations: InsertChange = new InsertChange(modulePath, dStart, dStr); | ||
|
||
return changeDeclarations.apply(); | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name these
pos
,line
,start
andstr
.