Spaces:
Sleeping
Sleeping
File size: 733 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import { config } from "dotenv";
import { existsSync } from "fs";
import { join } from "path";
export const load = <T extends Record<string, string> = Record<string, string>>(
root: string = process.cwd(),
): T => {
const localEnv = join(root, ".env.local");
const modeEnv = join(root, `.env.${process.env.NODE_ENV}`);
const defaultEnv = join(root, ".env");
return [localEnv, modeEnv, defaultEnv].reduce<T>((prev, path) => {
const variables = !existsSync(path) ? {} : (config({ path }).parsed ?? {});
Object.entries(variables).forEach(([key, value]) => {
if (!Object.prototype.hasOwnProperty.call(prev, key))
Object.assign(prev, { [key]: value });
});
return prev;
}, {} as T);
};
load();
|