|
| 1 | +import { writeFile, readFile } from "node:fs/promises"; |
| 2 | +import { pathToFileURL } from "node:url"; |
| 3 | +import { isAbsolute, join } from "node:path"; |
| 4 | +import type { FirebaseOptions } from "@firebase/app"; |
| 5 | + |
| 6 | +async function getWebConfig(): Promise<FirebaseOptions|undefined> { |
| 7 | + // $FIREBASE_WEBAPP_CONFIG can be either a JSON representation of FirebaseOptions or the path |
| 8 | + // to a filename |
| 9 | + if (!process.env.FIREBASE_WEBAPP_CONFIG) { |
| 10 | + return undefined; |
| 11 | + } |
| 12 | + |
| 13 | + let configFromEnvironment: Partial<FirebaseOptions>|undefined = undefined; |
| 14 | + if (process.env.FIREBASE_WEBAPP_CONFIG.startsWith("{\"")) { |
| 15 | + try { |
| 16 | + configFromEnvironment = JSON.parse(process.env.FIREBASE_WEBAPP_CONFIG); |
| 17 | + } catch(e) { |
| 18 | + console.error("FIREBASE_WEBAPP_CONFIG could not be parsed.", e); |
| 19 | + } |
| 20 | + } else { |
| 21 | + const fileName = process.env.FIREBASE_WEBAPP_CONFIG; |
| 22 | + const fileURL = pathToFileURL(isAbsolute(fileName) ? fileName : join(process.cwd(), fileName)); |
| 23 | + const fileContents = await readFile(fileURL, "utf-8").catch((err) => { |
| 24 | + console.error(err); |
| 25 | + }); |
| 26 | + if (fileContents) { |
| 27 | + try { |
| 28 | + configFromEnvironment = JSON.parse(fileContents); |
| 29 | + } catch(e) { |
| 30 | + console.error(`Contents of ${fileName} could not be parsed.`, e); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // In Firebase App Hosting the config provided to the environment variable is up-to-date and |
| 36 | + // "complete" we should not reach out to the webConfig endpoint to freshen it |
| 37 | + if (process.env.X_GOOGLE_TARGET_PLATFORM === "fah") { |
| 38 | + return configFromEnvironment as FirebaseOptions; |
| 39 | + } |
| 40 | + |
| 41 | + if (!configFromEnvironment) { |
| 42 | + return undefined; |
| 43 | + } |
| 44 | + const projectId = configFromEnvironment.projectId || "-"; |
| 45 | + const appId = configFromEnvironment.appId; |
| 46 | + const apiKey = configFromEnvironment.apiKey; |
| 47 | + if (!appId || !apiKey) { |
| 48 | + return undefined; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + const response = await fetch( |
| 53 | + `https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`, |
| 54 | + { headers: { "x-goog-api-key": apiKey } } |
| 55 | + ); |
| 56 | + if (!response.ok) { |
| 57 | + return undefined; |
| 58 | + } |
| 59 | + const json = await response.json(); |
| 60 | + return { ...json, apiKey }; |
| 61 | + } catch(e) { |
| 62 | + return undefined; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +getWebConfig().then(async (config) => { |
| 67 | + |
| 68 | + const emulatorHosts = Object.entries({ |
| 69 | + firestore: process.env.FIRESTORE_EMULATOR_HOST, |
| 70 | + database: process.env.FIREBASE_DATABASE_EMULATOR_HOST, |
| 71 | + storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST, |
| 72 | + auth: process.env.FIREBASE_AUTH_EMULATOR_HOST, |
| 73 | + }).reduce( |
| 74 | + // We want a falsy value if none of the above are defined |
| 75 | + (current, [key, value]) => value ? { ...current, [key]: value } : current, |
| 76 | + undefined as Record<string,string>|undefined |
| 77 | + ); |
| 78 | + |
| 79 | + // getDefaults() will use this object, rather than fallback to other autoinit suppliers, if it's |
| 80 | + // truthy—if we've done nothing here, make it falsy. |
| 81 | + const defaults = (config || emulatorHosts) ? { config, emulatorHosts } : undefined; |
| 82 | + |
| 83 | + await Promise.all([ |
| 84 | + writeFile(join(__dirname, "autoinit_env.js"), `module.exports = ${JSON.stringify(defaults)}`), |
| 85 | + writeFile(join(__dirname, "autoinit_env.mjs"), `export default ${JSON.stringify(defaults)}`), |
| 86 | + ]); |
| 87 | + |
| 88 | + process.exit(0); |
| 89 | + |
| 90 | +}); |
0 commit comments