@@ -2,9 +2,14 @@ import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devk
2
2
import { overwriteIfExists , safeReadJSON , stringifyFormatted } from '../../ng-add-common' ;
3
3
import { default as defaultDependencies , firebaseFunctions } from '../../versions.json' ;
4
4
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
+ }
8
13
9
14
export const ngUpdate = ( ) : Rule => (
10
15
host : Tree ,
@@ -49,20 +54,20 @@ export const ngUpdate = (): Rule => (
49
54
if ( ! content ) {
50
55
return ;
51
56
}
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 ) {
66
71
host . overwrite ( filePath , content ) ;
67
72
}
68
73
} ) ;
0 commit comments