Add CLI option --config/-c to specify config file

cshd
Dragory 2021-01-07 20:00:04 +02:00
parent aee9cde40a
commit e9893fe56e
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
4 changed files with 52 additions and 22 deletions

9
package-lock.json generated
View File

@ -5645,8 +5645,6 @@
"requires": {
"ansi-regex": "^5.0.0"
}
}
}
},
"yargs-parser": {
"version": "18.1.3",
@ -5658,4 +5656,11 @@
}
}
}
},
"yargs-parser": {
"version": "20.2.4",
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.4.tgz",
"integrity": "sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA=="
}
}
}

View File

@ -38,7 +38,8 @@
"sqlite3": "^5.0.0",
"tmp": "^0.1.0",
"transliteration": "^2.1.11",
"uuid": "^8.3.0"
"uuid": "^8.3.0",
"yargs-parser": "^20.2.4"
},
"devDependencies": {
"eslint": "^7.7.0",

View File

@ -2,12 +2,13 @@ const fs = require("fs");
const path = require("path");
const Ajv = require("ajv");
const schema = require("./data/cfg.schema.json");
const cliOpts = require("./cliOpts");
/** @type {ModmailConfig} */
let config = {};
// Config files to search for, in priority order
const configFiles = [
// Auto-detected config files, in priority order
const configFilesToSearch = [
"config.ini",
"config.json",
"config.json5",
@ -20,24 +21,46 @@ const configFiles = [
"config.json.txt",
];
let foundConfigFile;
for (const configFile of configFiles) {
let configFileToLoad;
const requestedConfigFile = cliOpts.config || cliOpts.c; // CLI option --config/-c
if (requestedConfigFile) {
try {
fs.accessSync(__dirname + "/../" + configFile);
foundConfigFile = configFile;
// Config files specified with --config/-c are loaded from cwd
fs.accessSync(requestedConfigFile);
configFileToLoad = requestedConfigFile;
} catch (e) {
if (e.code === "ENOENT") {
console.error(`Specified config file was not found: ${requestedConfigFile}`);
} else {
console.error(`Error reading specified config file ${requestedConfigFile}: ${e.message}`);
}
process.exit(1);
}
} else {
for (const configFile of configFilesToSearch) {
try {
// Auto-detected config files are always loaded from the bot's folder, even if the cwd differs
const relativePath = path.relative(process.cwd(), path.resolve(__dirname, "..", configFile));
fs.accessSync(relativePath);
configFileToLoad = relativePath;
break;
} catch (e) {}
}
}
// Load config values from a config file (if any)
if (foundConfigFile) {
console.log(`Loading configuration from ${foundConfigFile}...`);
if (configFileToLoad) {
const srcRelativePath = path.resolve(__dirname, process.cwd(), configFileToLoad);
console.log(`Loading configuration from ${configFileToLoad}...`);
try {
if (foundConfigFile.endsWith(".js")) {
config = require(`../${foundConfigFile}`);
if (configFileToLoad.endsWith(".js")) {
config = require(srcRelativePath);
} else {
const raw = fs.readFileSync(__dirname + "/../" + foundConfigFile, {encoding: "utf8"});
if (foundConfigFile.endsWith(".ini") || foundConfigFile.endsWith(".ini.txt")) {
const raw = fs.readFileSync(configFileToLoad, {encoding: "utf8"});
if (configFileToLoad.endsWith(".ini") || configFileToLoad.endsWith(".ini.txt")) {
config = require("ini").decode(raw);
} else {
config = require("json5").parse(raw);

1
src/cliOpts.js Normal file
View File

@ -0,0 +1 @@
module.exports = require("yargs-parser")(process.argv.slice(2));