forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot-env.js
More file actions
34 lines (29 loc) · 1.04 KB
/
Copy pathdot-env.js
File metadata and controls
34 lines (29 loc) · 1.04 KB
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
const path = require('path')
const process = require('process')
const dotenv = require('dotenv')
const filterObject = require('filter-obj')
const { isFileAsync, readFileAsync } = require('../lib/fs')
const loadDotEnvFiles = async function ({ projectDir, warn }) {
const dotenvFiles = ['.env.development', '.env']
const results = await Promise.all(
dotenvFiles.map(async (file) => {
const filepath = path.resolve(projectDir, file)
try {
const isFile = await isFileAsync(filepath)
if (!isFile) {
return
}
} catch (error) {
warn(`Failed reading env variables from file: ${filepath}: ${error.message}`)
return
}
const content = await readFileAsync(filepath)
const parsed = dotenv.parse(content)
// only keep envs not configured in process.env
const env = filterObject(parsed, (key) => !Object.prototype.hasOwnProperty.call(process.env, key))
return { file, env }
}),
)
return results.filter(Boolean)
}
module.exports = { loadDotEnvFiles }