Skip to content
Snippets Groups Projects
Commit fd5ab6eb authored by michael.minelli's avatar michael.minelli
Browse files

LocalConfig => Move for singleton to a generic class for local config files

parent 11c4e397
No related branches found
No related tags found
No related merge requests found
import * as fs from 'fs'; import * as fs from 'fs';
import SessionManager from '../managers/SessionManager';
import Config from './Config'; import Config from './Config';
import LocalConfigKeys from '../types/LocalConfigKeys';
import GitlabManager from '../managers/GitlabManager';
import JSON5 from 'json5'; import JSON5 from 'json5';
import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig';
class LocalConfigFile { class LocalConfigFile {
constructor(private filename: string) {} constructor(private filename: string) {}
private get configPath(): 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() { loadConfig() {
if ( !fs.existsSync(this.configPath) ) { if ( !fs.existsSync(this.configPath) ) {
...@@ -26,38 +21,37 @@ class LocalConfigFile { ...@@ -26,38 +21,37 @@ class LocalConfigFile {
try { try {
this._config = JSON5.parse(fs.readFileSync(this.configPath).toString()); 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 ) { } catch ( error ) {
console.log(error); console.log(error);
} }
} }
updateConfig(key: LocalConfigKeys, value: any) { getParam(key: string): any | null {
let previousValue = (this._config as any)[key]; const value = key in this._config ? this._config[key] : null;
if ( previousValue === value ) { if ( value === null ) {
return; 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; setParam(key: string, value: any): void {
} else { let previousValue = this.getParam(key);
(this._config as any)[key] = value; if ( JSON5.stringify(previousValue) === JSON5.stringify(value) ) {
return;
} }
this._config[key] = value;
try { try {
fs.writeFileSync(this.configPath, JSON5.stringify(this._config, null, 4)); fs.writeFileSync(this.configPath, JSON5.stringify(this._config, null, 4));
} catch ( error ) { } } catch ( error ) {
console.log(error);
}
} }
} }
export default new LocalConfig(); export default LocalConfigFile;
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment