import { LogLevel } from '../index.js'; /** * Log message interface for environment setup operations */ export interface LogMessage { /** Log level (log, warn, error) */ logLevel: LogLevel; /** Log message content */ message: string; } /** * Configuration interface for environment setup operations */ export interface EnvSetupConfig { /** Path to environment file (optional, uses fallback logic if empty) */ envFile?: string; /** Whether to override existing variables when loading from file (default: false) */ envFileOverride?: boolean; /** Whether to process environment file before flag variables (default: false) */ envFileFirst?: boolean; /** Object containing flag variables to set */ flagVars?: Record; /** Whether to override existing variables when setting flag variables (default: false) */ flagVarsOverride?: boolean; /** Object containing default variables to set (never override existing) */ defaultVars?: Record; } /** * Result object returned by environment setup operations */ export interface EnvSetupResult { /** Absolute path to the environment file successfully loaded, or null when none was loaded */ envFilePath: string | null; /** Log messages from the setting process with levels */ messages: LogMessage[]; /** Whether the operation was successful */ success: boolean; } /** * Environment manager for loading and setting environment variables * * Features: * - Load from .env files with priority system * - Set variables from objects with validation * - Prevent override of existing variables * - Comprehensive logging with levels (log, warn, error) and error handling * - Returns structured log messages with appropriate emojis */ export declare class EnvManager { /** * Template messages for logging with placeholder support * * Templates use %placeholder% syntax for dynamic content: * - %file% - File path * - %type% - Environment object type (flags/default) * - %error% - Error message * - %qty% - Quantity/count * - %list% - Comma-separated list */ private static readonly MESSAGES; /** * Internal log buffer for the current setup operation * Reset at the start of each setupEnvironment() call */ private static operationLog; private static envFilePath; /** * Setup environment variables from multiple sources with priority system * * @param config - Configuration object specifying sources and priorities (optional, uses defaults if not provided) * @returns Result with operation messages and success status * * @example * ```typescript * // With full configuration * const result = EnvManager.setupEnvironment({ * envFile: '.env.production', * envFileOverride: false, * envFileFirst: true, * flagVars: { DEBUG: true, PORT: 3000 }, * flagVarsOverride: true, * defaultVars: { NODE_ENV: 'development' } * }); * * // With default configuration (loads default .env file only) * const result = EnvManager.setupEnvironment(); * * if (result.success) { * console.log('Environment setup completed'); * result.messages.forEach(msg => { * console[msg.logLevel](msg.message); * }); * } * ``` */ static setupEnvironment(config?: EnvSetupConfig): EnvSetupResult; /** * Build array of operations based on configuration priority settings * * @private * @param config - Environment setup configuration * @returns Array of operation functions to execute in order */ private static buildOperations; /** * Execute all operations sequentially and return combined success status * * @private * @param operations - Array of operation functions to execute * @returns True if all operations succeeded, false otherwise */ private static executeOperations; /** * Process environment file loading with validation and error handling * * @private * @param config - Environment setup configuration containing file settings * @returns True if file processing succeeded, false otherwise */ private static processEnvFile; /** * Process environment variables from object (flags or defaults) with type validation * * @private * @param config - Environment setup configuration containing variable objects * @param type - Type of variables being processed (flags or default) * @returns True if object processing succeeded, false otherwise */ private static processEnvObject; /** * Load environment variables from a file with error handling and validation * * Features: * - Reads file content safely with try-catch * - Parses .env format using dotenv library * - Validates non-empty content * - Returns detailed operation results * * @private * @param resolvedPath - Absolute path to environment file * @param override - Whether to override existing environment variables * @returns Operation result with success status and variable details * * @example * ```typescript * const result = this.loadFromFile('/path/to/.env', false); * if (result.success) { * console.log(`Loaded ${result.setVars?.length} variables`); * } * ``` */ private static loadFromFile; /** * Set environment variables from an object with comprehensive validation * * Features: * - Converts values to strings automatically (number, boolean → string) * - Validates keys (no empty/whitespace-only keys) * - Validates values (no undefined/null/empty values after trimming) * - Respects existing variables unless override is true * - Returns detailed results for logging and debugging * - Sanitizes keys by trimming whitespace * * @private * @param envVars - Object with environment variable key-value pairs * @param override - Whether to override existing environment variables (default: false) * @returns Operation result with set/ignored/skipped variables and success status * * @example * ```typescript * const result = this.setFromObject({ * PORT: 8080, // number → '8080' * LOG_FORMAT: 'json', // string → 'json' * DEBUG: true, // boolean → 'true' * API_KEY: undefined, // ignored (undefined) * EMPTY: '', // ignored (empty string) * ' ': 'value' // ignored (invalid key) * }); * * console.log(`Set ${result.setVars?.length} variables`); * console.log(`Ignored ${result.ignoredVars?.length} variables`); * console.log(`Skipped ${result.skippedVars?.length} existing variables`); * ``` */ private static setFromObject; /** * Resolve file path using fallback logic with comprehensive path resolution * * Priority order: * 1. Specified filePath parameter (if not empty after trimming) * 2. Default .env file in current working directory * * @private * @param filePath - User-specified file path (may be empty for fallback logic) * @returns Resolved absolute path to environment file */ private static resolveFilePath; /** * Log detailed results of environment variable operation * * Logs counts and details for: * - Successfully set variables * - Ignored variables with reasons * - Skipped existing variables * * @private * @param result - Operation result containing variable details */ private static logOperationDetails; /** * Log informational message * @private * @param message - Message to log */ private static logInfo; /** * Log error message * @private * @param message - Error message to log */ private static logError; /** * Log warning message * @private * @param message - Warning message to log */ private static logWarning; /** * Log debug message * @private * @param message - Message to log */ private static logDebug; /** * Format message template by replacing placeholders with actual values * * @private * @param template - Message template with %placeholder% markers * @param replacements - Object with placeholder-value pairs * @returns Formatted message with placeholders replaced * * @example * ```typescript * const formatted = this.formatMessage( * 'Processing %type% with %count% items', * { type: 'flags', count: '5' } * ); * // Result: 'Processing flags with 5 items' * ``` */ private static formatMessage; } //# sourceMappingURL=env-manager.d.ts.map