-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
150 lines (132 loc) · 3.57 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
const url = require('url');
let fallbacksDisabled = false;
let throwError = true;
function _value(varName, fallback) {
const value = process.env[varName];
if (value === undefined) {
if (fallback === undefined && !throwError) {
return value;
}
if (fallback === undefined) {
throw new Error(
'GetEnv.Nonexistent: ' + varName + ' does not exist ' + 'and no fallback value provided.'
);
}
if (fallbacksDisabled) {
throw new Error(
'GetEnv.DisabledFallbacks: ' +
varName +
' relying on fallback ' +
'when fallbacks have been disabled'
);
}
return '' + fallback;
}
return value;
}
const convert = {
string: function(value) {
return '' + value;
},
int: function(value) {
const isInt = value.match(/^-?\d+$/);
if (!isInt) {
throw new Error('GetEnv.NoInteger: ' + value + ' is not an integer.');
}
return +value;
},
float: function(value) {
const isInfinity = +value === Infinity || +value === -Infinity;
if (isInfinity) {
throw new Error('GetEnv.Infinity: ' + value + ' is set to +/-Infinity.');
}
const isFloat = !(isNaN(value) || value === '');
if (!isFloat) {
throw new Error('GetEnv.NoFloat: ' + value + ' is not a number.');
}
return +value;
},
bool: function(value) {
const isBool = value === 'true' || value === 'false';
if (!isBool) {
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
}
return value === 'true';
},
boolish: function(value) {
try {
return convert.bool(value);
} catch (err) {
const isBool = value === '1' || value === '0';
if (!isBool) {
throw new Error('GetEnv.NoBoolean: ' + value + ' is not a boolean.');
}
return value === '1';
}
},
url: url.parse,
};
function converter(type) {
return function(varName, fallback) {
if (typeof varName == 'string') {
// default
const value = _value(varName, fallback);
return convert[type](value);
} else {
// multibert!
return getenv.multi(varName);
}
};
}
const getenv = converter('string');
Object.keys(convert).forEach(function(type) {
getenv[type] = converter(type);
});
getenv.array = function array(varName, type, fallback, separator) {
type = type || 'string';
separator = separator || /\s*,\s*/;
if (Object.keys(convert).indexOf(type) === -1) {
throw new Error('GetEnv.ArrayUndefinedType: Unknown array type ' + type);
}
const value = _value(varName, fallback);
return value.split(separator).map(convert[type]);
};
getenv.multi = function multi(spec) {
const result = {};
for (let key in spec) {
const value = spec[key];
if (Array.isArray(value)) {
// default value & typecast
switch (value.length) {
case 1: // no default value
case 2: // no type casting
result[key] = getenv(value[0], value[1]); // dirty, when case 1: value[1] is undefined
break;
case 3: // with typecast
result[key] = getenv[value[2]](value[0], value[1]);
break;
default:
// wtf?
throw 'getenv.multi(): invalid spec';
break;
}
} else {
// value or throw
result[key] = getenv(value);
}
}
return result;
};
getenv.disableFallbacks = function() {
fallbacksDisabled = true;
};
getenv.enableFallbacks = function() {
fallbacksDisabled = false;
};
getenv.disableErrors = function() {
throwError = false;
};
getenv.enableErrors = function() {
throwError = true;
};
module.exports = getenv;