@@ -2,9 +2,14 @@ import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devk
22import { overwriteIfExists , safeReadJSON , stringifyFormatted } from '../../ng-add-common' ;
33import { default as defaultDependencies , firebaseFunctions } from '../../versions.json' ;
44
5- const FIREBASE_IMPORT_REGEX = / (?< key > i m p o r t | e x p o r t ) \s + (?: (?< alias > [ \w , { } \s \* ] + ) \s + f r o m ) ? \s * (?: ( [ " ' ] ) ? f i r e b a s e \/ (?< ref > [ @ \w \s \\ \/ . - ] + ) \3? ) \s * ; / g;
6- const AT_FIREBASE_IMPORT_REGEX = / (?< key > i m p o r t | e x p o r t ) \s + (?: (?< alias > [ \w , { } \s \* ] + ) \s + f r o m ) ? \s * (?: ( [ " ' ] ) ? @ f i r e b a s e \/ (?< ref > [ @ \w \s \\ \/ . - ] + ) \3? ) \s * ; / g;
7- const ANGULAR_FIRE_IMPORT_REGEX = / (?< key > i m p o r t | e x p o r t ) \s + (?: (?< alias > [ \w , { } \s \* ] + ) \s + f r o m ) ? \s * (?: ( [ " ' ] ) ? @ a n g u l a r \/ f i r e \/ (?< ref > [ @ \w \s \\ \/ . - ] + ) \3? ) \s * ; / g;
5+ const IMPORT_REGEX = / (?< key > i m p o r t | e x p o r t ) \s + (?: (?< alias > [ \w , { } \s \* ] + ) \s + f r o m ) ? \s * (?: (?< quote > [ " ' ] ) ? (?< ref > [ @ \w \s \\ \/ . - ] + ) \3? ) \s * (?< term > [ ; \n ] ) / g;
6+ interface ImportRegexMatch {
7+ key : string ;
8+ alias : string ;
9+ ref : string ;
10+ quote : string ;
11+ term : string ;
12+ }
813
914export const ngUpdate = ( ) : Rule => (
1015 host : Tree ,
@@ -49,20 +54,20 @@ export const ngUpdate = (): Rule => (
4954 if ( ! content ) {
5055 return ;
5156 }
52- let didChangeContent = false ;
53- if ( content . match ( FIREBASE_IMPORT_REGEX ) ) {
54- content . replace ( FIREBASE_IMPORT_REGEX , '$1 $2 from $3firebase/compat/$4$3;' ) ;
55- didChangeContent = true ;
56- }
57- if ( content . match ( AT_FIREBASE_IMPORT_REGEX ) ) {
58- content . replace ( AT_FIREBASE_IMPORT_REGEX , '$1 $2 from $3@ firebase/compat/$4$3;' ) ;
59- didChangeContent = true ;
60- }
61- if ( content . match ( ANGULAR_FIRE_IMPORT_REGEX ) ) {
62- content . replace ( ANGULAR_FIRE_IMPORT_REGEX , '$1 $2 from $3@angular/fire/compat/$4$3;' ) ;
63- didChangeContent = true ;
64- }
65- if ( didChangeContent ) {
57+ const newContent = content . replace ( IMPORT_REGEX , ( substring , ... args ) => {
58+ const { alias , key , ref , quote , term } : ImportRegexMatch = args . pop ( ) ;
59+ if ( ref . startsWith ( '@angular/fire' ) && ! ref . startsWith ( '@angular/fire/compat' ) ) {
60+ return ` ${ key } ${ alias } from ${ quote } ${ ref . replace ( '@angular/fire' , '@angular/fire/compat' ) } ${ quote } ${ term } ` ;
61+ }
62+ if ( ref . startsWith ( 'firebase' ) && ! ref . startsWith ( 'firebase/compat' ) ) {
63+ return ` ${ key } ${ alias } from ${ quote } ${ ref . replace ( ' firebase' , 'firebase /compat' ) } ${ quote } ${ term } ` ;
64+ }
65+ if ( ref . startsWith ( '@firebase' ) ) {
66+ return ` ${ key } ${ alias } from ${ quote } ${ ref . replace ( '@firebase' , 'firebase' ) } ${ quote } ${ term } ` ;
67+ }
68+ return substring ;
69+ } ) ;
70+ if ( content !== newContent ) {
6671 host . overwrite ( filePath , content ) ;
6772 }
6873 } ) ;
0 commit comments