@halospv3/hce.shared-config
Version:
Automate commit message quality, changelogs, and CI/CD releases. Exports a semantic-release shareable configuration deserialized from this package's '.releaserc.yml'. Shared resources for .NET projects are also distributed with this package.
37 lines (32 loc) • 947 B
text/typescript
import { config as loadDotenv, type DotenvConfigOptions } from "dotenv";
import { env } from 'node:process';
/**
* Load a .env file from the CWD with the given options (or defaults), returns the new value of process.env
* @param dotenvOptions
* @param overrides
* @returns
*/
export function getEnv(dotenvOptions?: DotenvConfigOptions, overrides?: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
loadDotenv(dotenvOptions);
if (overrides)
Object.assign(env, overrides);
return env;
}
/**
* Get the value from the given env var. If undefined, load .env from CWD and try again or return undefined.
* @param envVar
* @returns
*/
export function getEnvVarValue(envVar: string): string | undefined {
let value = env[envVar];
if (!value) {
try {
loadDotenv();
}
catch (err) {
console.error(String(err))
}
value = env[envVar];
}
return value;
}