Minor fixes, beginning work on configuration system.

This commit is contained in:
Daniel Scalzi
2017-11-22 05:48:40 -05:00
parent 2c7dc16247
commit 5352239f91
4 changed files with 75 additions and 19 deletions

View File

@@ -0,0 +1,47 @@
const fs = require('fs')
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)){
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