managedenv
Version:
Manage your environment variables with ease
69 lines (68 loc) • 1.95 kB
TypeScript
export interface VariableDefinition<T = unknown> {
/**
* The name of the variable (e.g., "API_KEY").
*/
name: string;
/**
* A custom parser for the variable value (e.g., parseInt or Boolean).
*/
type?: (val: string) => T;
/**
* Optional project this variable belongs to. Useful in monorepo setups.
* Defaults to "env".
*/
project?: string;
/**
* Whether the variable is required to be set.
*/
required: boolean;
/**
* A fallback value if none is provided.
*/
default?: T;
/**
* Optional CLI flag (e.g., --token) that can be used instead of ENV.
*/
useFlagInstead?: string;
/**
* Only load the variable if this CLI flag is present.
*/
useWithFlag?: string;
/**
* Whether to quit the process if a required variable is missing.
*/
quitOnMissing?: boolean;
}
type ProjectVars = Record<string, Record<string, any>>;
export declare class EnvManager<T extends ProjectVars = {}> {
private definitions;
/**
* Adds a new variable definition.
*
* @param def The variable definition to add.
* @returns The updated EnvManager instance.
*/
add<const K extends string, const P extends string = "env", V = string>(def: VariableDefinition<V> & {
name: K;
project?: P;
}): EnvManager<T & {
[key in P]: {
[k in K]: V;
};
}>;
/**
* Retrieves the value of a CLI flag.
*
* @param flag The CLI flag to check (e.g., "--token").
* @returns The value of the flag or undefined if not set.
*/
private getFlagValue;
/**
* Loads the environment variables based on the defined variable definitions.
*
* @returns An object containing the loaded variables grouped by project.
* @throws Error if a required variable is missing and quitOnMissing is true.
*/
load(): T;
}
export {};