From cd96c70f1c85e493e5b810753f1438d7922cb379 Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 3 May 2018 19:55:21 +0300 Subject: [PATCH] Switch config parser to JSON5. Accept multiple config file names. --- .gitignore | 3 +++ package-lock.json | 15 +++++++++++++++ package.json | 1 + src/config.js | 30 ++++++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e3cfe6d..4de0866 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,8 @@ /.idea /node_modules /config.json +/config.json5 +/config.json.json +/config.json.txt /welcome.png /update.sh diff --git a/package-lock.json b/package-lock.json index 4eb91f6..93442e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index e7f718f..f6c5149 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/config.js b/src/config.js index 1f835e3..7208612 100644 --- a/src/config.js +++ b/src/config.js @@ -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 = {