@@ -30,6 +30,14 @@ import { parseName } from '../utility/parse-name';
30
30
import { validateHtmlSelector , validateName } from '../utility/validation' ;
31
31
import { Schema as ComponentOptions } from './schema' ;
32
32
33
+ function readIntoSourceFile ( host : Tree , modulePath : string ) : ts . SourceFile {
34
+ const text = host . read ( modulePath ) ;
35
+ if ( text === null ) {
36
+ throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
37
+ }
38
+ const sourceText = text . toString ( 'utf-8' ) ;
39
+ return ts . createSourceFile ( modulePath , sourceText , ts . ScriptTarget . Latest , true ) ;
40
+ }
33
41
34
42
function addDeclarationToNgModule ( options : ComponentOptions ) : Rule {
35
43
return ( host : Tree ) => {
@@ -38,12 +46,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
38
46
}
39
47
40
48
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 ) ;
49
+ const source = readIntoSourceFile ( host , modulePath ) ;
47
50
48
51
const componentPath = `/${ options . path } /`
49
52
+ ( options . flat ? '' : strings . dasherize ( options . name ) + '/' )
@@ -66,12 +69,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
66
69
67
70
if ( options . export ) {
68
71
// 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 ) ;
72
+ const source = readIntoSourceFile ( host , modulePath ) ;
75
73
76
74
const exportRecorder = host . beginUpdate ( modulePath ) ;
77
75
const exportChanges = addExportToModule ( source , modulePath ,
@@ -88,12 +86,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
88
86
89
87
if ( options . entryComponent ) {
90
88
// Need to refresh the AST because we overwrote the file in the host.
91
- const text = host . read ( modulePath ) ;
92
- if ( text === null ) {
93
- throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
94
- }
95
- const sourceText = text . toString ( 'utf-8' ) ;
96
- const source = ts . createSourceFile ( modulePath , sourceText , ts . ScriptTarget . Latest , true ) ;
89
+ const source = readIntoSourceFile ( host , modulePath ) ;
97
90
98
91
const entryComponentRecorder = host . beginUpdate ( modulePath ) ;
99
92
const entryComponentChanges = addEntryComponentToModule ( source , modulePath ,
0 commit comments