-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.js
62 lines (53 loc) · 1.17 KB
/
config.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var fs = require('fs')
var electron = require('electron')
module.exports = Config
function Config (loc) {
if (!(this instanceof Config)) return new Config(loc)
this.loc = loc
this.dats = {}
}
Config.prototype.update = function (dat) {
this.dats[dat.path] = dat
this.write()
}
Config.prototype.get = function (key) {
return this.dats[key]
}
Config.prototype.del = function (key) {
delete this.dats[key]
this.write()
}
Config.prototype.read = function () {
var self = this
var data, conf
try {
data = fs.readFileSync(self.loc)
} catch (e) {
if (e.code === 'ENOENT') {
self.write()
return self.read()
} else {
throw e
}
}
try {
conf = JSON.parse(data.toString())
} catch (e) {
var code = electron.dialog.showMessageBox({
message: 'Invalid configuration file\nCould not parse JSON',
detail: e.stack,
buttons: ['Reload Config', 'Exit app']
})
if (code === 0) {
return self.read()
} else {
throw e
}
}
self.dats = conf.dats
return conf
}
Config.prototype.write = function () {
var self = this
fs.writeFileSync(self.loc, JSON.stringify({dats: self.dats}, null, 2))
}