-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstore.js
34 lines (27 loc) · 827 Bytes
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// save preferences
// https://medium.com/@ccnokes/how-to-store-user-data-in-electron-3ba6bf66bc1e
const electron = require("electron");
const path = require("path");
const fs = require("fs");
class Store {
constructor(opts) {
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
this.path = path.join(userDataPath, `${opts.configName}.json`);
this.data = parseDataFile(this.path, opts.defaults);
}
get(key) {
return this.data[key];
}
set(key, val) {
this.data[key] = val;
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
}
function parseDataFile(filePath, defaults) {
try {
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return defaults;
}
}
module.exports = Store;