-
Notifications
You must be signed in to change notification settings - Fork 12
/
loadEnv.js
49 lines (41 loc) · 1.28 KB
/
loadEnv.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
/* eslint-disable @typescript-eslint/no-var-requires */
const nodePath = require('path');
const fs = require('fs');
const dotenv = require('dotenv');
const findEnvDir = dir => {
if (fs.existsSync(nodePath.resolve(dir, '.env'))) {
return dir;
}
try {
return findEnvDir(nodePath.resolve(dir, '..'));
} catch (e) {
console.error('Could not find env dir', e.message);
return null;
}
};
function loadEnv({ encoding, debug } = {}) {
process.env.FACET = process.env.FACET || 'dismoi';
const FACET = process.env.FACET;
const NODE_ENV = process.env.NODE_ENV || 'development';
const envDir = findEnvDir(__dirname);
process.env.ROOT_DIR = envDir;
const dotenvFiles = [
`.env.${NODE_ENV}.${FACET}.local`,
`.env.${NODE_ENV}.local`,
`.env.${NODE_ENV}.${FACET}`,
`.env.${FACET}`,
`.env.${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' && '.env.local',
'.env'
].filter(Boolean);
dotenvFiles.forEach(dotenvFile => {
const envPath = nodePath.resolve(envDir, dotenvFile);
if (envPath && fs.existsSync(envPath)) {
dotenv.config({ path: envPath, encoding, debug });
}
});
}
module.exports = loadEnv;