@@ -22,14 +22,27 @@ import {
2222 url ,
2323} from '@angular-devkit/schematics' ;
2424import * as ts from 'typescript' ;
25- import { addDeclarationToModule , addExportToModule } from '../utility/ast-utils' ;
25+ import {
26+ addDeclarationToModule ,
27+ addEntryComponentToModule ,
28+ addExportToModule ,
29+ } from '../utility/ast-utils' ;
2630import { InsertChange } from '../utility/change' ;
2731import { getWorkspace } from '../utility/config' ;
2832import { buildRelativePath , findModuleFromOptions } from '../utility/find-module' ;
2933import { parseName } from '../utility/parse-name' ;
3034import { validateHtmlSelector , validateName } from '../utility/validation' ;
3135import { Schema as ComponentOptions } from './schema' ;
3236
37+ function readIntoSourceFile ( host : Tree , modulePath : string ) : ts . SourceFile {
38+ const text = host . read ( modulePath ) ;
39+ if ( text === null ) {
40+ throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
41+ }
42+ const sourceText = text . toString ( 'utf-8' ) ;
43+
44+ return ts . createSourceFile ( modulePath , sourceText , ts . ScriptTarget . Latest , true ) ;
45+ }
3346
3447function addDeclarationToNgModule ( options : ComponentOptions ) : Rule {
3548 return ( host : Tree ) => {
@@ -38,12 +51,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
3851 }
3952
4053 const modulePath = options . module ;
41- const text = host . read ( modulePath ) ;
42- if ( text === null ) {
43- throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
44- }
45- const sourceText = text . toString ( 'utf-8' ) ;
46- const source = ts . createSourceFile ( modulePath , sourceText , ts . ScriptTarget . Latest , true ) ;
54+ const source = readIntoSourceFile ( host , modulePath ) ;
4755
4856 const componentPath = `/${ options . path } /`
4957 + ( options . flat ? '' : strings . dasherize ( options . name ) + '/' )
@@ -66,12 +74,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
6674
6775 if ( options . export ) {
6876 // Need to refresh the AST because we overwrote the file in the host.
69- const text = host . read ( modulePath ) ;
70- if ( text === null ) {
71- throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
72- }
73- const sourceText = text . toString ( 'utf-8' ) ;
74- const source = ts . createSourceFile ( modulePath , sourceText , ts . ScriptTarget . Latest , true ) ;
77+ const source = readIntoSourceFile ( host , modulePath ) ;
7578
7679 const exportRecorder = host . beginUpdate ( modulePath ) ;
7780 const exportChanges = addExportToModule ( source , modulePath ,
@@ -86,6 +89,24 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
8689 host . commitUpdate ( exportRecorder ) ;
8790 }
8891
92+ if ( options . entryComponent ) {
93+ // Need to refresh the AST because we overwrote the file in the host.
94+ const source = readIntoSourceFile ( host , modulePath ) ;
95+
96+ const entryComponentRecorder = host . beginUpdate ( modulePath ) ;
97+ const entryComponentChanges = addEntryComponentToModule (
98+ source , modulePath ,
99+ strings . classify ( `${ options . name } Component` ) ,
100+ relativePath ) ;
101+
102+ for ( const change of entryComponentChanges ) {
103+ if ( change instanceof InsertChange ) {
104+ entryComponentRecorder . insertLeft ( change . pos , change . toAdd ) ;
105+ }
106+ }
107+ host . commitUpdate ( entryComponentRecorder ) ;
108+ }
109+
89110
90111 return host ;
91112 } ;
0 commit comments