|
1
|
import dotenv from "dotenv" |
|
2
|
import { expand } from "dotenv-expand" |
|
3
|
import { join } from "path" |
|
4
|
const KNOWN_ENVIRONMENTS = ["staging", "production", "test"] |
|
5
|
|
|
6
|
/** |
|
7
|
* Loads ".env" file based upon the environment in which the |
|
8
|
* app is running. |
|
9
|
* |
|
10
|
* - Loads ".env" file by default. |
|
11
|
* - Loads ".env.staging" when "environment=staging". |
|
12
|
* - Loads ".env.production" when "environment=production". |
|
13
|
* - Loads ".env.test" when "environment=test". |
|
14
|
* |
|
15
|
* The ".env" file is always loaded alongside the environment |
|
16
|
* specific .env file. |
|
17
|
* |
|
18
|
* This method does not return any value and updates the "process.env" |
|
19
|
* object instead. |
|
20
|
*/ |
|
21
|
export function loadEnv(environment: string, envDir: string) { |
|
22
|
const filesToLoad = KNOWN_ENVIRONMENTS.includes(environment) |
|
23
|
? [`.env.${environment}`, ".env"].map((file) => join(envDir, file)) |
|
24
|
: [join(envDir, ".env")] |
|
25
|
try { |
|
26
|
expand(dotenv.config({ path: filesToLoad })) |
|
27
|
} catch {} |
|
28
|
} |