-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathenv.ts
More file actions
84 lines (71 loc) · 2.25 KB
/
Copy pathenv.ts
File metadata and controls
84 lines (71 loc) · 2.25 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
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
import {
DEFAULT_PERSIST_FILENAME,
DEFAULT_PERSIST_FORMAT,
DEFAULT_PERSIST_OUTPUT_DIR,
DEFAULT_PERSIST_SKIP_REPORT,
type RunnerArgs,
formatSchema,
} from '@code-pushup/models';
export function isCI() {
return isEnvVarEnabled('CI');
}
export function isEnvVarEnabled(name: string): boolean {
const value = coerceBooleanValue(process.env[name]);
if (typeof value === 'boolean') {
return value;
}
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;
}
type RUNNER_ARGS_ENV_VAR =
| 'CP_PERSIST_OUTPUT_DIR'
| 'CP_PERSIST_FILENAME'
| 'CP_PERSIST_FORMAT'
| 'CP_PERSIST_SKIP_REPORTS';
type RunnerEnv = Record<RUNNER_ARGS_ENV_VAR, string>;
const FORMAT_SEP = ',';
export function runnerArgsToEnv(config: RunnerArgs): RunnerEnv {
return {
CP_PERSIST_OUTPUT_DIR: config.persist.outputDir,
CP_PERSIST_FILENAME: config.persist.filename,
CP_PERSIST_FORMAT: config.persist.format.join(FORMAT_SEP),
CP_PERSIST_SKIP_REPORTS: config.persist.skipReports.toString(),
};
}
export function runnerArgsFromEnv(env: Partial<RunnerEnv>): RunnerArgs {
const formats = env.CP_PERSIST_FORMAT?.split(FORMAT_SEP)
.map(item => formatSchema.safeParse(item).data)
.filter(item => item != null);
const skipReports = coerceBooleanValue(env.CP_PERSIST_SKIP_REPORTS);
return {
persist: {
outputDir: env.CP_PERSIST_OUTPUT_DIR || DEFAULT_PERSIST_OUTPUT_DIR,
filename: env.CP_PERSIST_FILENAME || DEFAULT_PERSIST_FILENAME,
format: formats?.length ? formats : DEFAULT_PERSIST_FORMAT,
skipReports: skipReports ?? DEFAULT_PERSIST_SKIP_REPORT,
},
};
}