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 /.idea
/node_modules /node_modules
/config.json /config.json
/config.json5
/config.json.json
/config.json.txt
/welcome.png /welcome.png
/update.sh /update.sh

15
package-lock.json generated
View File

@ -1279,6 +1279,21 @@
"jsonify": "0.0.0" "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": { "jsonify": {
"version": "0.0.0", "version": "0.0.0",
"resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz",

View File

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

View File

@ -1,11 +1,37 @@
const json5 = require('json5');
const fs = require('fs');
const path = require('path'); const path = require('path');
let userConfig; 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 { try {
userConfig = require('../config'); const raw = fs.readFileSync(__dirname + '/../' + foundConfigFile);
userConfig = json5.parse(raw);
} catch (e) { } 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 = { const defaultConfig = {