@@ -19,43 +19,46 @@ const { writeFile, readFile } = require('node:fs/promises');
1919const { pathToFileURL } = require ( 'node:url' ) ;
2020const { isAbsolute, join } = require ( 'node:path' ) ;
2121
22+ const ENV_VARIABLE = 'FIREBASE_WEBAPP_CONFIG' ;
23+
2224async function getPartialConfig ( ) {
23- if ( ! process . env . FIREBASE_WEBAPP_CONFIG ) {
25+ const envVariable = process . env [ ENV_VARIABLE ] ?. trim ( ) ;
26+
27+ if ( ! envVariable ) {
2428 return undefined ;
2529 }
2630
2731 // Like FIREBASE_CONFIG (admin autoinit) FIREBASE_WEBAPP_CONFIG can be
2832 // either a JSON representation of FirebaseOptions or the path to a filename
29- if ( process . env . FIREBASE_WEBAPP_CONFIG . startsWith ( '{"' ) ) {
33+ if ( envVariable . startsWith ( '{"' ) ) {
3034 try {
31- return JSON . parse ( process . env . FIREBASE_WEBAPP_CONFIG ) ;
35+ return JSON . parse ( envVariable ) ;
3236 } catch ( e ) {
3337 console . warn (
34- 'FIREBASE_WEBAPP_CONFIG could not be parsed, ignoring.\n' ,
38+ `JSON payload in \$ ${ ENV_VARIABLE } could not be parsed, ignoring.\n` ,
3539 e
3640 ) ;
3741 return undefined ;
3842 }
3943 }
4044
41- const fileName = process . env . FIREBASE_WEBAPP_CONFIG ;
4245 const fileURL = pathToFileURL (
43- isAbsolute ( fileName ) ? fileName : join ( process . cwd ( ) , fileName )
46+ isAbsolute ( envVariable ) ? envVariable : join ( process . cwd ( ) , envVariable )
4447 ) ;
4548
4649 try {
4750 const fileContents = await readFile ( fileURL , 'utf-8' ) ;
4851 return JSON . parse ( fileContents ) ;
4952 } catch ( e ) {
5053 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` ,
5255 e
5356 ) ;
5457 return undefined ;
5558 }
5659}
5760
58- async function getFullConfig ( partialConfig ) {
61+ async function getFinalConfig ( partialConfig ) {
5962 if ( ! partialConfig ) {
6063 return undefined ;
6164 }
@@ -74,7 +77,7 @@ async function getFullConfig(partialConfig) {
7477 const apiKey = partialConfig . apiKey ;
7578 if ( ! appId || ! apiKey ) {
7679 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 } .`
7881 ) ;
7982 return undefined ;
8083 }
@@ -87,7 +90,7 @@ async function getFullConfig(partialConfig) {
8790 } ) ;
8891 if ( ! response . ok ) {
8992 console . warn (
90- `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG .`
93+ `Unable to fetch Firebase config, ignoring \$ ${ ENV_VARIABLE } .`
9194 ) ;
9295 console . warn (
9396 `${ url } returned ${ response . statusText } (${ response . status } )`
@@ -101,7 +104,7 @@ async function getFullConfig(partialConfig) {
101104 return { ...json , apiKey } ;
102105 } catch ( e ) {
103106 console . warn (
104- `Unable to fetch Firebase config, ignoring FIREBASE_WEBAPP_CONFIG .\n` ,
107+ `Unable to fetch Firebase config, ignoring \$ ${ ENV_VARIABLE } .\n` ,
105108 e
106109 ) ;
107110 return undefined ;
@@ -110,26 +113,27 @@ async function getFullConfig(partialConfig) {
110113
111114function handleUnexpectedError ( e ) {
112115 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 } .`
114117 ) ;
115118 console . warn ( e ) ;
116119 process . exit ( 0 ) ;
117120}
118121
119122getPartialConfig ( )
120123 . catch ( handleUnexpectedError )
121- . then ( getFullConfig )
124+ . then ( getFinalConfig )
122125 . 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+ }
129135 } ;
130136
131- const defaults = config && { config, emulatorHosts } ;
132-
133137 await Promise . all ( [
134138 writeFile (
135139 join ( __dirname , 'dist' , 'postinstall.js' ) ,
0 commit comments