[AppSettings] Minor source cleanup

This commit is contained in:
Ducko 2022-04-22 18:01:15 +01:00
parent 06b35cbc1d
commit 864df199ac
1 changed files with 12 additions and 11 deletions

View File

@ -2,17 +2,18 @@ const fs = require('fs');
class Settings { // Heavily based on original for compat, but simplified and tweaked class Settings { // Heavily based on original for compat, but simplified and tweaked
constructor(path) { constructor(path) {
this.path = path;
try { try {
this.store = JSON.parse(fs.readFileSync(this.path)); this.store = JSON.parse(fs.readFileSync(path));
} catch (e) { } catch (e) {
this.store = {}; this.store = {};
} }
this.mod = this.getMod(); Object.assign(this, {
path,
mod: this.getMod()
})
log('AppSettings', this.path, this.store); log('Settings', this.path, this.store);
} }
getMod() { // Get when file was last modified getMod() { // Get when file was last modified
@ -21,12 +22,12 @@ class Settings { // Heavily based on original for compat, but simplified and twe
} catch { } } catch { }
} }
get(key, defaultValue) { get(k, d) {
return this.store[key] ?? defaultValue; return this.store[k] ?? d;
} }
set(key, value) { set(k, v) {
this.store[key] = value; this.store[k] = v;
} }
save() { save() {
@ -36,9 +37,9 @@ class Settings { // Heavily based on original for compat, but simplified and twe
fs.writeFileSync(this.path, JSON.stringify(this.store, null, 2)); fs.writeFileSync(this.path, JSON.stringify(this.store, null, 2));
this.mod = this.getMod(); this.mod = this.getMod();
log('AppSettings', 'Saved'); log('Settings', 'Saved');
} catch (e) { } catch (e) {
log('AppSettings', e); log('Settings', e);
} }
} }
} }