Created
July 13, 2021 17:05
-
-
Save devoNOTbevo/c1d112d411ab8d9d69d0fe901102a9c5 to your computer and use it in GitHub Desktop.
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
const { exec } = require('child_process'); | |
const { writeFileSync } = require('fs'); | |
const environment = process.env.ENVIRONMENT || 'development'; | |
// test environment configuration | |
const configuration = { | |
development: { | |
common: { | |
base_url: 'http://localhost:5001/api/v1', | |
}, | |
facebook: { | |
client_id: '123456', | |
client_secret: '999999999999', | |
}, | |
google: { | |
client_id: '123456', | |
client_secret: '999999999999', | |
}, | |
}, | |
production: { | |
common: { | |
base_url: 'https://my-useful-api.com/api/v1', | |
}, | |
facebook: { | |
client_id: 'dfaDFKJkihag082nja0f8h', | |
client_secret: 'SUPER_SECRET', | |
}, | |
google: { | |
client_id: 'DF#*DKNFLNJ232DF', | |
client_secret: 'ALSO_SUPER_SECRET', | |
service_account: { | |
type: 'service_account', | |
project_id: 'sample', | |
private_key_id: '12f21d3233a25127b9080987c5', | |
private_key: | |
'-----BEGIN PRIVATE KEY-----\nMASDLFJALdmjdnfSODHJFAfnahfa;lmkdl;kjasdJDOJD\n-----END PRIVATE KEY-----\n', | |
}, | |
}, | |
}, | |
}; | |
const environmentVariableObject = configuration[environment] || {}; | |
const services = Object.keys(environmentVariableObject); | |
console.log(`Configuring environment for ${environment}`); | |
if (!services || services.length === 0) { | |
console.warn( | |
`Warning: no configuration for current environment: ${environment}` | |
); | |
process.exit(0); | |
} | |
// firebase emulators use .runtimeconfig.json within the functions source | |
if (environment === 'development') { | |
writeFileSync( | |
'./.runtimeconfig.json', | |
JSON.stringify(environmentVariableObject, null, 2) | |
); | |
process.exit(0); | |
} | |
// change object to dot notation paths with values | |
const [paths, values] = pathify(environmentVariableObject); | |
// now set these values using the firebase functions config | |
paths.forEach((variablePath, idx) => { | |
const value = values[idx]; | |
if (value === null) { | |
return; | |
} | |
exec( | |
`firebase functions:config:set ${variablePath}="${value}"`, | |
(err, stdout, stderr) => { | |
if (err) { | |
console.log(`error: ${err.message}`); | |
} | |
if (stderr) { | |
console.log(`stderr: ${stderr}`); | |
return; | |
} | |
console.log(`stdout: ${stdout} - ${variablePath}="${value}"`); | |
} | |
); | |
}); | |
/* ---- UTILITY FUNCTIONS --- */ | |
// https://stackoverflow.com/a/51663539/5472574 | |
// recursive function to change an object into a | |
// tuple - first element is an array of dot notation "paths" | |
// second element is an array of the values for | |
// terminal properties(null for non - terminal) | |
function pathify(o, res = [], path = [], values = []) { | |
for (const dir in o) { | |
const s = path.join('.'); | |
const isObject = typeof o[dir] === 'object'; | |
// console.log(s, path, dir, path.concat(dir), res); | |
if (s) { | |
res.push(`${s}.${dir}`); | |
} else { | |
res.push(dir); | |
} | |
if (isObject) { | |
// non-terminal matches to a null value | |
values.push(null); | |
pathify(o[dir], res, path.concat(dir), values); | |
} else { | |
// terminal matches to the terminal properties value | |
values.push(o[dir]); | |
} | |
} | |
return [res, values]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
results in