1
1
import { SchematicsException , Tree } from '@angular-devkit/schematics' ;
2
- import { FirebaseJSON , FirebaseRc } from './interfaces' ;
2
+ import { FirebaseJSON , FirebaseRc , FirebaseHostingConfig } from './interfaces' ;
3
3
import { experimental , JsonParseMode , parseJson } from '@angular-devkit/core' ;
4
4
import { from } from 'rxjs' ;
5
5
import { map , switchMap } from 'rxjs/operators' ;
@@ -52,13 +52,24 @@ function generateFirebaseJson(
52
52
? safeReadJSON ( path , tree )
53
53
: emptyFirebaseJson ( ) ;
54
54
55
- if ( firebaseJson . hosting . find ( config => config . target === project ) ) {
55
+ if ( firebaseJson . hosting &&
56
+ ( Array . isArray ( firebaseJson . hosting ) &&
57
+ firebaseJson . hosting . find ( config => config . target === project ) ||
58
+ ( < FirebaseHostingConfig > firebaseJson . hosting ) . target === project
59
+ ) ) {
56
60
throw new SchematicsException (
57
61
`Target ${ project } already exists in firebase.json`
58
62
) ;
59
63
}
60
64
61
- firebaseJson . hosting . push ( generateHostingConfig ( project , dist ) ) ;
65
+ const newConfig = generateHostingConfig ( project , dist ) ;
66
+ if ( firebaseJson . hosting === undefined ) {
67
+ firebaseJson . hosting = newConfig ;
68
+ } else if ( Array . isArray ( firebaseJson . hosting ) ) {
69
+ firebaseJson . hosting . push ( newConfig ) ;
70
+ } else {
71
+ firebaseJson . hosting = [ firebaseJson . hosting ! , newConfig ] ;
72
+ }
62
73
63
74
overwriteIfExists ( tree , path , stringifyFormatted ( firebaseJson ) ) ;
64
75
}
@@ -84,7 +95,9 @@ function generateFirebaseRc(
84
95
? safeReadJSON ( path , tree )
85
96
: emptyFirebaseRc ( ) ;
86
97
87
- if ( firebaseProject in firebaseRc . targets ) {
98
+ firebaseRc . targets = firebaseRc . targets || { } ;
99
+
100
+ if ( firebaseProject in firebaseRc . targets ! ) {
88
101
throw new SchematicsException (
89
102
`Firebase project ${ firebaseProject } already defined in .firebaserc`
90
103
) ;
0 commit comments