@halospv3/hce.shared-config
Version:
Automate commit message quality, changelogs, and CI/CD releases. Its `main` entry point is a Semantic Release config. Functions and classes are exposed for customization. An ESLint config, a Commitlint config, and addl. resources for .NET projects are als
40 lines (39 loc) • 1.9 kB
JavaScript
import { env } from "node:process";
import dotenvx from "@dotenvx/dotenvx";
//#region src/utils/env.ts
const { get, config: loadDotenv } = dotenvx;
/**
* A thin wrapper for {@link loadDotenv}. Loads a .env file from {@link process.cwd()} with the given options (or defaults), returns the new value of {@link process.env} with optional overrides.
* @param [dotenvOptions] An optional {@link DotenvConfigOptions} object to pass to {@link loadDotenv}.
* @param [overrides] If provided, this {@link NodeJS.ProcessEnv} object is merged into the return value, overriding existing properties where overlap occurs.
* @returns A {@link NodeJS.ProcessEnv} object whose properties are variables loaded from the
* process environment, the nearest .env file, and {@link overrides} (if provided). Where
* overlap occurs, the later source takes priority.
*/
function getEnv(dotenvOptions, overrides) {
loadDotenv(dotenvOptions);
if (overrides) Object.assign(env, overrides);
return env;
}
/**
* Get the value from the given env var in the current process or nearby .env file.
* If found in process environment, its value is returned.
* Else, try to get it from the nearest .env file.
* If NOT found, return `undefined`
* @param envVar The environment variable to lookup.
* @param [options] Options to pass to {@link get}
* @returns The string value of the environment variable or `undefined`.
* `undefined` may be returned when the variable is undefined or its string is
* empty, whitespace, or appears to have been converted from `null` or
* `undefined`.
*/
function getEnvVarValue(envVar, options) {
options ??= { ignore: ["MISSING_KEY", "MISSING_ENV_FILE"] };
let value = env[envVar];
const x = get(envVar, options);
if (typeof x === "string") value = x;
return value === "" || value === "undefined" ? void 0 : value;
}
//#endregion
export { getEnv, getEnvVarValue };
//# sourceMappingURL=env.mjs.map