MCPcopy Create free account
hub / github.com/GooseMod/OpenAsar / Settings

Class Settings

src/appSettings.js:3–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1const fs = require('fs');
2
3class 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
45let inst; // Instance of class
46exports.getSettings = () => inst = inst ?? new Settings(require('path').join(require('./paths').getUserData(), 'settings.json'));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected