| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 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
|