Skip to content

Instantly share code, notes, and snippets.

@jasmo2
Last active August 4, 2020 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasmo2/4e4d0a90a8d89bc596a86cd52f59cd4e to your computer and use it in GitHub Desktop.
Save jasmo2/4e4d0a90a8d89bc596a86cd52f59cd4e to your computer and use it in GitHub Desktop.
This script will upload wha is set inside `.runtimeconfig.json` * to firebase functions - jasmo2
/*
* NOTE: this file will upload wha is set inside `.runtimeconfig.json`
* to firebase functions.
*
* To run go to root in the project and run:
* cat .runtimeconfig.json | node ./parser.js
*/
const stdin = process.openStdin()
const spawn = require('child_process').spawn
function read() {
return new Promise((resolve, reason) => {
stdin.addListener("data", function (d) {
resolve(d.toString().trim())
});
});
}
function isObj(x) {
return x !== null && typeof x === 'object'
}
function parse(tree) {
const values = []
const properties = Object.keys(tree)
properties.forEach(prop => {
if (isObj(tree[prop])) {
const childrens = parse(tree[prop])
childrens.forEach(child => {
const value = prop + "." + child
values.push(value)
})
} else {
const value = prop + "=" + "\"" + tree[prop] + "\""
values.push(value)
}
})
return values
}
function runFirebaseConfigSet(properties) {
return new Promise((resolve, reject) => {
const args = ["functions:config:set"].concat(properties)
const cmd = spawn("firebase", args, { shell: true })
cmd.stdout.setEncoding('utf8')
cmd.stdout.on('data', data => { console.log(data) })
cmd.stderr.on('data', data => { console.log("Error:", cmd.stderr.toString()) })
cmd.on('close', code => {
console.log(`Exit code: ${code}`)
resolve(code)
})
})
}
read()
.then(input => {
const json = JSON.parse(input)
const properties = parse(json)
console.log("Found properties:\n", properties.map(it => "\t▷ " + it).join("\n"))
return properties
})
.then((properties) => runFirebaseConfigSet(properties))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment