-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_variable.ts
More file actions
23 lines (23 loc) · 1.06 KB
/
Copy path_variable.ts
File metadata and controls
23 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { getEnv } from "jsr:@hugoalh/env@^0.5.0/general";
import { isAbsolute as isPathAbsolute } from "node:path";
export function getVariableWithGuard(key: string, description: string): string {
const value: string = getEnv(key) ?? "";
if (value.length === 0) {
throw new ReferenceError(`Unable to get the ${description}, environment variable \`${key}\` is not defined!`);
}
return value;
}
export function getVariableWithGuardAbsolutePath(key: string, description: string): string {
const value: string = getVariableWithGuard(key, description);
if (!isPathAbsolute(value)) {
throw new Error(`\`${value}\` (environment variable \`${key}\`) is not an absolute path of the ${description}!`);
}
return value;
}
export function getVariableWithGuardExpected<T extends string>(key: string, description: string, expectedValues: readonly T[]): T {
const value: string = getVariableWithGuard(key, description);
if (!expectedValues.includes(value as T)) {
throw new Error(`\`${value}\` (environment variable \`${key}\`) is not a known ${description}!`);
}
return value as T;
}