Skip to content

Commit

Permalink
fix: validate env values on build
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Nov 27, 2023
1 parent d95e2bc commit 24ce787
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ import packageJson from './package.json' assert { type: 'json' }
dotenv.config()
const outputDirectory = 'dist'

function getEnv(key, defaultValue) {
const value = process.env[key]

if (!value && !defaultValue) {
throw new Error(`Missing environment variable ${key}`)
}

if (!value) {
console.warn(`Missing environment variable "${key}". Using default value: ${defaultValue}`)

return defaultValue
}

return value
}

function makeConfig(opts, entryFile, artifactName, functionJsonPath, transformFunctionJson) {
const isDev = opts.watch

Expand All @@ -33,8 +49,8 @@ function makeConfig(opts, entryFile, artifactName, functionJsonPath, transformFu
})

const env = {
fpcnd: process.env.FPCDN ?? 'fpcdn.io',
ingressApi: process.env.INGRESS_API ?? 'api.fpjs.io',
fpcdn: getEnv('FPCDN', 'fpcdn.io'),
ingressApi: getEnv('INGRESS_API', 'api.fpjs.io'),
}

/**
Expand Down Expand Up @@ -74,7 +90,7 @@ function makeConfig(opts, entryFile, artifactName, functionJsonPath, transformFu
commonjs(),
nodeResolve({ preferBuiltins: false, exportConditions: ['node'] }),
replace({
__FPCDN__: env.fpcnd,
__FPCDN__: env.fpcdn,
__INGRESS_API__: env.ingressApi,
__azure_function_version__: packageJson.version,
preventAssignment: true,
Expand Down

0 comments on commit 24ce787

Please sign in to comment.