Add CLI option --config/-c to specify config file
parent
aee9cde40a
commit
e9893fe56e
|
@ -5645,8 +5645,6 @@
|
||||||
"requires": {
|
"requires": {
|
||||||
"ansi-regex": "^5.0.0"
|
"ansi-regex": "^5.0.0"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"yargs-parser": {
|
"yargs-parser": {
|
||||||
"version": "18.1.3",
|
"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=="
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,8 @@
|
||||||
"sqlite3": "^5.0.0",
|
"sqlite3": "^5.0.0",
|
||||||
"tmp": "^0.1.0",
|
"tmp": "^0.1.0",
|
||||||
"transliteration": "^2.1.11",
|
"transliteration": "^2.1.11",
|
||||||
"uuid": "^8.3.0"
|
"uuid": "^8.3.0",
|
||||||
|
"yargs-parser": "^20.2.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^7.7.0",
|
"eslint": "^7.7.0",
|
||||||
|
|
47
src/cfg.js
47
src/cfg.js
|
@ -2,12 +2,13 @@ const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const Ajv = require("ajv");
|
const Ajv = require("ajv");
|
||||||
const schema = require("./data/cfg.schema.json");
|
const schema = require("./data/cfg.schema.json");
|
||||||
|
const cliOpts = require("./cliOpts");
|
||||||
|
|
||||||
/** @type {ModmailConfig} */
|
/** @type {ModmailConfig} */
|
||||||
let config = {};
|
let config = {};
|
||||||
|
|
||||||
// Config files to search for, in priority order
|
// Auto-detected config files, in priority order
|
||||||
const configFiles = [
|
const configFilesToSearch = [
|
||||||
"config.ini",
|
"config.ini",
|
||||||
"config.json",
|
"config.json",
|
||||||
"config.json5",
|
"config.json5",
|
||||||
|
@ -20,24 +21,46 @@ const configFiles = [
|
||||||
"config.json.txt",
|
"config.json.txt",
|
||||||
];
|
];
|
||||||
|
|
||||||
let foundConfigFile;
|
let configFileToLoad;
|
||||||
for (const configFile of configFiles) {
|
|
||||||
|
const requestedConfigFile = cliOpts.config || cliOpts.c; // CLI option --config/-c
|
||||||
|
if (requestedConfigFile) {
|
||||||
try {
|
try {
|
||||||
fs.accessSync(__dirname + "/../" + configFile);
|
// Config files specified with --config/-c are loaded from cwd
|
||||||
foundConfigFile = configFile;
|
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;
|
break;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load config values from a config file (if any)
|
// Load config values from a config file (if any)
|
||||||
if (foundConfigFile) {
|
if (configFileToLoad) {
|
||||||
console.log(`Loading configuration from ${foundConfigFile}...`);
|
const srcRelativePath = path.resolve(__dirname, process.cwd(), configFileToLoad);
|
||||||
|
console.log(`Loading configuration from ${configFileToLoad}...`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (foundConfigFile.endsWith(".js")) {
|
if (configFileToLoad.endsWith(".js")) {
|
||||||
config = require(`../${foundConfigFile}`);
|
config = require(srcRelativePath);
|
||||||
} else {
|
} else {
|
||||||
const raw = fs.readFileSync(__dirname + "/../" + foundConfigFile, {encoding: "utf8"});
|
const raw = fs.readFileSync(configFileToLoad, {encoding: "utf8"});
|
||||||
if (foundConfigFile.endsWith(".ini") || foundConfigFile.endsWith(".ini.txt")) {
|
if (configFileToLoad.endsWith(".ini") || configFileToLoad.endsWith(".ini.txt")) {
|
||||||
config = require("ini").decode(raw);
|
config = require("ini").decode(raw);
|
||||||
} else {
|
} else {
|
||||||
config = require("json5").parse(raw);
|
config = require("json5").parse(raw);
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
module.exports = require("yargs-parser")(process.argv.slice(2));
|
Loading…
Reference in New Issue