config changes to make it not async

This commit is contained in:
Boki 2025-06-20 20:05:05 -04:00
parent 24680e403d
commit 92d4b90987
5 changed files with 131 additions and 13 deletions

View file

@ -27,6 +27,62 @@ import { EnvLoader } from './loaders/env.loader';
// Create singleton instance
let configInstance: ConfigManager<AppConfig> | null = null;
// Synchronously load critical env vars for early initialization
function loadCriticalEnvVarsSync(): void {
// Load .env file synchronously if it exists
try {
const fs = require('fs');
const path = require('path');
const envPath = path.resolve(process.cwd(), '.env');
if (fs.existsSync(envPath)) {
const envContent = fs.readFileSync(envPath, 'utf-8');
const lines = envContent.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const equalIndex = trimmed.indexOf('=');
if (equalIndex === -1) continue;
const key = trimmed.substring(0, equalIndex).trim();
let value = trimmed.substring(equalIndex + 1).trim();
// Remove surrounding quotes
if ((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))) {
value = value.slice(1, -1);
}
// Only set if not already set
if (!(key in process.env)) {
process.env[key] = value;
}
}
}
} catch (error) {
// Ignore errors - env file is optional
}
}
// Load critical env vars immediately
loadCriticalEnvVarsSync();
/**
* Initialize configuration synchronously (env vars only)
* This should be called at the very start of the application
*/
export function initializeConfigSync(): AppConfig {
if (!configInstance) {
configInstance = new ConfigManager<AppConfig>({
loaders: [
new EnvLoader(''), // Environment variables only for sync
]
});
}
return configInstance.initializeSync(appConfigSchema);
}
/**
* Initialize the global configuration
*/