-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathenv-utils.ts
More file actions
46 lines (42 loc) · 1.5 KB
/
env-utils.ts
File metadata and controls
46 lines (42 loc) · 1.5 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
import { readEnvFile } from './github-env';
import { WrapperConfig } from './types';
export function normalizeEnvValue(value: string | undefined): string | undefined {
const normalizedValue = value?.trim();
return normalizedValue || undefined;
}
export function getConfigEnvValue(config: WrapperConfig, key: string): string | undefined {
const envFileValue = config.envFile
? readEnvFile(config.envFile)[key]
: undefined;
const value =
config.additionalEnv?.[key] ??
envFileValue ??
(config.envAll ? process.env[key] : undefined);
return normalizeEnvValue(value);
}
export function getLowerCaseProcessEnvValue(key: string): string | undefined {
return normalizeEnvValue(process.env[key])?.toLowerCase();
}
/**
* Returns an object containing only the specified environment variable names
* that are currently set (non-empty) in `process.env`.
*
* This avoids the repetitive `...(process.env.X && { X: process.env.X })` pattern
* while keeping the conditional-inclusion semantics: variables that are absent or
* empty are simply omitted from the result.
*
* @example
* // Instead of:
* // ...(process.env.FOO && { FOO: process.env.FOO }),
* // ...(process.env.BAR && { BAR: process.env.BAR }),
* // Write:
* // ...pickEnvVars('FOO', 'BAR'),
*/
export function pickEnvVars(...names: string[]): Record<string, string> {
const result: Record<string, string> = {};
for (const name of names) {
const val = process.env[name];
if (val) result[name] = val;
}
return result;
}