import path from 'path'; import fse from 'fs-extra'; import dotenv from 'dotenv'; import { Logger } from '@stackbit/types'; // dot env file loading - based on Next.js env loading // https://github.com/vercel/next.js/blob/canary/packages/next-env/index.ts export async function loadEnv(dir: string, logger?: Logger) { const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development'; const dotEnvFiles = [`.env.${mode}.local`, '.env.local', `.env.${mode}`, '.env']; for (const envFile of dotEnvFiles) { const envFilePath = path.join(dir, envFile); if (!(await fse.pathExists(envFilePath))) { continue; } logger?.debug(`[env] Loading env from ${envFilePath}`); dotenv.config({ path: envFilePath }); } }