Select Git revision
CompletionFishCommand.ts
LocalConfigFile.ts 1.57 KiB
import * as fs from 'fs';
import JSON5 from 'json5';
class LocalConfigFile {
private readonly folder: string;
private readonly filename: string;
constructor(folder: string, filename: string) {
this.folder = folder;
this.filename = filename;
this.loadConfig();
}
private get configPath(): string {
return `${ this.folder }/${ this.filename }`;
}
private _config: { [key: string]: unknown } = {};
loadConfig() {
if ( !fs.existsSync(this.configPath) ) {
fs.mkdirSync(this.folder, { recursive: true });
fs.writeFileSync(this.configPath, JSON5.stringify({}));
}
try {
this._config = JSON5.parse(fs.readFileSync(this.configPath).toString());
} catch ( error ) {
console.log(error);
}
}
getParam(key: string): unknown {
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;
}
}
setParam(key: string, value: unknown): void {
const 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 ) {
console.log(error);
}
}
}
export default LocalConfigFile;