1
1
import { strings } from '@angular-devkit/core' ;
2
2
import { Rule , SchematicsException , Tree , apply , branchAndMerge , chain , filter , mergeWith , move , noop , template , url } from '@angular-devkit/schematics' ;
3
- import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils' ;
3
+ import { addDeclarationToModule , addEntryComponentToModule , addExportToModule , addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils' ;
4
4
import { InsertChange } from '@schematics/angular/utility/change' ;
5
5
import { buildRelativePath } from '@schematics/angular/utility/find-module' ;
6
6
import { parseName } from '@schematics/angular/utility/parse-name' ;
@@ -27,7 +27,79 @@ function addImportToNgModule(options: ComponentOptions): Rule {
27
27
if ( ! options . module ) {
28
28
return host ;
29
29
}
30
+ if ( ! options . createModule && options . module ) {
31
+ addImportToDeclarations ( host , options ) ;
32
+ }
33
+ if ( options . createModule && options . module ) {
34
+ addImportToImports ( host , options ) ;
35
+ }
36
+ return host ;
37
+ } ;
38
+ }
39
+
40
+ function addImportToDeclarations ( host : Tree , options : ComponentOptions ) : void {
41
+ if ( options . module ) {
42
+ const modulePath = options . module ;
43
+ let source = readIntoSourceFile ( host , modulePath ) ;
44
+
45
+ const componentPath = `/${ options . path } /`
46
+ + ( options . flat ? '' : strings . dasherize ( options . name ) + '/' )
47
+ + strings . dasherize ( options . name )
48
+ + '.component' ;
49
+ const relativePath = buildRelativePath ( modulePath , componentPath ) ;
50
+ const classifiedName = strings . classify ( `${ options . name } Component` ) ;
51
+ const declarationChanges = addDeclarationToModule ( source ,
52
+ modulePath ,
53
+ classifiedName ,
54
+ relativePath ) ;
55
+
56
+ const declarationRecorder = host . beginUpdate ( modulePath ) ;
57
+ for ( const change of declarationChanges ) {
58
+ if ( change instanceof InsertChange ) {
59
+ declarationRecorder . insertLeft ( change . pos , change . toAdd ) ;
60
+ }
61
+ }
62
+ host . commitUpdate ( declarationRecorder ) ;
63
+
64
+ if ( options . export ) {
65
+ // Need to refresh the AST because we overwrote the file in the host.
66
+ source = readIntoSourceFile ( host , modulePath ) ;
30
67
68
+ const exportRecorder = host . beginUpdate ( modulePath ) ;
69
+ const exportChanges = addExportToModule ( source , modulePath ,
70
+ strings . classify ( `${ options . name } Component` ) ,
71
+ relativePath ) ;
72
+
73
+ for ( const change of exportChanges ) {
74
+ if ( change instanceof InsertChange ) {
75
+ exportRecorder . insertLeft ( change . pos , change . toAdd ) ;
76
+ }
77
+ }
78
+ host . commitUpdate ( exportRecorder ) ;
79
+ }
80
+
81
+ if ( options . entryComponent ) {
82
+ // Need to refresh the AST because we overwrote the file in the host.
83
+ source = readIntoSourceFile ( host , modulePath ) ;
84
+
85
+ const entryComponentRecorder = host . beginUpdate ( modulePath ) ;
86
+ const entryComponentChanges = addEntryComponentToModule (
87
+ source , modulePath ,
88
+ strings . classify ( `${ options . name } Component` ) ,
89
+ relativePath ) ;
90
+
91
+ for ( const change of entryComponentChanges ) {
92
+ if ( change instanceof InsertChange ) {
93
+ entryComponentRecorder . insertLeft ( change . pos , change . toAdd ) ;
94
+ }
95
+ }
96
+ host . commitUpdate ( entryComponentRecorder ) ;
97
+ }
98
+ }
99
+ }
100
+
101
+ function addImportToImports ( host : Tree , options : ComponentOptions ) : void {
102
+ if ( options . module ) {
31
103
const modulePath = options . module ;
32
104
const moduleSource = readIntoSourceFile ( host , modulePath ) ;
33
105
@@ -47,8 +119,7 @@ function addImportToNgModule(options: ComponentOptions): Rule {
47
119
}
48
120
}
49
121
host . commitUpdate ( importRecorder ) ;
50
- return host ;
51
- } ;
122
+ }
52
123
}
53
124
54
125
export default function ( options : ComponentOptions ) : Rule {
@@ -73,6 +144,7 @@ export default function(options: ComponentOptions): Rule {
73
144
74
145
const templateSource = apply ( url ( './files' ) , [
75
146
options . spec ? noop ( ) : filter ( p => ! p . endsWith ( '.spec.ts' ) ) ,
147
+ options . createModule ? noop ( ) : filter ( p => ! p . endsWith ( '.module.ts' ) ) ,
76
148
template ( {
77
149
...strings ,
78
150
'if-flat' : ( s : string ) => options . flat ? '' : s ,
0 commit comments