-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
326 lines (281 loc) · 10.8 KB
/
index.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
declare const process: any;
/**
* Explicit any (for places that truly take any type - i.e. not a placeholder)
*/
type Any = any;
/**************************************************************************************************/
// WARNING: DO NOT TOUCH THIS BLOCK! It's required in this exact form to work with Webpack
// Without each var explicitly defined with in "process.env.VARIABLE_NAME" format,
// Webpack can't inject the properties' values for use in a browser environment
const RAW_NODE_ENV = process.env.NODE_ENV;
const RAW_LOG_LEVEL = process.env.LOG_LEVEL;
const RAW_RELEASE_ENV = process.env.RELEASE_ENV;
const RAW_TEST_MODE = process.env.TEST_MODE;
const RAW_IE_COMPAT = process.env.IE_COMPAT;
const RAW_AVOID_WEB = process.env.AVOID_WEB;
const RAW_LOADED_MOCHA_OPTS = process.env.LOADED_MOCHA_OPTS;
const RAW_mocha = process.env.mocha;
const RAW_TEST_SECURITY = process.env.TEST_SECURITY;
const RAW_SECURITY_TEST = process.env.SECURITY_TEST;
const RAW_IS_LOCAL = process.env.IS_LOCAL;
const RAW_SKIP_BASIC_AUTH = process.env.SKIP_BASIC_AUTH;
/**************************************************************************************************/
/****************************************** TYPE EXPORTS ******************************************/
/**
* Accepted node environments (NODE_ENV values), including short forms:
* development | dev | production | prod
*
* Used by e.g. Express & React to determine whether to activate optimizations
*/
export type NodeEnv = 'development' | 'dev' | 'production' | 'prod';
/**
* Accepted node environments (NODE_ENV values), excluding short forms:
* development | production
*
* Used by e.g. Express & React to determine whether to activate optimizations
*/
export type NodeEnvFull = 'development' | 'production';
/**
* Accepted commonly-used release environment names (short- & long-form):
* development | dev | qa | uat | production | prod
*
* Usually relates to which deployment server the code is currently running on
*/
export type ReleaseEnv = 'development' | 'dev' | 'qa' | 'uat' | 'production' | 'prod';
/**
* Short forms of names of commonly-used release environments:
* dev | prod | qa | uat
*
* Usually relates to which deployment server the code is currently running on
*
* Short forms often act as an actual added subdomain e.g. qa.example.com
*/
export type ReleaseEnvShort = 'dev' | 'prod' | 'qa' | 'uat';
/**
* Current level of log verbosity:
* trace | silly | debug | verbose | info | warn | error | wtf
*
* Lower levels = less logging, higher = more
*
* Usually 'warn' or 'error' is used in production, 'info' in normal cases in
* development, and even lower options when intensively debugging
*
* 'trace' also sometimes has specific meanings in other modules
*/
export type LogLevel = 'trace' | 'silly' | 'debug' | 'verbose' | 'info' | 'warn' | 'error' | 'wtf';
/******************************************** HELPERS *********************************************/
const hasVal = (val: Any) => typeof val !== `undefined` && val !== null && val !== ``;
const strToBool = (rawVal: string): boolean => {
const val = rawVal.toLowerCase();
return val === `false` || val === `f` ? false : val === `true` || val === `t`;
};
const toBool = (rawVal: string | boolean | null, def: boolean) => {
// If value not set, return false, or default if given
if (!hasVal(rawVal)) return hasVal(def) ? def : false;
// If val is a string, convert from boolean string (e.g. "true") to boolean
if (typeof rawVal === `string`) return strToBool(rawVal);
// If val is boolean, return it as-is
return rawVal;
};
/********************************* GET & PROCESS ENVIRONMENT VALS *********************************/
let NODE_ENV: NodeEnv;
let RELEASE_ENV: ReleaseEnv;
let LOG_LEVEL: LogLevel;
let TEST_MODE: boolean;
let IE_COMPAT: boolean;
let AVOID_WEB: boolean;
let IS_LOCAL: boolean;
let SKIP_BASIC_AUTH: boolean;
// prettier-ignore
{
NODE_ENV = hasVal(RAW_NODE_ENV) ? RAW_NODE_ENV.toLowerCase() : `development`;
RELEASE_ENV = hasVal(RAW_RELEASE_ENV) ? RAW_RELEASE_ENV.toLowerCase() : NODE_ENV;
LOG_LEVEL = hasVal(RAW_LOG_LEVEL) ? RAW_LOG_LEVEL.toLowerCase() : `info`;
TEST_MODE = hasVal(RAW_TEST_MODE) ? toBool(RAW_TEST_MODE, false) : false;
IE_COMPAT = hasVal(RAW_IE_COMPAT) ? toBool(RAW_IE_COMPAT, false) : false;
AVOID_WEB = hasVal(RAW_AVOID_WEB) ? toBool(RAW_AVOID_WEB, false) : false;
IS_LOCAL = hasVal(RAW_IS_LOCAL) ? toBool(RAW_mocha, false) : false;
SKIP_BASIC_AUTH = hasVal(RAW_SKIP_BASIC_AUTH) ? toBool(RAW_SKIP_BASIC_AUTH, false) : false;
}
const WAS_RUN_THRU_MOCHA = hasVal(RAW_LOADED_MOCHA_OPTS) || (RAW_mocha && toBool(RAW_mocha, false));
/**
* Node environment (NODE_ENV):
* development | production
* Converts short-form to long-form, contains default value if NODE_ENV not set
*/
export const nodeEnv = NODE_ENV.replace(/^dev$/, 'development').replace(
/^prod$/,
'production'
) as NodeEnvFull;
/**
* Directly output log level (LOG_LEVEL env var):
* trace | silly | debug | verbose | info | warn | error | wtf
* Resolves default value
*/
export const logLevel = LOG_LEVEL;
/**
* Namespace for direct access to environment variables:
* NODE_ENV
* LOG_LEVEL
* IE_COMPAT
* TEST_MODE
* AVOID_WEB
* RELEASE_ENV
* IS_LOCAL
* SKIP_BASIC_AUTH
* WAS_RUN_THRU_MOCHA
*/
export const env = {
NODE_ENV: nodeEnv,
LOG_LEVEL: logLevel,
IE_COMPAT,
TEST_MODE,
AVOID_WEB,
RELEASE_ENV,
IS_LOCAL,
SKIP_BASIC_AUTH,
WAS_RUN_THRU_MOCHA,
};
/******************************************** NODE_ENV ********************************************/
/**
* true if current process was run with NODE_ENV=development or NODE_ENV=dev
*/
export const isDevelopment = nodeEnv === `development`;
export {isDevelopment as isDev};
/**
* true if current process was run with NODE_ENV=production or NODE_ENV=prod
*/
export const isProduction = nodeEnv === `production`;
export {isProduction as isProd};
/**
* true if NODE_ENV=production, TEST_SECURITY=true, or SECURITY_TEST=true
*/
export const prodOrSecurityTest =
isProduction ||
(hasVal(RAW_TEST_SECURITY) && toBool(RAW_TEST_SECURITY, false)) ||
(hasVal(RAW_SECURITY_TEST) && toBool(RAW_SECURITY_TEST, false));
export {prodOrSecurityTest as isProdOrSecurityTest};
/******************************************* LOG_LEVEL ********************************************/
/**
* true if current process was run with LOG_LEVEL=trace
*/
export const isTrace = LOG_LEVEL === `trace`;
/**
* true if current process was run with LOG_LEVEL=silly
*/
export const isSilly = isTrace || LOG_LEVEL === `silly`;
/**
* true if current process was run with LOG_LEVEL=verbose
*/
export const isVerbose = isSilly || LOG_LEVEL === `verbose`;
/**
* true if current process was run with LOG_LEVEL=debug
*/
export const isDebug = isVerbose || LOG_LEVEL === `debug`;
/**
* true if current process was run with LOG_LEVEL=info
*/
export const isInfo = isDebug || LOG_LEVEL === `info`;
/**
* true if current process was run with LOG_LEVEL=warn
*/
export const isWarn = isInfo || LOG_LEVEL === `warn`;
/**
* true if current process was run with LOG_LEVEL=error
*/
export const isError = isWarn || LOG_LEVEL === `error`;
/**
* true if current process was run with LOG_LEVEL=wtf
*/
export const isWTF = isError || LOG_LEVEL === `wtf`;
export {isWTF as isWtf};
/********************************** IE COMPATIBILITY (IE_COMPAT) **********************************/
export const isIECompat = IE_COMPAT;
export {isIECompat as isIeCompat};
/******************************************* AVOID_WEB ********************************************/
/**
* Check for env var requesting total avoidance of web
* e.g. for avoiding use of CDNs, and using local bundles instead
*/
export const isAvoidWeb = AVOID_WEB;
export {isAvoidWeb as avoidWeb};
/******************************************** IS_LOCAL ********************************************/
/**
* true if IS_LOCAL=true, indicating process is running in localhost environment
*
* Must be set manually (it doesn't automatically detect local environment)
*/
export const isLocal = IS_LOCAL;
/**************************************** SKIP_BASIC_AUTH *****************************************/
/**
* true if process run with SKIP_BASIC_AUTH=true, indicating that basic auth
* should be shut off
*
* Meant to be used when basic auth is conditionally used by a server, often
* based on deployment environment
*/
export const isSkipBasicAuth = SKIP_BASIC_AUTH;
export {isSkipBasicAuth as skipBasicAuth};
export {isSkipBasicAuth as doSkipBasicAuth};
/************************** TEST ENVIRONMENT (LOADED_MOCHA_OPTS, Mocha) ***************************/
/**
* true if TEST_MODE was set explicitly to true or false (e.g. TEST_MODE=true)
*/
export const isTestMode = TEST_MODE && toBool(`TEST_MODE`, false);
/**
* true if current script was run via Mocha
*/
export const isMocha = WAS_RUN_THRU_MOCHA;
export {isMocha as isMochaEnv};
export {isMocha as runByMocha};
/******************************* RELEASE ENVIRONMENT (RELEASE_ENV) ********************************/
/**
* Output value of RELEASE_ENV=something
* Should be 'dev', 'qa', 'uat', 'prod', 'development', or 'production'
*/
export const releaseEnv = RELEASE_ENV;
export {releaseEnv as releaseEnvironment};
/**
* true if RELEASE_ENV=uat
*/
export const isReleaseEnvUAT = RELEASE_ENV === `uat`;
export {isReleaseEnvUAT as isUAT};
/**
* true if RELEASE_ENV=qa
*/
export const isReleaseEnvQA = RELEASE_ENV === `qa`;
export {isReleaseEnvQA as isQA};
/**
* true if RELEASE_ENV=dev
*/
export const isReleaseEnvDev = RELEASE_ENV === `dev` || RELEASE_ENV === `development`;
export {isReleaseEnvDev as isReleaseEnvDevelopment};
/**
* true if RELEASE_ENV=prod
*/
export const isReleaseEnvProd = RELEASE_ENV === `prod` || RELEASE_ENV === `production`;
export {isReleaseEnvProd as isReleaseEnvProduction};
/**
* 3-4 letter version of release environment name (RELEASE_ENV):
* dev | qa | uat | prod
*
* Default: 'dev'
*/
export const releaseEnvShort: ReleaseEnvShort = (function() {
return isReleaseEnvProd ? `prod` : isReleaseEnvUAT ? `uat` : isReleaseEnvQA ? `qa` : `dev`;
})();
export {releaseEnvShort as releaseEnvAbbrev};
/**
* true if in development mode, but not on either QA, UAT, or prod release
* environment
*
* i.e. true if NODE_ENV=dev and RELEASE_ENV isn't qa, uat, or prod
*
* Reason: sometimes we want to do things in development and treat QA and/or
* UAT as a development environment, but explicitly not do an action in a
* release environment (mainly QA or UAT)
*/
export const isDevNonReleaseEnv =
isDevelopment && !isReleaseEnvQA && !isReleaseEnvUAT && !isReleaseEnvProd;
export {isDevNonReleaseEnv as isDevNotQaUat}
export {isDevNonReleaseEnv as isDevNotQaUatProd}