config changes to make it not async
This commit is contained in:
parent
24680e403d
commit
92d4b90987
5 changed files with 131 additions and 13 deletions
|
|
@ -82,6 +82,51 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the configuration synchronously (only env vars, no file loading)
|
||||
*/
|
||||
initializeSync(schema?: ConfigSchema): T {
|
||||
if (this.config) {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
this.schema = schema;
|
||||
|
||||
// Only use EnvLoader for sync initialization
|
||||
const envLoader = this.loaders.find(loader => loader.constructor.name === 'EnvLoader');
|
||||
if (!envLoader) {
|
||||
throw new ConfigError('No EnvLoader found for synchronous initialization');
|
||||
}
|
||||
|
||||
// Load env vars synchronously
|
||||
const envLoaderInstance = envLoader as any;
|
||||
const config = envLoaderInstance.loadSync ? envLoaderInstance.loadSync() : {};
|
||||
|
||||
// Add environment if not present
|
||||
if (typeof config === 'object' && config !== null && !('environment' in config)) {
|
||||
(config as Record<string, unknown>)['environment'] = this.environment;
|
||||
}
|
||||
|
||||
// Validate if schema provided
|
||||
if (this.schema) {
|
||||
try {
|
||||
this.config = this.schema.parse(config) as T;
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
throw new ConfigValidationError(
|
||||
'Configuration validation failed',
|
||||
error.errors
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
this.config = config as T;
|
||||
}
|
||||
|
||||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current configuration
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -26,6 +26,10 @@ export class EnvLoader implements ConfigLoader {
|
|||
}
|
||||
|
||||
async load(): Promise<Record<string, unknown>> {
|
||||
return this.loadSync();
|
||||
}
|
||||
|
||||
loadSync(): Record<string, unknown> {
|
||||
try {
|
||||
// Load root .env file - try multiple possible locations
|
||||
const possiblePaths = ['./.env', '../.env', '../../.env'];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue