-
Notifications
You must be signed in to change notification settings - Fork 0
/
envLoader.ts
31 lines (26 loc) · 1.12 KB
/
envLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Loads the correct env vars
* based on APP_CONTEXT set via package.json or netlify.toml (backend functions)
*
* @param process.env.APP_CONTEXT set via package.json
* @throw Error
*/
import dotenv from 'dotenv'
export function envLoad(): void {
let envFilename = 'development'
const appContext: string | undefined = process.env.APP_CONTEXT
// console.log('appContext', appContext)
if (!appContext)
// same as (appContext == null)
throw new ReferenceError('process.env.APP_CONTEXT is not defined')
if (appContext === 'production') return // dont load for production as pro. env is stored on deployment UI
if (appContext === 'stage') envFilename = 'stage'
// appContext=test.. use development env by default
// if (appContext.includes('test')) envFilename = 'development' // ensures that appContext=test:integrations sources development env
// load env from file into node's process.env
const envParseResult: any = dotenv.config({ path: '.env.' + envFilename })
if (envParseResult.error) {
throw envParseResult.error
}
// console.log('new process.env with added env from file', process.env)
}