@@ -19,43 +19,46 @@ const { writeFile, readFile } = require('node:fs/promises');
19
19
const { pathToFileURL } = require ( 'node:url' ) ;
20
20
const { isAbsolute, join } = require ( 'node:path' ) ;
21
21
22
+ const ENV_VARIABLE = 'FIREBASE_WEBAPP_CONFIG' ;
23
+
22
24
async function getPartialConfig ( ) {
23
- if ( ! process . env . FIREBASE_WEBAPP_CONFIG ) {
25
+ const envVariable = process . env [ ENV_VARIABLE ] ?. trim ( ) ;
26
+
27
+ if ( ! envVariable ) {
24
28
return undefined ;
25
29
}
26
30
27
31
// Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be
28
32
// either a JSON representation of FirebaseOptions or the path to a filename
29
- if ( process . env . FIREBASE_WEBAPP_CONFIG . startsWith ( '{"' ) ) {
33
+ if ( envVariable . startsWith ( '{"' ) ) {
30
34
try {
31
- return JSON . parse ( process . env . FIREBASE_WEBAPP_CONFIG ) ;
35
+ return JSON . parse ( envVariable ) ;
32
36
} catch ( e ) {
33
37
console . warn (
34
- 'FIREBASE_WEBAPP_CONFIG could not be parsed, ignoring.\n' ,
38
+ `JSON payload in \$ ${ ENV_VARIABLE } could not be parsed, ignoring.\n` ,
35
39
e
36
40
) ;
37
41
return undefined ;
38
42
}
39
43
}
40
44
41
- const fileName = process . env . FIREBASE_WEBAPP_CONFIG ;
42
45
const fileURL = pathToFileURL (
43
- isAbsolute ( fileName ) ? fileName : join ( process . cwd ( ) , fileName )
46
+ isAbsolute ( envVariable ) ? envVariable : join ( process . cwd ( ) , envVariable )
44
47
) ;
45
48
46
49
try {
47
50
const fileContents = await readFile ( fileURL , 'utf-8' ) ;
48
51
return JSON . parse ( fileContents ) ;
49
52
} catch ( e ) {
50
53
console . warn (
51
- `Contents of "${ fileName } " could not be parsed, ignoring FIREBASE_WEBAPP_CONFIG .\n` ,
54
+ `Contents of "${ envVariable } " could not be parsed, ignoring \$ ${ ENV_VARIABLE } .\n` ,
52
55
e
53
56
) ;
54
57
return undefined ;
55
58
}
56
59
}
57
60
58
- async function getFullConfig ( partialConfig ) {
61
+ async function getFinalConfig ( partialConfig ) {
59
62
if ( ! partialConfig ) {
60
63
return undefined ;
61
64
}
@@ -74,7 +77,7 @@ async function getFullConfig(partialConfig) {
74
77
const apiKey = partialConfig . apiKey ;
75
78
if ( ! appId || ! apiKey ) {
76
79
console . warn (
77
- `Unable to fetch Firebase config, appId and apiKey are required, ignoring FIREBASE_WEBAPP_CONFIG .`
80
+ `Unable to fetch Firebase config, appId and apiKey are required, ignoring \$ ${ ENV_VARIABLE } .`
78
81
) ;
79
82
return undefined ;
80
83
}
@@ -87,7 +90,7 @@ async function getFullConfig(partialConfig) {
87
90
} ) ;
88
91
if ( ! response . ok ) {
89
92
console . warn (
90
- `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG .`
93
+ `Unable to fetch Firebase config, ignoring \$ ${ ENV_VARIABLE } .`
91
94
) ;
92
95
console . warn (
93
96
`${ url } returned ${ response . statusText } (${ response . status } )`
@@ -101,7 +104,7 @@ async function getFullConfig(partialConfig) {
101
104
return { ...json , apiKey } ;
102
105
} catch ( e ) {
103
106
console . warn (
104
- `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG .\n` ,
107
+ `Unable to fetch Firebase config, ignoring \$ ${ ENV_VARIABLE } .\n` ,
105
108
e
106
109
) ;
107
110
return undefined ;
@@ -110,26 +113,27 @@ async function getFullConfig(partialConfig) {
110
113
111
114
function handleUnexpectedError ( e ) {
112
115
console . warn (
113
- ' Unexpected error encountered in @firebase/util postinstall script, ignoring FIREBASE_WEBAPP_CONFIG.'
116
+ ` Unexpected error encountered in @firebase/util postinstall script, ignoring \$ ${ ENV_VARIABLE } .`
114
117
) ;
115
118
console . warn ( e ) ;
116
119
process . exit ( 0 ) ;
117
120
}
118
121
119
122
getPartialConfig ( )
120
123
. catch ( handleUnexpectedError )
121
- . then ( getFullConfig )
124
+ . then ( getFinalConfig )
122
125
. catch ( handleUnexpectedError )
123
- . then ( async config => {
124
- const emulatorHosts = {
125
- firestore : process . env . FIRESTORE_EMULATOR_HOST ,
126
- database : process . env . FIREBASE_DATABASE_EMULATOR_HOST ,
127
- storage : process . env . FIREBASE_STORAGE_EMULATOR_HOST ,
128
- auth : process . env . FIREBASE_AUTH_EMULATOR_HOST
126
+ . then ( async finalConfig => {
127
+ const defaults = finalConfig && {
128
+ config : finalConfig ,
129
+ emulatorHosts : {
130
+ firestore : process . env . FIRESTORE_EMULATOR_HOST ,
131
+ database : process . env . FIREBASE_DATABASE_EMULATOR_HOST ,
132
+ storage : process . env . FIREBASE_STORAGE_EMULATOR_HOST ,
133
+ auth : process . env . FIREBASE_AUTH_EMULATOR_HOST
134
+ }
129
135
} ;
130
136
131
- const defaults = config && { config, emulatorHosts } ;
132
-
133
137
await Promise . all ( [
134
138
writeFile (
135
139
join ( __dirname , 'dist' , 'postinstall.js' ) ,
0 commit comments