-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
41 lines (36 loc) · 1008 Bytes
/
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
const fs = require('fs');
const envFilePaths = [
require.main.path,
process.env.PWD,
process.env.INIT_CWD,
];
const envFiles = [];
for (const path of envFilePaths) {
if (!envFiles.includes(`${path}/.env`)) envFiles.push(`${path}/.env`);
}
for (const envFile of envFiles) {
if (!fs.existsSync(envFile)) continue;
fs.readFileSync(envFile, 'utf8')
.replace(/\r/g, '')
.split('\n')
.filter((line) => line && !line.startsWith('#'))
.map((line) => line.split('='))
.forEach(([key, ...value]) => {
process.env[key] = value.join('=');
});
console.log(`Loaded env file: '${envFile}'`);
}
/**
* Get an environment variable.
* @param {string} key The environment variable key.
* @param {string} defVal The default value.
*/
const get = (key, defVal = undefined) => {
const value = process.env[key] ?? defVal;
if (value === undefined) {
console.error(`Missing environment variable '${key}'`);
process.exit(1);
}
return value;
};
module.exports = get;