-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
108 lines (91 loc) · 2.09 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const fs = require('fs')
const path = require('path')
const root = process.cwd()
const FUCK_ENV_DEFAULT = '.env,default.env,package.json'
const EOF = '\n'
const ENV_VALUE_RE = /^(\s*[$\w]+\s*=\s*)(.*)?(\s*)$/
const ENV_PAIR_RE = /^\s*([$\w]+)\s*=\s*(.*)?\s*$/
const LINEFEED_RE = /\\n/gm
const QUOTES_RE = /(^['"]|['"]$)/g
/**
* Get files from environment variables
*/
function getFiles () {
const FUCK_ENV = process.env.FUCK_ENV
|| process.env.npm_package_config_FUCK_ENV
|| FUCK_ENV_DEFAULT
return FUCK_ENV.split(',').map(file => path.resolve(root, file))
}
const entries = function * entries (object) {
if (!object) {
return
}
const keys = Object.keys(object)
let key
while ((key = keys.shift())) {
yield [key, object[key]]
}
}
/**
* Parse an env string into an object
*/
function * parse (raw) {
for (const line of raw.split(EOF)) {
const matched = line.match(ENV_PAIR_RE)
if (matched === null) {
yield [undefined, undefined, line]
continue
}
const key = matched[1]
let value = matched[2] || ''
// Expand newlines in quoted values
const length = value.length
if (length !== 0
&& value.charAt(0) === '"'
&& value.charAt(length - 1) === '"'
) {
value = value.replace(LINEFEED_RE, EOF)
}
value = value.replace(QUOTES_RE, '')
yield [key, value, line]
}
}
function isFilePackage (file) {
return file.endsWith('package.json')
}
function loadFile (file) {
return fs.readFileSync(file, 'utf-8')
}
function loadJson (file) {
return require(file)
}
function prettify (env) {
const list = []
for (const key of Object.keys(env)) {
let value = env[key]
if (typeof value !== 'string') {
if (Array.isArray(value)
|| (value !== null && typeof value === 'object')
) {
value = JSON.stringify(value)
} else {
value = String(value)
}
}
list.push(`${key}=${value}`)
}
list.sort()
return list.join(EOF)
}
module.exports = {
root,
EOF,
ENV_VALUE_RE,
getFiles,
entries,
parse,
isFilePackage,
loadFile,
loadJson,
prettify,
}