Switch config parser to JSON5. Accept multiple config file names.

master
Dragory 2018-05-03 19:55:21 +03:00
parent 685ccdd226
commit cd96c70f1c
4 changed files with 47 additions and 2 deletions

3
.gitignore vendored
View File

@ -2,5 +2,8 @@
/.idea
/node_modules
/config.json
/config.json5
/config.json.json
/config.json.txt
/welcome.png
/update.sh

15
package-lock.json generated
View File

@ -1279,6 +1279,21 @@
"jsonify": "0.0.0"
}
},
"json5": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz",
"integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==",
"requires": {
"minimist": "1.2.0"
},
"dependencies": {
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
}
}
},
"jsonify": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",

View File

@ -13,6 +13,7 @@
"dependencies": {
"eris": "^0.8.4",
"humanize-duration": "^3.12.1",
"json5": "^1.0.1",
"knex": "^0.14.2",
"mime": "^1.3.4",
"moment": "^2.21.0",

View File

@ -1,11 +1,37 @@
const json5 = require('json5');
const fs = require('fs');
const path = require('path');
let userConfig;
// Try to find our config file from several options
const configFiles = [
'config.json',
'config.json5',
'config.json.json',
'config.json.txt'
];
let foundConfigFile;
for (const configFile of configFiles) {
try {
fs.accessSync(__dirname + '/../' + configFile);
foundConfigFile = configFile;
break;
} catch (e) {}
}
if (! foundConfigFile) {
throw new Error(`Could not find config.json!`);
}
// Parse the config using JSON5
try {
userConfig = require('../config');
const raw = fs.readFileSync(__dirname + '/../' + foundConfigFile);
userConfig = json5.parse(raw);
} catch (e) {
throw new Error(`Config file could not be found or read! The error given was: ${e.message}`);
throw new Error(`Error reading config file! The error given was: ${e.message}`);
}
const defaultConfig = {