Skip to content

Instantly share code, notes, and snippets.

@zhigang1992
Created March 10, 2019 10:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhigang1992/88dfa42117c9d12b5689e26451a58223 to your computer and use it in GitHub Desktop.
Save zhigang1992/88dfa42117c9d12b5689e26451a58223 to your computer and use it in GitHub Desktop.
import * as path from 'path';
import { execSync } from 'child_process';
import * as admin from 'firebase-admin';
import devConfig from '../configs/dev.json';
import prodConfig from '../configs/prod.json';
import stagingConfig from '../configs/staging.json';
const firebaseBin = path.resolve(__dirname, '../../node_modules/.bin/firebase');
type Env = 'dev' | 'staging' | 'prod';
const projectsMap: { [key: string]: Env } = {
'mercy-dev': 'dev',
'mercy-staging': 'staging',
'mercy-prod': 'prod'
};
const storageURL = (env: Env) => {
switch (env) {
case 'staging':
return 'gs://mercy-staging.appspot.com';
case 'dev':
return 'gs://mercy-dev.appspot.com';
case 'prod':
return 'gs://mercy-prod.appspot.com';
}
};
const configFor = (env: Env): any => {
switch (env) {
case 'dev':
return devConfig;
case 'prod':
return prodConfig;
case 'staging':
return stagingConfig;
}
};
export const currentEnv = (): Env => {
return (
(process.env.DEPOLY_TO as Env) ||
projectsMap[execSync(`${firebaseBin} use`, { encoding: 'utf8' }).trim()]
);
};
export const setupFirebase = () => {
const env = currentEnv();
console.log(`Current Env: \n- \x1b[1m${env}\x1b[0m`);
admin.initializeApp({
credential: admin.credential.cert(configFor(env)),
storageBucket: storageURL(env)
});
admin.firestore().settings({ timestampsInSnapshots: true });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment