| 1 | const fs = require('fs'); |
| 2 | |
| 3 | class Settings { // Heavily based on original for compat, but simplified and tweaked |
| 4 | constructor(path) { |
| 5 | try { |
| 6 | this.store = JSON.parse(fs.readFileSync(path)); |
| 7 | } catch { |
| 8 | this.store = {}; |
| 9 | } |
| 10 | |
| 11 | this.path = path; |
| 12 | this.mod = this.getMod(); |
| 13 | |
| 14 | log('Settings', this.path, this.store); |
| 15 | } |
| 16 | |
| 17 | getMod() { // Get when file was last modified |
| 18 | try { |
| 19 | return fs.statSync(this.path).mtime.getTime(); |
| 20 | } catch { } |
| 21 | } |
| 22 | |
| 23 | get(k, d) { |
| 24 | return this.store[k] ?? d; |
| 25 | } |
| 26 | |
| 27 | set(k, v) { |
| 28 | this.store[k] = v; |
| 29 | } |
| 30 | |
| 31 | save() { |
| 32 | if (this.mod && this.mod !== this.getMod()) return; // File was last modified after Settings was made, so was externally edited therefore we don't save over |
| 33 | |
| 34 | try { |
| 35 | fs.writeFileSync(this.path, JSON.stringify(this.store, null, 2)); |
| 36 | this.mod = this.getMod(); |
| 37 | |
| 38 | log('Settings', 'Saved'); |
| 39 | } catch (e) { |
| 40 | log('Settings', e); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | let inst; // Instance of class |
| 46 | exports.getSettings = () => inst = inst ?? new Settings(require('path').join(require('./paths').getUserData(), 'settings.json')); |
nothing calls this directly
no outgoing calls
no test coverage detected