Skip to content

Instantly share code, notes, and snippets.

@devoNOTbevo
Created July 13, 2021 17:05
Show Gist options
  • Save devoNOTbevo/c1d112d411ab8d9d69d0fe901102a9c5 to your computer and use it in GitHub Desktop.
Save devoNOTbevo/c1d112d411ab8d9d69d0fe901102a9c5 to your computer and use it in GitHub Desktop.
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];
}
@devoNOTbevo
Copy link
Author

devoNOTbevo commented Jul 13, 2021

export ENVIRONMENT=production && node configure_firebase.js

results in

console.log(functions.config().google.service_account.type) // service_account"
console.log(functions.config().common.base_url)// https://my-useful-api.com/api/v1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment