From fd5ab6ebe086ad6d844fb76f9af0e0c565c2da7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Thu, 2 Nov 2023 13:37:15 +0100 Subject: [PATCH] LocalConfig => Move for singleton to a generic class for local config files --- NodeApp/src/config/LocalConfigFile.ts | 54 ++++++++++++--------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/NodeApp/src/config/LocalConfigFile.ts b/NodeApp/src/config/LocalConfigFile.ts index 5964792..33cabe2 100644 --- a/NodeApp/src/config/LocalConfigFile.ts +++ b/NodeApp/src/config/LocalConfigFile.ts @@ -1,21 +1,16 @@ -import * as fs from 'fs'; -import SessionManager from '../managers/SessionManager'; -import Config from './Config'; -import LocalConfigKeys from '../types/LocalConfigKeys'; -import GitlabManager from '../managers/GitlabManager'; -import JSON5 from 'json5'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; +import * as fs from 'fs'; +import Config from './Config'; +import JSON5 from 'json5'; class LocalConfigFile { - constructor(private filename: string) {} private get configPath(): string { - return `${ Config.localConfig.folder }/${ Config.localConfig.file }`; + return `${ Config.localConfig.folder }/${ this.filename }`; } - private _config: { [key in LocalConfigKeys]?: any } = {}; + private _config: { [key: string]: any } = {}; loadConfig() { if ( !fs.existsSync(this.configPath) ) { @@ -26,38 +21,37 @@ class LocalConfigFile { try { this._config = JSON5.parse(fs.readFileSync(this.configPath).toString()); - - if ( LocalConfigKeys.API_TOKEN in this._config && ClientsSharedConfig.apiURL in this._config[LocalConfigKeys.API_TOKEN] ) { - SessionManager.token = this._config[LocalConfigKeys.API_TOKEN][ClientsSharedConfig.apiURL]; - } - - GitlabManager.config = this._config[LocalConfigKeys.GITLAB]; } catch ( error ) { console.log(error); } } - updateConfig(key: LocalConfigKeys, value: any) { - let previousValue = (this._config as any)[key]; - if ( previousValue === value ) { - return; + getParam(key: string): any | null { + const value = key in this._config ? this._config[key] : null; + if ( value === null ) { + return null; + } else if ( typeof value === 'object' ) { + return { ...value }; + } else { + return value; } + } - if ( key === LocalConfigKeys.API_TOKEN ) { - if ( !(LocalConfigKeys.API_TOKEN in this._config) ) { - (this._config as any)[LocalConfigKeys.API_TOKEN] = {}; - } - - (this._config as any)[LocalConfigKeys.API_TOKEN][ClientsSharedConfig.apiURL] = value; - } else { - (this._config as any)[key] = value; + setParam(key: string, value: any): void { + let previousValue = this.getParam(key); + if ( JSON5.stringify(previousValue) === JSON5.stringify(value) ) { + return; } + this._config[key] = value; + try { fs.writeFileSync(this.configPath, JSON5.stringify(this._config, null, 4)); - } catch ( error ) { } + } catch ( error ) { + console.log(error); + } } } -export default new LocalConfig(); \ No newline at end of file +export default LocalConfigFile; \ No newline at end of file -- GitLab