-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloadEnv.js
72 lines (62 loc) · 1.99 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const dotenvFlow = require('dotenv-flow');
function loadEnv() {
// Prevent loadEnv from logging twice
let shouldLog = true;
if (process.env.ENVIRONMENT_LOADED === 'true') {
shouldLog = false;
return;
}
process.env.ENVIRONMENT_LOADED = 'true';
// Production builds won't try to read from filesystem for environment
if (process.env.NODE_ENV === 'production') {
if (shouldLog) {
console.log('Not injecting variables in production env.');
}
return;
}
const envPaths = [];
const dotEnvPath = path.join(__filename, '..', '.env');
const secretPath = path.join(__filename, '..', '.env.secrets');
try {
if (fs.existsSync(secretPath)) {
envPaths.push(secretPath);
} else {
// eslint-disable-next-line no-console
console.log('No .env.secrets file found in root directory');
}
if (fs.existsSync(dotEnvPath)) {
envPaths.push(dotEnvPath);
} else {
// eslint-disable-next-line no-console
console.log('No .env file found in root directory');
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
// Get obj of envVars from files
const environmentVars = dotenvFlow.parse(envPaths);
// Set global envs
Object.entries(environmentVars).forEach(([key, value]) => {
process.env[key] = value;
});
// overwrite envs with docker env if necessary
let dockerVariables = 0;
Object.entries(process.env).forEach(([key, value]) => {
// If a DOCKER_ variable passed in from compose, overwrite global env var with docker env var
if (typeof key === 'string' && key.split('_')[0] === 'DOCKER') {
dockerVariables += 1;
const adjustedKey = key.slice(key.indexOf('_') + 1, key.length);
process.env[adjustedKey] = value;
}
});
if (dockerVariables) {
if (shouldLog) {
console.log(`Injected ${dockerVariables} DOCKER_ prefixed envs.`);
}
}
}
module.exports = loadEnv;