-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
60 lines (51 loc) · 1 KB
/
index.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
58
59
60
var fs = require('fs')
function run (path) {
var file = path || '.env'
var res = out(file)
console.log(res)
}
function out (file) {
return lines(split(read(file)))
}
function outText (text) {
return lines(split(text))
}
function read (file) {
return fs.readFileSync(file, 'utf8')
}
function split (text) {
return text.trim().split(/[\n\r]+/)
}
function lines (ls) {
var vars = ls.map(line)
var out = []
vars.forEach(function (v) {
if (!v) return
out.push('export ' + v.name + '=' + v.value)
})
return out.join('\n')
}
function addEscapes (s) {
return s.replace(/([`${}])/g, '\\$1')
}
function line (l) {
var com = /^\s*#/
if (com.test(l)) return
var rex = /^\s*(export\s+)?([\w\.\-]+)\s*=\s*(.*)?\s*$/
var m = l.match(rex)
if (m && m[2]) {
return {
name: addEscapes(m[2]).trim(),
value: m[3] ? addEscapes(m[3]).trim() : ''
}
}
}
module.exports = {
run: run,
out: out,
outText: outText,
read: read,
split: split,
lines: lines,
line: line
}