diff --git a/src/schematics/update/v7/index.ts b/src/schematics/update/v7/index.ts index ee65c9378..be16be6df 100644 --- a/src/schematics/update/v7/index.ts +++ b/src/schematics/update/v7/index.ts @@ -2,6 +2,10 @@ import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devk import { overwriteIfExists, safeReadJSON, stringifyFormatted } from '../../ng-add-common'; import { default as defaultDependencies, firebaseFunctions } from '../../versions.json'; +const FIREBASE_IMPORT_REGEX = /(?import|export)\s+(?:(?[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?firebase\/(?[@\w\s\\\/.-]+)\3?)\s*;/g; +const AT_FIREBASE_IMPORT_REGEX = /(?import|export)\s+(?:(?[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@firebase\/(?[@\w\s\\\/.-]+)\3?)\s*;/g; +const ANGULAR_FIRE_IMPORT_REGEX = /(?import|export)\s+(?:(?[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@angular\/fire\/(?[@\w\s\\\/.-]+)\3?)\s*;/g; + export const ngUpdate = (): Rule => ( host: Tree, context: SchematicContext @@ -45,14 +49,23 @@ export const ngUpdate = (): Rule => ( if (!content) { return; } - // TODO clean this up, skip overwrite if uneeded - content.replace(/(?import|export)\s+(?:(?[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?firebase\/(?[@\w\s\\\/.-]+)\3?)\s*;/, '$1 $2 from $3firebase/compat/$4$3;'); - content.replace(/(?import|export)\s+(?:(?[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@firebase\/(?[@\w\s\\\/.-]+)\3?)\s*;/, '$1 $2 from $3@firebase/compat/$4$3;'); - content.replace(/(?import|export)\s+(?:(?[\w,{}\s\*]+)\s+from)?\s*(?:(["'])?@angular\/fire\/(?[@\w\s\\\/.-]+)\3?)\s*;/, '$1 $2 from $3@angular/fire/compat/$4$3;'); - host.overwrite(filePath, content); - console.log(filePath); + let didChangeContent = false; + if (content.match(FIREBASE_IMPORT_REGEX)) { + content.replace(FIREBASE_IMPORT_REGEX, '$1 $2 from $3firebase/compat/$4$3;'); + didChangeContent = true; + } + if (content.match(AT_FIREBASE_IMPORT_REGEX)) { + content.replace(AT_FIREBASE_IMPORT_REGEX, '$1 $2 from $3@firebase/compat/$4$3;'); + didChangeContent = true; + } + if (content.match(ANGULAR_FIRE_IMPORT_REGEX)) { + content.replace(ANGULAR_FIRE_IMPORT_REGEX, '$1 $2 from $3@angular/fire/compat/$4$3;'); + didChangeContent = true; + } + if (didChangeContent) { + host.overwrite(filePath, content); + } }); - console.log('Called ng-update'); return host; };