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 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';
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment