-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.js
More file actions
33 lines (27 loc) · 684 Bytes
/
Store.js
File metadata and controls
33 lines (27 loc) · 684 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 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, 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 (err) {
return defaults
}
}
module.exports = Store