configmanager.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const fs = require('fs')
  2. const mkpath = require('mkdirp')
  3. const path = require('path')
  4. const uuidV4 = require('uuid/v4')
  5. class ConfigManager {
  6. constructor(path){
  7. this.path = path
  8. this.config = null
  9. this.load()
  10. }
  11. /**
  12. * Generates a default configuration object and saves it.
  13. *
  14. * @param {Boolean} save - optional. If true, the default config will be saved after being generated.
  15. */
  16. _generateDefault(save = true){
  17. this.config = {
  18. settings: {},
  19. clientToken: uuidV4(),
  20. authenticationDatabase: []
  21. }
  22. if(save){
  23. this.save()
  24. }
  25. }
  26. load(){
  27. if(!fs.existsSync(this.path)){
  28. mkpath.sync(path.join(this.path, '..'))
  29. this._generateDefault()
  30. } else {
  31. this.config = JSON.parse(fs.readFileSync(this.path, 'UTF-8'))
  32. }
  33. }
  34. save(){
  35. fs.writeFileSync(this.path, JSON.stringify(this.config, null, 4), 'UTF-8')
  36. }
  37. getClientToken(){
  38. return this.config.clientToken
  39. }
  40. }
  41. module.exports = ConfigManager