aboutsummaryrefslogtreecommitdiffstats
path: root/lib/persist.js
blob: 24d00693d9b4e1ba8cf4d6bc49c2c80aac20b3d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fs = require("fs");

module.exports = (path, initial = {}) => {
  try {
    return require("../" + path);
  } catch {
    return new Proxy(initial, {
      set(obj, prop, value) {
        fs.writeFileSync(
          path,
          JSON.stringify({ ...obj, [prop]: value }, null, 2),
          "utf-8"
        );
        return Reflect.set(...arguments);
      }
    });
  }
};