aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/serverLib/ConfigManager.js
diff options
context:
space:
mode:
authormarzavec <admin@marzavec.com>2019-03-19 07:36:21 +0100
committermarzavec <admin@marzavec.com>2019-03-19 07:36:21 +0100
commitc634e03cb553e21158fddc6d4221a54aa799de79 (patch)
tree74f303d4ab65b08c7ec316fb713146cc99024c00 /server/src/serverLib/ConfigManager.js
parentMerge pull request #56 from paulgreg/manifest (diff)
downloadhackchat-c634e03cb553e21158fddc6d4221a54aa799de79.tar.gz
hackchat-c634e03cb553e21158fddc6d4221a54aa799de79.zip
refactoring 1 of 2
Diffstat (limited to 'server/src/serverLib/ConfigManager.js')
-rw-r--r--server/src/serverLib/ConfigManager.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/server/src/serverLib/ConfigManager.js b/server/src/serverLib/ConfigManager.js
new file mode 100644
index 0000000..ebec050
--- /dev/null
+++ b/server/src/serverLib/ConfigManager.js
@@ -0,0 +1,89 @@
+/**
+ * Server configuration manager, handling loading, creation, parsing and saving
+ * of the main config.json file
+ *
+ * Version: v2.0.0
+ * Developer: Marzavec ( https://github.com/marzavec )
+ * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
+ *
+ */
+const dateFormat = require('dateformat');
+const fse = require('fs-extra');
+const path = require('path');
+
+class ConfigManager {
+ /**
+ * Create a `ConfigManager` instance for managing application settings
+ *
+ * @param {String} base executing directory name; __dirname
+ */
+ constructor (base = __dirname) {
+ this.configPath = path.resolve(base, 'config/config.json');
+
+ if (!fse.existsSync(this.configPath)){
+ fse.ensureFileSync(this.configPath);
+ }
+ }
+
+ /**
+ * (Re)builds the config.json (main server config), or loads the config into mem
+ * if rebuilding, process will exit- this is to allow a process manager to take over
+ *
+ * @param {Boolean} reconfiguring set to true by `scripts/configure.js`, will exit if true
+ */
+ async load () {
+ try {
+ this.config = fse.readJsonSync(this.configPath);
+ } catch (e) {
+ return false;
+ }
+
+ return this.config;
+ }
+
+ /**
+ * Creates backup of current config into configPath
+ *
+ */
+ async backup () {
+ const backupPath = `${this.configPath}.${dateFormat('dd-mm-yy-HH-MM-ss')}.bak`;
+ fse.copySync(this.configPath, backupPath);
+
+ return backupPath;
+ }
+
+ /**
+ * First makes a backup of the current `config.json`, then writes current config
+ * to disk
+ *
+ */
+ async save () {
+ const backupPath = await this.backup();
+
+ try {
+ fse.writeJSONSync(this.configPath, this.config);
+ fse.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
+ */
+ async set (key, value) {
+ const realKey = `${key}`;
+ this.config[realKey] = value;
+
+ return await this.save();
+ }
+}
+
+module.exports = ConfigManager;