Skip to content

Instantly share code, notes, and snippets.

@zhigang1992 zhigang1992/setupFirebase.ts Secret
Created Mar 10, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.