-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_config.js
More file actions
66 lines (62 loc) · 2.01 KB
/
_config.js
File metadata and controls
66 lines (62 loc) · 2.01 KB
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
63
64
65
66
const {readfile, writefile} = require('./_file.js')
let defaultConfig
var config = {}
function getfield(datafile, field) {
if (field)
if (field in config[datafile])
return config[datafile][field]
else
if (field in defaultConfig)
return defaultConfig[field]
else
return null
else
return config[datafile]
}
function getconfig(datafile, field, funcresponse) {
if (!(datafile in config)) {
readfile(`./data/${datafile}.json`,
function(data) {
try {
config[datafile] = JSON.parse(data)
} catch(err) {
console.log(`Error while decoding JSON from ./data/${datafile}.json`)
console.log(err)
}
funcresponse(getfield(datafile, field))
},
function(err) {
config[datafile] = defaultConfig
funcresponse(getfield(datafile, field))
}
)
} else
funcresponse(getfield(datafile, field))
}
module.exports.getconfig = getconfig
function editconfig(datafile, field, data, responsefunc) {
// Prevent editing Defalut config
if (datafile === 'Default') return
// Use getconfig to automatically load config
getconfig(datafile, null, function(dt){
config[datafile][field] = data
writefile(__dirname + `/data/${datafile}.json`, JSON.stringify(config[datafile]), function(err) {
if (err) {
console.log(`Error while saving JSON to ./data/${datafile}.json`)
console.log(err)
}
if (responsefunc)
responsefunc(err)
})
})
}
module.exports.editconfig = editconfig
// Load default config... by default
if(!defaultConfig){
getconfig('Default', null, function(conf) {
defaultConfig = conf
console.log(conf)
console.log('Default config initalized.')
module.exports.defaultConfig = conf
})
}