@@ -5,7 +5,7 @@ import { SchematicsException, Tree } from '@angular-devkit/schematics';
55import ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript' ;
66import { findNode , addImportToModule , insertImport } from '@schematics/angular/utility/ast-utils' ;
77import { InsertChange , ReplaceChange , applyToUpdateRecorder , Change } from '@schematics/angular/utility/change' ;
8- import { findModuleFromOptions , buildRelativePath } from '@schematics/angular/utility/find-module' ;
8+ import { buildRelativePath } from '@schematics/angular/utility/find-module' ;
99import { overwriteIfExists } from './common' ;
1010
1111// We consider a project to be a universal project if it has a `server` architect
@@ -151,17 +151,36 @@ export function addEnvironmentEntry(
151151 return host ;
152152}
153153
154- export function addToNgModule ( host : Tree , options : { sourcePath : string , features : FEATURES [ ] } ) {
155-
156- const modulePath = findModuleFromOptions ( host , {
157- name : 'app' ,
158- path : options . sourcePath ,
159- } ) ;
154+ // TODO rewrite using typescript
155+ export function addFixesToServer ( host : Tree , options : { sourcePath : string , features : FEATURES [ ] } ) {
156+ const serverPath = `/server.ts` ;
160157
161- if ( ! modulePath ) {
158+ if ( ! host . exists ( serverPath ) ) {
162159 return host ;
163160 }
164161
162+ const text = host . read ( serverPath ) ;
163+ if ( text === null ) {
164+ throw new SchematicsException ( `File ${ serverPath } does not exist.` ) ;
165+ }
166+ const sourceText = text . toString ( 'utf-8' ) ;
167+ const addZonePatch = ! sourceText . includes ( 'import \'zone.js/dist/zone-patch-rxjs\';' ) ;
168+ const addFirestorePatch = options . features . includes ( FEATURES . Firestore ) &&
169+ ! sourceText . includes ( 'import \'@angular/fire/firestore-protos\';' ) ;
170+
171+ if ( addZonePatch || addFirestorePatch ) {
172+ overwriteIfExists ( host , serverPath , sourceText . replace ( 'import \'zone.js/dist/zone-node\';' , `import 'zone.js/dist/zone-node';
173+ ${ addZonePatch ? 'import \'zone.js/dist/zone-patch-rxjs\';' : '' }
174+ ${ addFirestorePatch ? 'import \'@angular/fire/firestore-protos\';' : '' } `) ) ;
175+ }
176+
177+ return host ;
178+ }
179+
180+ export function addToNgModule ( host : Tree , options : { sourcePath : string , features : FEATURES [ ] } ) {
181+
182+ const modulePath = `/${ options . sourcePath } /app/app.module.ts` ;
183+
165184 if ( ! host . exists ( modulePath ) ) {
166185 throw new Error ( `Specified module path ${ modulePath } does not exist` ) ;
167186 }
0 commit comments