X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils%2FConfiguration.ts;h=c185133a0a1d92a410a427aba0eee8517c05bb30;hb=5612b691da864fc2822629bbee32ed8008ee6472;hp=c7689f36a8604767b32135bd338f5358a2f1d0b1;hpb=c127bd643861f50701371eddbad282edb420798a;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index c7689f36..c185133a 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,3 +1,9 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +import chalk from 'chalk'; + import ConfigurationData, { StationTemplateUrl, StorageConfiguration, @@ -5,18 +11,13 @@ import ConfigurationData, { UIServerConfiguration, WorkerConfiguration, } from '../types/ConfigurationData'; - -import Constants from './Constants'; import { EmptyObject } from '../types/EmptyObject'; -import { FileType } from '../types/FileType'; import { HandleErrorParams } from '../types/Error'; +import { FileType } from '../types/FileType'; import { StorageType } from '../types/Storage'; -import WorkerConstants from '../worker/WorkerConstants'; import { WorkerProcessType } from '../types/Worker'; -import chalk from 'chalk'; -import { fileURLToPath } from 'url'; -import fs from 'fs'; -import path from 'path'; +import WorkerConstants from '../worker/WorkerConstants'; +import Constants from './Constants'; export default class Configuration { private static configurationFile = path.join( @@ -29,6 +30,10 @@ export default class Configuration { private static configuration: ConfigurationData | null = null; private static configurationChangeCallback: () => Promise; + private constructor() { + // This is intentional + } + static setConfigurationChangeCallback(cb: () => Promise): void { Configuration.configurationChangeCallback = cb; } @@ -59,10 +64,10 @@ export default class Configuration { }, }; if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) { - uiServerConfiguration = { - ...uiServerConfiguration, - ...Configuration.getConfig().uiServer, - }; + uiServerConfiguration = Configuration.deepMerge( + uiServerConfiguration, + Configuration.getConfig().uiServer + ); } return uiServerConfiguration; } @@ -383,6 +388,33 @@ export default class Configuration { } } + private static isObject(item): boolean { + return item && typeof item === 'object' && !Array.isArray(item); + } + + private static deepMerge(target: object, ...sources: object[]): object { + if (!sources.length) { + return target; + } + const source = sources.shift(); + + if (Configuration.isObject(target) && Configuration.isObject(source)) { + for (const key in source) { + if (Configuration.isObject(source[key])) { + if (!target[key]) { + Object.assign(target, { [key]: {} }); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + Configuration.deepMerge(target[key], source[key]); + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + Object.assign(target, { [key]: source[key] }); + } + } + } + return Configuration.deepMerge(target, ...sources); + } + private static objectHasOwnProperty(object: unknown, property: string): boolean { return Object.prototype.hasOwnProperty.call(object, property) as boolean; }