-
Notifications
You must be signed in to change notification settings - Fork 9
/
env.js
96 lines (86 loc) · 2.64 KB
/
env.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Inspired by https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/env.js
*/
const fs = require("fs");
const path = require("path");
/**
* Loads .env files in dotEnvConfigPath into process.env according to https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
* Returns env vars loaded, filtered with `envVarNameFilter`
*
* @param {String} dotEnvConfigPath
* @param {String} NODE_ENV
* @param {Array[String|RegExp]} envVarNameFilter
*/
function loadConfig(
envVarNameFilter = [/^WEB_APP_/i],
dotEnvConfigPath = process.cwd(),
NODE_ENV = process.env.NODE_ENV
) {
if (!NODE_ENV) {
throw new Error(
"The NODE_ENV environment variable is required but was not specified."
);
}
if (
!envVarNameFilter ||
(envVarNameFilter && envVarNameFilter.length === 0)
) {
throw new Error("Third argument must be an array");
}
const dotEnvPath = path.resolve(dotEnvConfigPath, ".env");
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [
`${dotEnvPath}.${NODE_ENV}.local`,
`${dotEnvPath}.${NODE_ENV}`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV !== "test" && `${dotEnvPath}.local`,
dotEnvPath
].filter(Boolean);
// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv
// https://github.com/motdotla/dotenv-expand
dotenvFiles.forEach(dotenvFile => {
if (fs.existsSync(dotenvFile)) {
require("dotenv-expand")(
require("dotenv").config({
path: dotenvFile
})
);
}
});
const raw = Object.keys(process.env)
.filter(key => filterEnvVarName(key, envVarNameFilter))
.reduce(
(env, key) => {
env[key] = process.env[key];
return env;
},
{ NODE_ENV }
);
// Stringify all values so we can feed into Webpack DefinePlugin
const stringified = {
"process.env": Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]);
return env;
}, {})
};
return { raw, stringified };
}
function filterEnvVarName(key, envVarNameFilter) {
for (const filter of envVarNameFilter) {
if (typeof filter === "string") {
return key.startsWith(filter);
}
if (filter instanceof RegExp) {
return filter.test(key);
}
return false;
}
}
module.exports = {
loadConfig
};