-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl-env.js
178 lines (148 loc) · 3.04 KB
/
ctrl-env.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict'
class CtrlEnv {
constructor(
variables,
options = {},
) {
this._keys = []
const {
prefix = '',
separator = '_',
} = options
this.variables = variables
this.prefix = prefix
this.separator = separator
}
reduceVariables(
result,
envVar,
) {
const failure = this.check(envVar)
if (!failure) {
return result
}
const [
type,
message,
value,
] = failure
const fullMessage = `${message}: ${envVar[0]}${value ? ` ${value}` : ''}`
if (type === CtrlEnv.raise.warning) {
result.warnings.push(fullMessage)
return result
}
result.errors.push(fullMessage)
return result
}
check(envVar) {
const raise = CtrlEnv.raise
let warning = false
const [
key,
options = {},
] = envVar
const {
required = true,
prefixed = true,
values,
type,
} = options
const prefix = prefixed
? `${this.prefix}${this.prefix ? this.separator : ''}`
: ''
const fullKey = `${prefix}${key}`
let value = process.env[fullKey]
if (!value) {
return required
? [
raise.error,
raise.error.MISSING,
]
: [
raise.warning,
raise.warning.MISSING,
]
}
switch (type) {
case undefined:
case 'string':
break
case 'integer': {
const parsed = Number.parseInt(value)
if (parsed.toString() !== value) {
return [
raise.error,
raise.error.INVALID,
value,
]
}
value = parsed
} break
case 'float':
case 'number':
value = Number.parseFloat(value)
if (Number.isNaN(value)) {
return [
raise.error,
raise.error.INVALID,
value,
]
}
break
default: warning = [
raise.warning,
raise.warning.UNSUPPORTED_TYPE,
type,
]
}
if (Array.isArray(values) && !values.includes(value)) {
return [
raise.error,
raise.error.INVALID,
value,
]
}
this._keys.push(key)
Object.defineProperty(this, key, {
get: () => value,
})
return warning
}
assert() {
const assertions = this.variables.reduce(
this.reduceVariables.bind(this),
{
errors: [],
warnings: [],
},
)
const {
errors,
warnings,
} = assertions
if (warnings.length > 0) {
warnings.forEach((warning) => console.warn(warning))
}
if (errors.length > 0) {
throw new Error(errors.join('\n'))
}
return assertions
}
get all() {
return this._keys.reduce(
(result, key) => (result[key] = this[key], result),
{},
)
}
}
CtrlEnv.raise = {
error: {
INVALID: 'Invalid value',
MISSING: 'Missing required key',
},
warning: {
MISSING: 'Missing optional key',
UNSUPPORTED_TYPE: 'Unsupported type check',
},
}
module.exports = CtrlEnv