forked from arnosolo/send_ios_photos_to_win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStore.js
34 lines (29 loc) · 862 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
const electron = require('electron');
const path = require('path');
const fs = require('fs');
/* 用于读写配置文件 */
class Store {
constructor(opts) {
// renderer has to get `app` module via remote, main gets it directly
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
this.path = path.join(userDataPath, opts.configName + '.json');
this.data = parseDataFile(this.path, opts.defaults);
this.opts = opts
}
get(key) {
this.data = parseDataFile(this.path, this.opts.defaults);
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;