summaryrefslogtreecommitdiff
path: root/server/js/config.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/js/config.js')
-rw-r--r--server/js/config.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/server/js/config.js b/server/js/config.js
new file mode 100644
index 0000000..5e0bc7b
--- /dev/null
+++ b/server/js/config.js
@@ -0,0 +1,57 @@
+"use strict";
+
+// Imports
+const fs = require('fs');
+const path = require('path');
+const { ensureFolder, envPaths } = require('./paths');
+
+// Handling Variables
+const indentation = '\t';
+const configFolder = ensureFolder(path.join(envPaths.CONFIG, 'sneedbay'));
+const defaultConfigPath = path.join(configFolder, 'config.json');
+const defaultConfig = {
+ 'settings': {
+ 'nsfw_content': false,
+ 'port': undefined
+ }
+};
+var loadedConfig = {};
+
+// Functions
+function createConfig(customConfig, configPath)
+{
+ const path = configPath || defaultConfigPath;
+ const config = customConfig || defaultConfig;
+
+ try
+ {
+ fs.writeFileSync(path, JSON.stringify(config, null, indentation), 'utf-8');
+ }
+
+ catch (err)
+ {
+ console.log(err);
+ }
+}
+
+function loadConfig(configPath)
+{
+ const path = configPath || defaultConfigPath;
+
+ if (configPath && !fs.existsSync(path))
+ {
+ createConfig(defaultConfig, path);
+ }
+
+ try
+ {
+ this.loadedConfig = JSON.parse(fs.readFileSync(path));
+ }
+
+ catch (err)
+ {
+ console.log(err);
+ }
+}
+
+module.exports = { loadConfig, defaultConfigPath, loadedConfig, createConfig }; \ No newline at end of file