Skip to content

Commit

Permalink
fix: merge configs accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
en9inerd committed Feb 22, 2024
1 parent 5889fcc commit 1bb0827
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/services/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { access } from 'fs/promises';
import { fileURLToPath } from 'url';
import { join, dirname } from 'path';
Expand All @@ -6,9 +7,6 @@ import { configInstance } from '../keys.js';

export class ConfigService {
private rootAppDir = '';
private defaultConfigPath = '';
private envConfigPath = '';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private config: any = {};
private isConfigLoaded = false;
private static [configInstance]: ConfigService;
Expand All @@ -30,35 +28,50 @@ export class ConfigService {
}

this.rootAppDir = await this.getRootAppDir();
this.defaultConfigPath = join(this.rootAppDir, 'config', 'default.js');
await this.checkConfigFileExists(this.defaultConfigPath, 'Default');
const defaultConfigPath = join(this.rootAppDir, 'config', 'default.js');
await this.checkConfigFileExists(defaultConfigPath, 'Default');

let envConfigExists = false;
let envConfigPath = '';
if (process.env.NODE_ENV) {
this.envConfigPath = join(this.rootAppDir, 'config', `${process.env.NODE_ENV}.js`);
envConfigExists = await this.checkConfigFileExists(this.envConfigPath, 'Environment', false);
envConfigPath = join(this.rootAppDir, 'config', `${process.env.NODE_ENV}.js`);
await this.checkConfigFileExists(envConfigPath, 'Environment', false);
}

const defaultConfig = await this.loadConfigFile(this.defaultConfigPath);
const envConfig = envConfigExists ? await this.loadConfigFile(this.envConfigPath) : {};
const defaultConfig = await this.loadConfigFile(defaultConfigPath);
const envConfig = envConfigPath ? await this.loadConfigFile(envConfigPath) : {};

this.config = { ...defaultConfig, ...envConfig };
this.config = this.mergeConfigs(defaultConfig, envConfig);
Object.freeze(this.config); // Make the config read-only
this.isConfigLoaded = true;
}

private async checkConfigFileExists(filePath: string, configType: string, throwError = true) {
try {
await access(filePath);
return true;
} catch (error) {
if (throwError) {
throw new ConfigException(`${configType} config file not found: ${filePath}`);
}
return false;
}
}

private mergeConfigs(defaultConfig: any, overrideConfig: any) {
const mergedConfig = { ...defaultConfig };

// Loop through the keys in overrideConfig
for (const key in overrideConfig) {
if (typeof overrideConfig[key] === 'object' && key in defaultConfig) {
// Recursively merge nested objects
mergedConfig[key] = this.mergeConfigs(defaultConfig[key], overrideConfig[key]);
} else {
// Overwrite the property in the defaultConfig
mergedConfig[key] = overrideConfig[key];
}
}

return mergedConfig;
}

private async getRootAppDir(): Promise<string> {
const appDir = process.env.PWD;
if (appDir) {
Expand Down

0 comments on commit 1bb0827

Please sign in to comment.