@@ -22,14 +22,27 @@ import {
22
22
url ,
23
23
} from '@angular-devkit/schematics' ;
24
24
import * 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' ;
26
30
import { InsertChange } from '../utility/change' ;
27
31
import { getWorkspace } from '../utility/config' ;
28
32
import { buildRelativePath , findModuleFromOptions } from '../utility/find-module' ;
29
33
import { parseName } from '../utility/parse-name' ;
30
34
import { validateHtmlSelector , validateName } from '../utility/validation' ;
31
35
import { Schema as ComponentOptions } from './schema' ;
32
36
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
+ }
33
46
34
47
function addDeclarationToNgModule ( options : ComponentOptions ) : Rule {
35
48
return ( host : Tree ) => {
@@ -38,12 +51,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
38
51
}
39
52
40
53
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 ) ;
47
55
48
56
const componentPath = `/${ options . path } /`
49
57
+ ( options . flat ? '' : strings . dasherize ( options . name ) + '/' )
@@ -66,12 +74,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
66
74
67
75
if ( options . export ) {
68
76
// 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 ) ;
75
78
76
79
const exportRecorder = host . beginUpdate ( modulePath ) ;
77
80
const exportChanges = addExportToModule ( source , modulePath ,
@@ -86,6 +89,24 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
86
89
host . commitUpdate ( exportRecorder ) ;
87
90
}
88
91
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
+
89
110
90
111
return host ;
91
112
} ;
0 commit comments