-
-
Save zhigang1992/88dfa42117c9d12b5689e26451a58223 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
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