Created
January 22, 2018 17:43
-
-
Save Swivelgames/14e64dc16194a93ff48fd8d93883d173 to your computer and use it in GitHub Desktop.
Firebase Configuration Updater
This file contains 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
#!/usr/bin/env node | |
/* eslint-disable import/no-extraneous-dependencies, no-console */ | |
const path = require('path'); | |
const yargs = require('yargs'); | |
const fs = require('fs'); | |
const { spawn } = require('child_process'); | |
const argv = yargs | |
.option('f', { | |
alias: 'file', | |
demandOption: true, | |
default: path.join(__dirname, '.runtimeconfig.json'), | |
describe: 'Runtime Config File to Load', | |
type: 'string' | |
}) | |
.argv; | |
const filename = path.resolve(argv.f); | |
fs.readFile(filename, 'utf8', (err, data) => { | |
if (err) { | |
console.error(`An error occured when attempting to read the file: ${filename}`); | |
console.error(err); | |
console.error('Terminating.'); | |
process.exit(1); | |
return; | |
} | |
let config; | |
try { | |
config = JSON.parse(data); | |
} catch (e) { | |
console.error(`Unable to parse runtimeconfig. Make sure it is valid JSON: ${filename}`); | |
console.error(String(e)); | |
console.error('Terminating.'); | |
process.exit(1); | |
return; | |
} | |
const flatCfg = Object.keys(config) | |
.filter(key => typeof config[key] === 'object') | |
.reduce((retObj, key) => Object.assign({}, | |
retObj, | |
Object.keys(config[key]).reduce((deepRetObj, deepKey) => Object.assign({}, | |
deepRetObj, | |
{ [`${key}.${deepKey}`]: config[key][deepKey] } | |
), {}) | |
), {}); | |
Object.keys(flatCfg) | |
.reduce((prom, key) => ( | |
prom.then(() => new Promise((resolve, reject) => { | |
const val = String(flatCfg[key]); | |
console.info(`Setting firebase config: ${key}=${val}`); | |
const cmd = spawn('firebase', ['functions:config:set', `${key}=${val}`]); | |
let errorMsg = ''; | |
cmd.stderr.on('data', (d) => { errorMsg += d; }); | |
cmd.on('close', (code) => { | |
if (code !== 0) reject([code, `Firebase: Exited with non-zero: ${code}\n\n[Error Output]\n${errorMsg}`]); | |
resolve(code); | |
}); | |
})) | |
), Promise.resolve()) | |
.then( | |
() => { console.info('Success! Configuration Set!'); }, | |
(errors) => { console.error(JSON.stringify(errors)); } | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment