configmanager.js 1.0 KB

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