var fs = require('node:fs'); var path = require('node:path'); var dotEnv = require('dotenv'); var dotenvExpand = require('dotenv-expand'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs); var path__default = /*#__PURE__*/_interopDefaultLegacy(path); var dotEnv__default = /*#__PURE__*/_interopDefaultLegacy(dotEnv); /** * Setup environment variables from .env files based on NODE_ENV * @param rootPath - root path for the .env files * @param debug - dotEnv debug flag * @returns list of loaded files */ function setupEnv(rootPath, { envVar = "NODE_ENV", debug = false, onLoad } = {}) { const resolvedEnv = (process.env[envVar] || "").toLowerCase(); if (!resolvedEnv) { throw new Error(`Environment variable ${envVar} is not set`); } let files = [`.env.${resolvedEnv}.local`, ".env.local", `.env.${resolvedEnv}`, ".env"]; if (resolvedEnv === "test") { files = files.filter(file => !file.includes(".local")); } const loaded = []; for (const file of files) { const fullPath = path__default["default"].normalize(`${rootPath}/${file}`); if (fs__default["default"].existsSync(fullPath)) { loaded.push(file); const result = dotEnv__default["default"].config({ path: fullPath, override: false, debug }); if (onLoad) { onLoad({ path: rootPath, filename: file, result }); } } } // @ts-expect-error - process.env string index signature dotenvExpand.expand({ ignoreProcessEnv: true, parsed: process.env }); return loaded; } exports.setupEnv = setupEnv; //# sourceMappingURL=index.cjs.map