Last active
April 25, 2023 22:36
-
-
Save basharovV/9c5c2a75c3891d7147dce3f457f06d9d to your computer and use it in GitHub Desktop.
Deploy Firebase Environment config from JSON file
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
/** | |
* 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