/** * Core configuration module for the Stock Bot platform using Yup */ import path from 'node:path'; import { config as dotenvConfig } from 'dotenv'; /** * Represents an error related to configuration validation */ export class ConfigurationError extends Error { constructor(message: string) { super(message); this.name = 'ConfigurationError'; } } /** * Environment types */ export enum Environment { Development = 'development', Testing = 'testing', Staging = 'staging', Production = 'production', } /** * Loads environment variables from .env files based on the current environment */ export function loadEnvVariables(envOverride?: string): void { const env = envOverride || process.env.NODE_ENV || 'development'; console.log(`Current environment: ${env}`); // Order of loading: // 1. .env (base environment variables) // 2. .env.{environment} (environment-specific variables) // 3. .env.local (local overrides, not to be committed) const envFiles = ['.env', `.env.${env}`, '.env.local']; for (const file of envFiles) { dotenvConfig({ path: path.resolve(process.cwd(), file) }); } } /** * Gets the current environment from process.env.NODE_ENV */ export function getEnvironment(): Environment { const env = process.env.NODE_ENV?.toLowerCase() || 'development'; switch (env) { case 'development': return Environment.Development; case 'testing': case 'test': // Handle both 'test' and 'testing' for compatibility return Environment.Testing; case 'staging': return Environment.Staging; case 'production': return Environment.Production; default: return Environment.Development; } }