Skip to content

Instantly share code, notes, and snippets.

@basharovV
Last active April 25, 2023 22:36
Show Gist options
  • Save basharovV/9c5c2a75c3891d7147dce3f457f06d9d to your computer and use it in GitHub Desktop.
Save basharovV/9c5c2a75c3891d7147dce3f457f06d9d to your computer and use it in GitHub Desktop.
Deploy Firebase Environment config from JSON file
/**
* Deploy Firebase Environment Variables config from a JSON file
*
* Before using this script, make sure that you've set the right Firebase project to deploy to
* firebase use <alias>
*
* Example usage: node set_firebase_config.js exampleConfig.json
*/
var fs = require('fs');
var util = require('util');
var exec = require('child_process').exec;
var setCmd = "firebase functions:config:set ";
var getCmd = "firebase functions:config:get";
const generateSetCommand = (prevKey, obj) => {
Object.keys(obj).forEach((key, idx) => {
var val = obj[key];
if (prevKey === null) generateSetCommand(`${key}`, val);
else if (typeof val === 'object') generateSetCommand(`${prevKey}.${key}`, val);
else return setCmd += ` ${prevKey}.${key}="${val}"`;
})
}
const readConfigFromFile = (filePath) =>
new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, contents) => {
resolve(JSON.parse(contents));
});
});
const setFirebaseConfig = (config) => {
generateSetCommand(null, config);
console.log("Command generated ✔︎ Deploying config...\n")
console.log(`${setCmd}\n`);
exec(setCmd, (err, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (err != null) {
console.log(err);
}
})
}
const args = process.argv.slice(2)
const filePath = args[0];
// Deploy the environment variables to the current Firebase alias project
readConfigFromFile(filePath)
.then(setFirebaseConfig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment