@@ -3,13 +3,14 @@ import { Path } from '@angular-devkit/core';
3
3
import {
4
4
apply ,
5
5
applyTemplates ,
6
+ branchAndMerge ,
6
7
chain ,
7
8
mergeWith ,
8
9
move ,
9
- noop ,
10
10
Rule ,
11
11
SchematicContext ,
12
12
SchematicsException ,
13
+ noop ,
13
14
Tree ,
14
15
url ,
15
16
} from '@angular-devkit/schematics' ;
@@ -29,6 +30,12 @@ import {
29
30
visitTSSourceFiles ,
30
31
} from '../../schematics-core' ;
31
32
import { Schema as EntityDataOptions } from './schema' ;
33
+ import { getProjectMainFile } from '../../schematics-core/utility/project' ;
34
+ import { isStandaloneApp } from '../../schematics-core/utility/standalone' ;
35
+ import {
36
+ addFunctionalProvidersToStandaloneBootstrap ,
37
+ callsProvidersFunction ,
38
+ } from '@schematics/angular/private/standalone' ;
32
39
33
40
function addNgRxDataToPackageJson ( ) {
34
41
return ( host : Tree , context : SchematicContext ) => {
@@ -95,6 +102,83 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
95
102
} ;
96
103
}
97
104
105
+ function addStandaloneConfig ( options : EntityDataOptions ) : Rule {
106
+ return ( host : Tree ) => {
107
+ const mainFile = getProjectMainFile ( host , options ) ;
108
+ if ( host . exists ( mainFile ) ) {
109
+ const providerFn = 'provideEntityData' ;
110
+
111
+ if ( callsProvidersFunction ( host , mainFile , providerFn ) ) {
112
+ // exit because the store config is already provided
113
+ return host ;
114
+ }
115
+
116
+ const providerOptions = [
117
+ ...( options . entityConfig
118
+ ? [ ts . factory . createIdentifier ( `entityConfig` ) ]
119
+ : [ ts . factory . createIdentifier ( `{}` ) ] ) ,
120
+ ...( options . effects
121
+ ? [ ts . factory . createIdentifier ( `withEffects()` ) ]
122
+ : [ ] ) ,
123
+ ] ;
124
+
125
+ const patchedConfigFile = addFunctionalProvidersToStandaloneBootstrap (
126
+ host ,
127
+ mainFile ,
128
+ providerFn ,
129
+ '@ngrx/data' ,
130
+ providerOptions
131
+ ) ;
132
+
133
+ const configFileContent = host . read ( patchedConfigFile ) ;
134
+ const source = ts . createSourceFile (
135
+ patchedConfigFile ,
136
+ configFileContent ?. toString ( 'utf-8' ) || '' ,
137
+ ts . ScriptTarget . Latest ,
138
+ true
139
+ ) ;
140
+
141
+ const recorder = host . beginUpdate ( patchedConfigFile ) ;
142
+
143
+ const changes = [ ] ;
144
+
145
+ if ( options . effects ) {
146
+ const withEffectsImport = insertImport (
147
+ source ,
148
+ patchedConfigFile ,
149
+ 'withEffects' ,
150
+ '@ngrx/data'
151
+ ) ;
152
+
153
+ changes . push ( withEffectsImport ) ;
154
+ }
155
+
156
+ if ( options . entityConfig ) {
157
+ const entityConfigImport = insertImport (
158
+ source ,
159
+ patchedConfigFile ,
160
+ 'entityConfig' ,
161
+ './entity-metadata'
162
+ ) ;
163
+
164
+ changes . push ( entityConfigImport ) ;
165
+ }
166
+
167
+ changes . forEach ( ( change : any ) => {
168
+ recorder . insertLeft ( change . pos , change . toAdd ) ;
169
+ } ) ;
170
+
171
+ host . commitUpdate ( recorder ) ;
172
+
173
+ return host ;
174
+ }
175
+
176
+ throw new SchematicsException (
177
+ `Main file not found for a project ${ options . project } `
178
+ ) ;
179
+ } ;
180
+ }
181
+
98
182
const renames = {
99
183
NgrxDataModule : 'EntityDataModule' ,
100
184
NgrxDataModuleWithoutEffects : 'EntityDataModuleWithoutEffects' ,
@@ -285,22 +369,29 @@ export default function (options: EntityDataOptions): Rule {
285
369
return ( host : Tree , context : SchematicContext ) => {
286
370
( options as any ) . name = '' ;
287
371
options . path = getProjectPath ( host , options ) ;
372
+ const mainFile = getProjectMainFile ( host , options ) ;
373
+ const isStandalone = isStandaloneApp ( host , mainFile ) ;
288
374
options . effects = options . effects === undefined ? true : options . effects ;
289
- options . module = options . module
290
- ? findModuleFromOptions ( host , options as any )
291
- : options . module ;
375
+ options . module =
376
+ options . module && ! isStandalone
377
+ ? findModuleFromOptions ( host , options as any )
378
+ : options . module ;
292
379
293
380
const parsedPath = parseName ( options . path , '' ) ;
294
381
options . path = parsedPath . path ;
295
382
383
+ const configOrModuleUpdate = isStandalone
384
+ ? addStandaloneConfig ( options )
385
+ : addEntityDataToNgModule ( options ) ;
386
+
296
387
return chain ( [
297
388
options && options . skipPackageJson ? noop ( ) : addNgRxDataToPackageJson ( ) ,
298
389
options . migrateNgrxData
299
390
? chain ( [
300
391
removeAngularNgRxDataFromPackageJson ( ) ,
301
392
renameNgrxDataModule ( ) ,
302
393
] )
303
- : addEntityDataToNgModule ( options ) ,
394
+ : branchAndMerge ( chain ( [ configOrModuleUpdate ] ) ) ,
304
395
options . entityConfig
305
396
? createEntityConfigFile ( options , parsedPath . path )
306
397
: noop ( ) ,
0 commit comments