-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenvs.js
46 lines (43 loc) · 1021 Bytes
/
envs.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
const _ = require('lodash')
const fs = require('fs')
const separateEnvsFromString = str =>
_.reduce(
str.split(' '),
(task, substr, index, array) => {
if (/^([a-zA-Z]{1,2}[a-zA-Z_0-9]{1,})=([a-zA-Z0-9]{1,})$/.test(substr)) {
_.assign(task.envs, _.fromPairs([substr.split('=')]))
} else {
task.string.push(substr)
}
if (index === array.length - 1) {
task.string = task.string.join(' ')
}
return task
},
{
string: [],
envs: {},
}
)
const getDataFromEnvFile = filename => {
let envFile = {}
try {
const data = fs.readFileSync(filename)
envFile = _.reduce(
_.compact(data.toString().split(/[\n\r]/g)),
(envFile, line) => {
const variable = line.split('=')
envFile[variable[0]] = variable[1]
return envFile
},
{}
)
} catch (e) {
console.warn('.ENV file not found')
}
return envFile
}
module.exports = {
separateEnvsFromString,
getDataFromEnvFile,
}