feat(api): refactor startup env var check

This commit is contained in:
Lars Hampe 2024-10-07 14:29:49 +02:00
parent 00fe6eafcd
commit 92fe2fb32d

View File

@ -8,30 +8,48 @@ declare module 'bun' {
CLERK_WEBHOOK_SECRET: string
CLERK_SECRET_KEY: string
CLERK_PUBLISHABLE_KEY: string
BETTERSTACK_LOG_TOKEN: string
}
}
const TEST_VARIABLES = ['POSTGRES_URL']
const DEVELOPMENT_VARIABLES = [
...TEST_VARIABLES,
'CLERK_WEBHOOK_SECRET',
'CLERK_SECRET_KEY',
'CLERK_PUBLISHABLE_KEY',
]
const PRODUCTION_VARIABLES = [...DEVELOPMENT_VARIABLES, 'BETTERSTACK_LOG_TOKEN']
export const startup = async () => {
if (import.meta.env.NODE_ENV === 'test') {
if (!import.meta.env.POSTGRES_URL) {
logger.error('Env Var POSTGRES_URL is missing!')
process.exit(0)
}
return
TEST_VARIABLES.map((key) => {
if (!import.meta.env[key]) {
logger.error(`Env Var ${key} is missing!`)
process.exit(0)
}
})
}
if (import.meta.env.NODE_ENV === 'development') {
DEVELOPMENT_VARIABLES.map((key) => {
if (!import.meta.env[key]) {
logger.error(`Env Var ${key} is missing!`)
process.exit(0)
}
})
}
if (import.meta.env.NODE_ENV === 'production') {
PRODUCTION_VARIABLES.map((key) => {
if (!import.meta.env[key]) {
logger.error(`Env Var ${key} is missing!`)
process.exit(0)
}
})
}
const keys = [
'POSTGRES_URL',
'CLERK_WEBHOOK_SECRET',
'CLERK_SECRET_KEY',
'CLERK_PUBLISHABLE_KEY',
'BETTERSTACK_LOG_TOKEN',
]
keys.map((key) => {
if (!import.meta.env[key]) {
logger.error(`Env Var ${key} is missing!`)
process.exit(0)
}
})
if (import.meta.env.NODE_ENV === 'production') {
await migrateDatabase('migrations')