2017-11-22 10:48:40 +00:00
|
|
|
const fs = require('fs')
|
2017-11-27 09:31:54 +00:00
|
|
|
const mkpath = require('mkdirp')
|
2017-11-22 10:48:40 +00:00
|
|
|
const path = require('path')
|
|
|
|
const uuidV4 = require('uuid/v4')
|
|
|
|
|
|
|
|
class ConfigManager {
|
|
|
|
|
|
|
|
constructor(path){
|
|
|
|
this.path = path
|
|
|
|
this.config = null
|
|
|
|
this.load()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a default configuration object and saves it.
|
|
|
|
*
|
|
|
|
* @param {Boolean} save - optional. If true, the default config will be saved after being generated.
|
|
|
|
*/
|
|
|
|
_generateDefault(save = true){
|
|
|
|
this.config = {
|
|
|
|
settings: {},
|
|
|
|
clientToken: uuidV4(),
|
|
|
|
authenticationDatabase: []
|
|
|
|
}
|
|
|
|
if(save){
|
|
|
|
this.save()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load(){
|
|
|
|
if(!fs.existsSync(this.path)){
|
2017-11-27 09:31:54 +00:00
|
|
|
mkpath.sync(path.join(this.path, '..'))
|
2017-11-22 10:48:40 +00:00
|
|
|
this._generateDefault()
|
|
|
|
} else {
|
|
|
|
this.config = JSON.parse(fs.readFileSync(this.path, 'UTF-8'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save(){
|
|
|
|
fs.writeFileSync(this.path, JSON.stringify(this.config, null, 4), 'UTF-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
getClientToken(){
|
|
|
|
return this.config.clientToken
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ConfigManager
|