Last active
April 20, 2022 21:50
-
-
Save leoriviera/29826f3475d348dbae8a7b94610ea650 to your computer and use it in GitHub Desktop.
Code to set Firebase's `.runtimeconfig.json` to Firebase's environmental variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync, writeFileSync } from "fs"; | |
/** | |
* A function which traverses and object, retrieving keys and values. | |
* @param {Object} o the object to be traversed | |
* @param {string} p the dot notation for the object, o | |
* @returns {string[]} the keys and values of the object | |
*/ | |
const traverse = (o, p = "") => { | |
return Object.entries(o) | |
.map(([k, v]) => { | |
if (typeof v === "object") { | |
return traverse(v, `${p}${k}_`); | |
} | |
return `${(p + k).toUpperCase()}="${v}"`; | |
}) | |
.flat(Infinity); | |
}; | |
console.log("Parsing config.json"); | |
const runtimeConfig = JSON.parse(readFileSync("config.json")); | |
console.log("Traversing config"); | |
const runtimeConfigKeyValues = traverse(runtimeConfig); | |
console.log("Writing .env file"); | |
writeFileSync(".env", runtimeConfigKeyValues.join("\n") + "\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment