|
| 1 | +import { writeFile } from "fs/promises"; |
| 2 | + |
| 3 | +let firebaseConfig = {}; |
| 4 | +if (process.env.FIREBASE_CONFIG?.startsWith("{")) { |
| 5 | + // TODO probably want a more robust yaml parse |
| 6 | + firebaseConfig = Object.fromEntries(process.env.FIREBASE_CONFIG.match(/[^(\:\{\},)]+\:[^(,})]+/g).map(it => { |
| 7 | + const parts = it.split(":"); |
| 8 | + return [parts[0], parts.slice(1).join(":")] |
| 9 | + })); |
| 10 | +} |
| 11 | + |
| 12 | +const projectId = firebaseConfig.projectId; |
| 13 | +const appId = firebaseConfig.appId; |
| 14 | +const apiKey = firebaseConfig.apiKey; |
| 15 | + |
| 16 | +const config = projectId && appId && apiKey && await (await fetch( |
| 17 | + `https://firebase.googleapis.com/v1alpha/projects/${projectId}/apps/${appId}/webConfig`, |
| 18 | + { headers: { "x-goog-api-key": apiKey } } |
| 19 | +)).json(); |
| 20 | + |
| 21 | +if (config) { |
| 22 | + config.apiKey = apiKey; |
| 23 | +} |
| 24 | + |
| 25 | +let emulatorHosts = { |
| 26 | + firestore: process.env.FIRESTORE_EMULATOR_HOST, |
| 27 | + database: process.env.FIREBASE_DATABASE_EMULATOR_HOST, |
| 28 | + storage: process.env.FIREBASE_STORAGE_EMULATOR_HOST, |
| 29 | + auth: process.env.FIREBASE_AUTH_EMULATOR_HOST, |
| 30 | +}; |
| 31 | + |
| 32 | +if (!Object.values(emulatorHosts).filter(it => it).length) { |
| 33 | + emulatorHosts = undefined; |
| 34 | +} |
| 35 | + |
| 36 | +const defaults = (config || emulatorHosts) && { config, emulatorHosts }; |
| 37 | + |
| 38 | +await Promise.all([ |
| 39 | + writeFile("./defaults.js", `module.exports = ${JSON.stringify(defaults)}`), |
| 40 | + writeFile("./defaults.mjs", `export default ${JSON.stringify(defaults)}`), |
| 41 | +]); |
0 commit comments