-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.js
More file actions
33 lines (28 loc) · 727 Bytes
/
Store.js
File metadata and controls
33 lines (28 loc) · 727 Bytes
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
const electron = require('electron');
const path = require('path');
const os = require('os');
const fs = require('fs');
class Store {
constructor(options) {
const userDataPath = (electron.app || electron.remote.app).getPath(
'userData'
);
this.path = path.join(userDataPath, options.configName + '.json');
this.data = parseDatafile(this.path, options.defaults);
}
get(key) {
return this.data[key];
}
set(key, value) {
this.data[key] = value;
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;