made config async
This commit is contained in:
parent
92d4b90987
commit
caf1c5fcaf
6 changed files with 91 additions and 143 deletions
|
|
@ -1,28 +1,8 @@
|
|||
// Export all schemas
|
||||
export * from './schemas';
|
||||
|
||||
// Export types
|
||||
export * from './types';
|
||||
|
||||
// Export errors
|
||||
export * from './errors';
|
||||
|
||||
// Export loaders
|
||||
export { EnvLoader } from './loaders/env.loader';
|
||||
export { FileLoader } from './loaders/file.loader';
|
||||
|
||||
// Export ConfigManager
|
||||
export { ConfigManager } from './config-manager';
|
||||
|
||||
// Export utilities
|
||||
export * from './utils/secrets';
|
||||
export * from './utils/validation';
|
||||
|
||||
// Import necessary types for singleton
|
||||
import { EnvLoader } from './loaders/env.loader';
|
||||
import { FileLoader } from './loaders/file.loader';
|
||||
import { ConfigManager } from './config-manager';
|
||||
import { AppConfig, appConfigSchema } from './schemas';
|
||||
import { FileLoader } from './loaders/file.loader';
|
||||
import { EnvLoader } from './loaders/env.loader';
|
||||
|
||||
// Create singleton instance
|
||||
let configInstance: ConfigManager<AppConfig> | null = null;
|
||||
|
|
@ -37,30 +17,36 @@ function loadCriticalEnvVarsSync(): void {
|
|||
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;
|
||||
|
||||
if (!trimmed || trimmed.startsWith('#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const equalIndex = trimmed.indexOf('=');
|
||||
if (equalIndex === -1) continue;
|
||||
|
||||
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("'"))) {
|
||||
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) {
|
||||
} catch {
|
||||
// Ignore errors - env file is optional
|
||||
}
|
||||
}
|
||||
|
|
@ -69,26 +55,16 @@ function loadCriticalEnvVarsSync(): void {
|
|||
loadCriticalEnvVarsSync();
|
||||
|
||||
/**
|
||||
* Initialize configuration synchronously (env vars only)
|
||||
* This should be called at the very start of the application
|
||||
* Initialize the global configuration synchronously.
|
||||
*
|
||||
* This loads configuration from all sources in the correct hierarchy:
|
||||
* 1. Schema defaults (lowest priority)
|
||||
* 2. default.json
|
||||
* 3. [environment].json (e.g., development.json)
|
||||
* 4. .env file values
|
||||
* 5. process.env values (highest priority)
|
||||
*/
|
||||
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
|
||||
*/
|
||||
export async function initializeConfig(
|
||||
configPath?: string
|
||||
): Promise<AppConfig> {
|
||||
export function initializeConfig(configPath?: string): AppConfig {
|
||||
if (!configInstance) {
|
||||
configInstance = new ConfigManager<AppConfig>({
|
||||
configPath,
|
||||
|
|
@ -98,21 +74,21 @@ export async function initializeConfig(
|
|||
}
|
||||
|
||||
/**
|
||||
* Initialize configuration for a service in a monorepo
|
||||
* Initialize configuration for a service in a monorepo.
|
||||
* Automatically loads configs from:
|
||||
* 1. Root config directory (../../config)
|
||||
* 2. Service-specific config directory (./config)
|
||||
* 3. Environment variables
|
||||
*/
|
||||
export async function initializeServiceConfig(): Promise<AppConfig> {
|
||||
export function initializeServiceConfig(): AppConfig {
|
||||
if (!configInstance) {
|
||||
const environment = process.env.NODE_ENV || 'development';
|
||||
configInstance = new ConfigManager<AppConfig>({
|
||||
loaders: [
|
||||
new FileLoader('../../config', environment), // Root config
|
||||
new FileLoader('./config', environment), // Service config
|
||||
new FileLoader('./config', environment), // Service config
|
||||
new EnvLoader(''), // Environment variables
|
||||
]
|
||||
],
|
||||
});
|
||||
}
|
||||
return configInstance.initialize(appConfigSchema);
|
||||
|
|
@ -185,4 +161,24 @@ export function isProduction(): boolean {
|
|||
|
||||
export function isTest(): boolean {
|
||||
return getConfig().environment === 'test';
|
||||
}
|
||||
}
|
||||
|
||||
// Export all schemas
|
||||
export * from './schemas';
|
||||
|
||||
// Export types
|
||||
export * from './types';
|
||||
|
||||
// Export errors
|
||||
export * from './errors';
|
||||
|
||||
// Export loaders
|
||||
export { EnvLoader } from './loaders/env.loader';
|
||||
export { FileLoader } from './loaders/file.loader';
|
||||
|
||||
// Export ConfigManager
|
||||
export { ConfigManager } from './config-manager';
|
||||
|
||||
// Export utilities
|
||||
export * from './utils/secrets';
|
||||
export * from './utils/validation';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue