Skip to content

Instantly share code, notes, and snippets.

@leoriviera
Last active April 20, 2022 21:50
Show Gist options
  • Save leoriviera/29826f3475d348dbae8a7b94610ea650 to your computer and use it in GitHub Desktop.
Save leoriviera/29826f3475d348dbae8a7b94610ea650 to your computer and use it in GitHub Desktop.
Code to set Firebase's `.runtimeconfig.json` to Firebase's environmental variables.
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