aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/serverLib/ConfigManager.js
blob: bb414be5dd5d9985aea23108b17354ef7c015278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import dateFormat from 'dateformat';
import {
  existsSync,
  ensureFileSync,
  readJsonSync,
  copySync,
  writeJSONSync,
  removeSync,
} from 'fs-extra';
import { resolve } from 'path';

/**
  * Server configuration manager, handling loading, creation, parsing and saving
  * of the main config.json file
  * @property {String} base - Base path that all imports are required in from
  * @author Marzavec ( https://github.com/marzavec )
  * @version v2.0.0
  * @license WTFPL ( http://www.wtfpl.net/txt/copying/ )
  */
class ConfigManager {
  /**
    * Create a `ConfigManager` instance for managing application settings
    * @param {String} basePath executing directory name; __dirname
    */
  constructor(basePath = __dirname) {
    /**
      * Full path to config.json file
      * @type {String}
      */
    this.configPath = resolve(basePath, 'config/config.json');

    if (!existsSync(this.configPath)) {
      ensureFileSync(this.configPath);
    }
  }

  /**
    * Loads config.json (main server config) into memory
    * @public
    * @return {(JSON|Boolean)} False if the config.json could not be loaded
    */
  async load() {
    try {
      this.config = readJsonSync(this.configPath);
    } catch (e) {
      return false;
    }

    return this.config;
  }

  /**
    * Creates backup of current config into configPath
    * @private
    * @return {String} Backed up config.json path
    */
  backup() {
    const backupPath = `${this.configPath}.${dateFormat('dd-mm-yy-HH-MM-ss')}.bak`;
    copySync(this.configPath, backupPath);

    return backupPath;
  }

  /**
    * First makes a backup of the current `config.json`, then writes current config
    * to disk
    * @public
    * @return {Boolean} False on failure
    */
  save() {
    const backupPath = this.backup();

    try {
      writeJSONSync(this.configPath, this.config, {
        // Indent with two spaces
        spaces: 2,
      });
      removeSync(backupPath);

      return true;
    } catch (err) {
      console.log(`Failed to save config file: ${err}`);

      return false;
    }
  }

  /**
    * Updates current config[`key`] with `value` then writes changes to disk
    * @param {*} key arbitrary configuration key
    * @param {*} value new value to change `key` to
    * @public
    * @return {Boolean} False on failure
    */
  set(key, value) {
    const realKey = `${key}`;
    this.config[realKey] = value;

    return this.save();
  }
}

export default ConfigManager;