UNPKG

@nodedaemon/core

Version:

Production-ready Node.js process manager with zero external dependencies

75 lines 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadEnvFile = loadEnvFile; exports.findEnvFile = findEnvFile; exports.mergeEnvConfigs = mergeEnvConfigs; const fs_1 = require("fs"); const path_1 = require("path"); function loadEnvFile(filePath) { const envConfig = {}; if (!(0, fs_1.existsSync)(filePath)) { return envConfig; } try { const content = (0, fs_1.readFileSync)(filePath, 'utf8'); const lines = content.split('\n'); for (const line of lines) { // Skip empty lines and comments const trimmedLine = line.trim(); if (!trimmedLine || trimmedLine.startsWith('#')) { continue; } // Parse KEY=VALUE format const separatorIndex = trimmedLine.indexOf('='); if (separatorIndex > 0) { const key = trimmedLine.substring(0, separatorIndex).trim(); const value = trimmedLine.substring(separatorIndex + 1).trim(); // Remove surrounding quotes if present const unquotedValue = value.replace(/^["']|["']$/g, ''); envConfig[key] = unquotedValue; } } } catch (error) { // Silently fail if env file cannot be read } return envConfig; } function findEnvFile(scriptPath, envFile) { const scriptDir = (0, path_1.dirname)(scriptPath); // If specific env file is provided if (envFile) { const envPath = (0, path_1.join)(scriptDir, envFile); return (0, fs_1.existsSync)(envPath) ? envPath : null; } // Look for common env file names in order of priority const envFiles = [ '.env.local', '.env.development', '.env.production', '.env' ]; // Check current directory first for (const file of envFiles) { const envPath = (0, path_1.join)(process.cwd(), file); if ((0, fs_1.existsSync)(envPath)) { return envPath; } } // Then check script directory for (const file of envFiles) { const envPath = (0, path_1.join)(scriptDir, file); if ((0, fs_1.existsSync)(envPath)) { return envPath; } } return null; } function mergeEnvConfigs(...configs) { const merged = {}; for (const config of configs) { Object.assign(merged, config); } return merged; } //# sourceMappingURL=env.js.map