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