-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmodule.js
42 lines (35 loc) · 1.05 KB
/
module.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
const { readFileSync, accessSync, constants } = require('fs')
const { join } = require('path')
const { parse } = require('dotenv')
const logger = require('./logger')
module.exports = function (moduleOptions) {
const options = {
only: null,
path: this.options.srcDir,
filename: '.env',
systemvars: false,
...this.options.dotenv,
...moduleOptions
}
const envFilePath = join(options.path, options.filename)
try {
accessSync(envFilePath, constants.R_OK)
} catch (err) {
logger.warn(`No \`${options.filename}\` file found in \`${options.path}\`.`)
return
}
const envConfig = parse(readFileSync(envFilePath))
if (options.systemvars) {
Object.keys(process.env).forEach((key) => {
if (!(key in envConfig)) {
envConfig[key] = process.env[key]
}
})
}
Object.keys(envConfig).forEach((key) => {
if (!Array.isArray(options.only) || options.only.includes(key)) {
this.options.env[key] = this.options.env[key] || envConfig[key]
}
})
}
module.exports.meta = require('../package.json')