-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
151 lines (118 loc) · 4.82 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict'
var fs = require('fs')
var path = require('path')
//----------------------
// Export
//-------------------
module.exports = function (env_file, options) {
options = options || {}
if (typeof options.verbose === 'undefined') {
options.verbose = module.exports.verbose
}
if (typeof options.overwrite === 'undefined') {
options.overwrite = module.exports.overwrite
}
if (typeof options.raise === 'undefined') {
options.raise = module.exports.raise
}
module.exports.logger = options.logger || module.exports.logger
if (typeof env_file !== 'string') {
if (options.raise) {
throw new TypeError("Environment file argument is not a valid `String`: " + env_file)
} else {
if (options.verbose && module.exports.logger) {
module.exports.logger.error('[ENV]: ERROR Environment file argument is not a valid `String`:', process.env.ENV_FILE)
}
return {}
}
}
try {
env_file = process.env.ENV_FILE = path.resolve(env_file) || process.env.ENV_FILE
} catch (err) {
if (options.raise) {
throw new TypeError("Environment file path could not be resolved: " + err)
} else {
if (options.verbose && module.exports.logger) {
module.exports.logger.error('[ENV]: ERROR Environment file path could not be resolved:', process.env.ENV_FILE)
}
return {}
}
}
module.exports.data = module.exports.data || {}
module.exports.data[env_file] = {}
if (options.verbose && module.exports.logger) {
module.exports.logger.info('[ENV]: Loading environment:', env_file)
}
if (fs.existsSync(env_file)) {
var lines
try {
lines = (fs.readFileSync(env_file, 'utf8') || '')
.split(/\r?\n|\r/)
.filter(function (line) {
return /\s*=\s*/i.test(line)
})
.map(function (line) {
return line.replace('exports ', '')
})
} catch (err) {
if (options.raise) {
throw new TypeError("Environment file could not be read: " + err)
} else {
if (options.verbose && module.exports.logger) {
module.exports.logger.error('[ENV]: ERROR Environment file could not be read:', env_file)
}
return {}
}
}
module.exports.lines = module.exports
module.exports.lines.comments = []
module.exports.lines.variables = []
var is_comment
lines.forEach(function(line) {
is_comment = /^\s*\#/i.test(line) // ignore comment lines (starting with #).
if (is_comment) {
module.exports.lines.comments.push(line)
if (options.verbose && module.exports.logger) {
module.exports.logger.info('[ENV]: Ignored line:', line)
}
} else {
module.exports.lines.variables.push(line)
var key_value = line.match(/^([^=]+)\s*=\s*(.*)$/)
var env_key = key_value[1]
// remove ' and " characters if right side of = is quoted
var env_value = key_value[2].match(/^(['"]?)([^\n]*)\1$/m)[2]
// overwrite already defined `process.env.*` values?
if (!!options.overwrite) {
module.exports.data[env_file][env_key] = env_value
if (options.verbose && module.exports.logger && module.exports.data[env_file][env_key] !== env_value) {
module.exports.logger.info('[ENV]: Overwritten ', module.exports.data[env_file][env_key], ' => ', env_value)
}
} else {
module.exports.data[env_file][env_key] = process.env[env_key] || env_value
}
process.env[env_key] = module.exports.data[env_file][env_key]
if (options.verbose && module.exports.logger) {
module.exports.logger.info('[ENV]:', module.exports.data[env_file])
}
}
})
}
else {
if (options.raise) {
throw new TypeError("Environment file doesn't exist: " + env_file)
} else {
if (options.verbose && module.exports.logger) {
module.exports.logger.error('[ENV]: ERROR Environment file path could not be resolved:', env_file)
}
return {}
}
}
return module.exports.data[env_file]
}
module.exports.log = false
module.exports.logger = console
module.exports.overwrite = false
module.exports.raise = true
module.exports.data = {}
module.exports.sync = module.exports
// TODO: module.exports.async = module.exports.async