1
- import { chain , externalSchematic , Rule , Tree , SchematicsException , apply , url , template , move , mergeWith , noop , filter } from '@angular-devkit/schematics' ;
1
+ import {
2
+ chain ,
3
+ externalSchematic ,
4
+ Rule ,
5
+ Tree ,
6
+ apply ,
7
+ url ,
8
+ template ,
9
+ move ,
10
+ mergeWith ,
11
+ noop ,
12
+ } from '@angular-devkit/schematics' ;
2
13
import { FeatureOptions } from './schema' ;
3
14
import { strings } from '@angular-devkit/core' ;
4
-
5
- import { addImportToModule , addDeclarationToModule , addExportToModule } from '@schematics/angular/utility/ast-utils' ;
6
- import { InsertChange } from '@schematics/angular/utility/change' ;
7
- import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript' ;
8
- // import * as ts from 'typescript';
9
-
10
-
11
-
12
- // Taken from @schematics /angular
13
- function readIntoSourceFile ( host : Tree , modulePath : string ) : ts . SourceFile {
14
- const text = host . read ( modulePath ) ;
15
- if ( text === null ) {
16
- throw new SchematicsException ( `File ${ modulePath } does not exist.` ) ;
17
- }
18
- const sourceText = text . toString ( 'utf-8' ) ;
19
-
20
- return ts . createSourceFile ( modulePath , sourceText , ts . ScriptTarget . Latest , true ) ;
21
- }
22
-
23
- function addImport (
24
- modulePath : string ,
25
- ngModuleToImportPath : string ,
26
- ngModuleToImportName : string ,
27
- optional = false ) : Rule {
28
-
29
- return ( host : Tree ) => {
30
-
31
- if ( optional && ! host . exists ( modulePath ) ) {
32
- return ;
33
- }
34
-
35
- const source = readIntoSourceFile ( host , modulePath ) ;
36
-
37
- const changes = addImportToModule (
38
- source ,
39
- modulePath ,
40
- ngModuleToImportName ,
41
- ngModuleToImportPath )
42
-
43
- const declarationRecorder = host . beginUpdate ( modulePath ) ;
44
- for ( const change of changes ) {
45
- if ( change instanceof InsertChange ) {
46
- declarationRecorder . insertLeft ( change . pos , change . toAdd ) ;
47
- }
48
- }
49
- host . commitUpdate ( declarationRecorder ) ;
50
- }
51
- }
52
-
53
- function addDeclaration (
54
- modulePath : string ,
55
- componentToImportPath : string ,
56
- componentToImportName : string ) : Rule {
57
-
58
- return ( host : Tree ) => {
59
-
60
- const source = readIntoSourceFile ( host , modulePath ) ;
61
-
62
- const changes = addDeclarationToModule (
63
- source ,
64
- modulePath ,
65
- componentToImportName ,
66
- componentToImportPath ) ;
67
-
68
- const declarationRecorder = host . beginUpdate ( modulePath ) ;
69
- for ( const change of changes ) {
70
- if ( change instanceof InsertChange ) {
71
- declarationRecorder . insertLeft ( change . pos , change . toAdd ) ;
72
- }
73
- }
74
- host . commitUpdate ( declarationRecorder ) ;
75
- }
76
- }
77
-
78
- function addExport (
79
- modulePath : string ,
80
- componentToImportPath : string ,
81
- componentToImportName : string ) : Rule {
82
-
83
- return ( host : Tree ) => {
84
-
85
- const source = readIntoSourceFile ( host , modulePath ) ;
86
-
87
- const changes = addExportToModule (
88
- source ,
89
- modulePath ,
90
- componentToImportName ,
91
- componentToImportPath ) ;
92
-
93
- const declarationRecorder = host . beginUpdate ( modulePath ) ;
94
- for ( const change of changes ) {
95
- if ( change instanceof InsertChange ) {
96
- declarationRecorder . insertLeft ( change . pos , change . toAdd ) ;
97
- }
98
- }
99
- host . commitUpdate ( declarationRecorder ) ;
100
- }
101
- }
102
-
103
- function addTsExport ( filePath : string , filesToExport : string [ ] ) : Rule {
15
+ import {
16
+ addDeclaration ,
17
+ addExport ,
18
+ addImport ,
19
+ addTsExport ,
20
+ filterTemplates ,
21
+ } from '../rules' ;
22
+ import { readWorkspaceName } from '../utils' ;
23
+
24
+ export default function ( options : FeatureOptions ) : Rule {
104
25
return ( host : Tree ) => {
105
- let content = host . read ( filePath ) + '\n' ;
106
-
107
- for ( const file of filesToExport ) {
108
- content += `export * from '${ file } ';\n` ;
109
- }
110
-
111
- host . overwrite ( filePath , content ) ;
112
- }
113
- }
114
-
115
- function filterTemplates ( options : FeatureOptions ) : Rule {
116
- if ( ! options . entity ) {
117
- return filter ( path => ! ! path . match ( / \. f a c a d e \. t s $ / ) ) ;
118
- }
119
- return filter ( _ => true ) ;
120
- }
121
-
122
- function readWorkspaceName ( host : Tree ) : string {
123
- const content = host . read ( 'nx.json' ) . toString ( ) ;
124
- const config = JSON . parse ( content ) ;
125
- return '@' + config [ 'npmScope' ] ;
126
- }
127
-
128
- export default function ( options : FeatureOptions ) : Rule {
129
-
130
- return ( host : Tree ) => {
131
-
132
26
const workspaceName = readWorkspaceName ( host ) ;
133
27
134
28
const domainFolderName = strings . dasherize ( options . domain ) ;
135
29
const domainPath = `libs/${ domainFolderName } /domain/src/lib` ;
136
- const domainModuleClassName = strings . classify ( options . domain ) + "DomainModule" ;
30
+ const domainModuleClassName =
31
+ strings . classify ( options . domain ) + 'DomainModule' ;
137
32
const domainImportPath = `${ workspaceName } /${ domainFolderName } /domain` ;
138
33
const domainIndexPath = `libs/${ domainFolderName } /domain/src/index.ts` ;
139
34
140
35
const featureName = strings . dasherize ( options . name ) ;
141
36
const featureFolderName = ( options . prefix ? 'feature-' : '' ) + featureName ;
142
37
const featurePath = `libs/${ domainFolderName } /${ featureFolderName } /src/lib` ;
143
38
const featureModulePath = `${ featurePath } /${ domainFolderName } -${ featureFolderName } .module.ts` ;
144
- const featureModuleClassName = strings . classify ( `${ options . domain } -${ featureFolderName } Module` ) ;
39
+ const featureModuleClassName = strings . classify (
40
+ `${ options . domain } -${ featureFolderName } Module`
41
+ ) ;
145
42
const featureImportPath = `${ workspaceName } /${ domainFolderName } /${ featureFolderName } ` ;
146
43
const featureIndexPath = `libs/${ domainFolderName } /${ featureFolderName } /src/index.ts` ;
147
44
148
45
const entityName = options . entity ? strings . dasherize ( options . entity ) : '' ;
149
46
150
47
const featureComponentImportPath = `./${ featureName } .component` ;
151
- const featureComponentClassName = strings . classify ( `${ featureName } Component` ) ;
48
+ const featureComponentClassName = strings . classify (
49
+ `${ featureName } Component`
50
+ ) ;
152
51
153
52
const appName = options . app || options . domain ;
154
53
const appFolderName = strings . dasherize ( appName ) ;
@@ -157,7 +56,9 @@ export default function(options: FeatureOptions): Rule {
157
56
if ( options . app ) {
158
57
const requiredAppModulePath = `apps/${ appFolderName } /src/app/app.module.ts` ;
159
58
if ( ! host . exists ( requiredAppModulePath ) ) {
160
- throw new Error ( `Specified app ${ options . app } does not exist: ${ requiredAppModulePath } expected!` ) ;
59
+ throw new Error (
60
+ `Specified app ${ options . app } does not exist: ${ requiredAppModulePath } expected!`
61
+ ) ;
161
62
}
162
63
}
163
64
@@ -175,7 +76,7 @@ export default function(options: FeatureOptions): Rule {
175
76
176
77
const featureTemplates = apply ( url ( './files/forFeature' ) , [
177
78
template ( { ...strings , ...options , workspaceName } ) ,
178
- move ( featurePath )
79
+ move ( featurePath ) ,
179
80
] ) ;
180
81
181
82
return chain ( [
@@ -189,24 +90,42 @@ export default function(options: FeatureOptions): Rule {
189
90
buildable : options . type === 'buildable' ,
190
91
} ) ,
191
92
addImport ( featureModulePath , domainImportPath , domainModuleClassName ) ,
192
- ( ! options . lazy && host . exists ( appModulePath ) ) ?
193
- chain ( [
194
- addImport ( appModulePath , featureImportPath , featureModuleClassName , true ) ,
195
- addImport ( appModulePath , '@angular/common/http' , 'HttpClientModule' , true )
196
- ] ) :
197
- noop ( ) ,
93
+ ! options . lazy && host . exists ( appModulePath )
94
+ ? chain ( [
95
+ addImport (
96
+ appModulePath ,
97
+ featureImportPath ,
98
+ featureModuleClassName ,
99
+ true
100
+ ) ,
101
+ addImport (
102
+ appModulePath ,
103
+ '@angular/common/http' ,
104
+ 'HttpClientModule' ,
105
+ true
106
+ ) ,
107
+ ] )
108
+ : noop ( ) ,
198
109
mergeWith ( domainTemplates ) ,
199
- ( options . entity ) ?
200
- addTsExport ( domainIndexPath , [
201
- `./lib/entities/${ entityName } ` ,
202
- `./lib/infrastructure/${ entityName } .data.service`
203
- ] ) :
204
- noop ( ) ,
110
+ options . entity
111
+ ? addTsExport ( domainIndexPath , [
112
+ `./lib/entities/${ entityName } ` ,
113
+ `./lib/infrastructure/${ entityName } .data.service` ,
114
+ ] )
115
+ : noop ( ) ,
205
116
addTsExport ( domainIndexPath , [ `./lib/application/${ featureName } .facade` ] ) ,
206
117
mergeWith ( featureTemplates ) ,
207
118
addTsExport ( featureIndexPath , [ `./lib/${ featureName } .component` ] ) ,
208
- addDeclaration ( featureModulePath , featureComponentImportPath , featureComponentClassName ) ,
209
- addExport ( featureModulePath , featureComponentImportPath , featureComponentClassName ) ,
119
+ addDeclaration (
120
+ featureModulePath ,
121
+ featureComponentImportPath ,
122
+ featureComponentClassName
123
+ ) ,
124
+ addExport (
125
+ featureModulePath ,
126
+ featureComponentImportPath ,
127
+ featureComponentClassName
128
+ ) ,
210
129
] ) ;
211
- }
130
+ } ;
212
131
}
0 commit comments