-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
env.js
57 lines (48 loc) · 1.46 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
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
const validate = require('dotenv-validator').default;
const example = fs.readFileSync(
path.resolve(__dirname, '.env.example'),
'utf-8'
);
const { envRules, envDefault } = example.split('\n').reduce(
(env, line) => {
const [nameSlug, valueAndDescriptors] = line.split('=');
const name = (nameSlug || '').trim();
if (!name) {
return env;
}
const [valueSlug, descriptorsSlug] = (valueAndDescriptors || '').split('#');
const value = (valueSlug || '').trim();
const descriptors = (descriptorsSlug || '')
.trim()
.replace('#')
.split(',')
.map((d) => d.toLowerCase().trim());
const isRequired = !descriptors.includes('optional');
env.envDefault[name] = isRequired ? value : '';
env.envRules[name] = {
required: isRequired,
};
return env;
},
{ envRules: {}, envDefault: {} }
);
/**
* Loads `.env` file contents into process.env.
*
* See https://docs.dotenv.org
*
* @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', debug: true, override: false }`
* @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
*
*/
function config(options) {
const envParsed = dotenv.config(options).parsed;
validate({ envParsed, envDefault, envRules });
return envParsed;
}
module.exports = {
config,
};