-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathenv.ts
More file actions
53 lines (43 loc) · 1.27 KB
/
env.ts
File metadata and controls
53 lines (43 loc) · 1.27 KB
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
import { ui } from './logging.js';
export function isCI() {
return isEnvVarEnabled('CI');
}
export function isVerbose() {
return isEnvVarEnabled('CP_VERBOSE');
}
export function isEnvVarEnabled(name: string): boolean {
const value = coerceBooleanValue(process.env[name]);
if (typeof value === 'boolean') {
return value;
}
if (process.env[name]) {
ui().logger.warning(
`Environment variable ${name} expected to be a boolean (true/false/1/0), but received value ${process.env[name]}. Treating it as disabled.`,
);
}
return false;
}
export function coerceBooleanValue(value: unknown): boolean | undefined {
if (typeof value === 'boolean') {
return value;
}
if (typeof value === 'string') {
const booleanValuePairs = [
['true', 'false'],
['on', 'off'],
['yes', 'no'],
];
const lowerCaseValue = value.toLowerCase();
// eslint-disable-next-line functional/no-loop-statements
for (const [trueValue, falseValue] of booleanValuePairs) {
if (lowerCaseValue === trueValue || lowerCaseValue === falseValue) {
return lowerCaseValue === trueValue;
}
}
const intValue = Number.parseInt(value, 10);
if (!Number.isNaN(intValue)) {
return intValue !== 0;
}
}
return undefined;
}