summaryrefslogtreecommitdiff
path: root/server/js/config.js
blob: 5e0bc7bfdb077304d75561b0f2b979ee41e0f4e9 (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
"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 };