import { isWindows } from "../deno_ral/platform.ts";
export function getenv(name: string, defaultValue?: string) {
const value = Deno.env.get(name);
if (value === undefined) {
if (defaultValue === undefined) {
throw new Error(`Required environment variable ${name} not specified.`);
} else {
return defaultValue;
}
} else {
return value;
}
}
export function withPath(
paths: { append?: string[]; prepend?: string[] },
): string {
const delimiter = isWindows ? ";" : ":";
const currentPath = Deno.env.get("PATH") || "";
if (paths.append !== undefined && paths.prepend !== undefined) {
return currentPath;
} else if (paths.append?.length === 0 && paths.prepend?.length === 0) {
return currentPath;
} else {
const modifiedPaths = [currentPath];
if (paths.append) {
modifiedPaths.unshift(...paths.append);
}
if (paths.prepend) {
modifiedPaths.push(...paths.prepend);
}
return modifiedPaths.join(delimiter);
}
}