-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
49 lines (36 loc) · 1.35 KB
/
main.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
const chalk = require('chalk')
const envLiteral = (debug) => {
const interpolate = envValue => {
const matches = envValue.match(/(.?\${?(?:[a-zA-Z0-9_]+)?}?)/g) || []
return matches.reduce((newEnv, match) => {
const parts = /(.?)\${?([a-zA-Z0-9_]+)?}?/g.exec(match)
const prefix = parts[1]
let value, replacePart
if (prefix === '\\') {
replacePart = parts[0]
value = replacePart.replace('\\$', '$')
} else {
const key = parts[2]
replacePart = parts[0].substring(prefix.length)
if (process.env.hasOwnProperty(key)) {
value = process.env[key]
} else {
value = ''
if (debug) {
console.error(chalk.bgRedBright.black(
` env Literal => env var ${key} not defined `
))
}
}
// Resolve recursive interpolations
value = interpolate(value)
}
return newEnv.replace(replacePart, value)
}, envValue)
}
const vars = process.env
Object.entries(vars).forEach(([key, value]) => {
process.env[key] = interpolate(value)
})
}
module.exports = envLiteral