Show bot version in console on start-up

cshd
Dragory 2020-11-22 12:54:44 +02:00
parent 37cba80ed9
commit 3f3de28091
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
5 changed files with 57 additions and 39 deletions

5
src/BotError.js Normal file
View File

@ -0,0 +1,5 @@
class BotError extends Error {}
module.exports = {
BotError,
};

40
src/botVersion.js Normal file
View File

@ -0,0 +1,40 @@
const fs = require("fs");
const path = require("path");
const gitDir = path.resolve(__dirname, "..", ".git");
function getPackageVersion() {
const packageJson = require("../package.json");
return packageJson.version;
}
function getHeadCommitHash() {
try {
fs.accessSync(gitDir);
} catch (e) {
return null;
}
// Find HEAD ref and read the commit hash from that ref
const headRefInfo = fs.readFileSync(path.resolve(gitDir, "HEAD"), { encoding: "utf8" });
if (headRefInfo.startsWith("ref:")) {
const refPath = headRefInfo.slice(5).trim(); // ref: refs/heads/... to refs/heads/...
return fs.readFileSync(path.resolve(gitDir, refPath), { encoding: "utf8" }).trim();
} else {
// Detached head, just the commit hash
return headRefInfo.trim();
}
}
function getPrettyVersion() {
const packageVersion = getPackageVersion();
const headCommitHash = getHeadCommitHash();
return headCommitHash
? `v${packageVersion} (${headCommitHash.slice(0, 8)})`
: packageVersion;
}
module.exports = {
getPrettyVersion,
};

View File

@ -16,7 +16,7 @@ try {
process.exit(1);
}
const utils = require("./utils");
const { BotError } = require("./BotError");
// Error handling
// Force crash on unhandled rejections and uncaught exceptions.
@ -32,7 +32,7 @@ function errorHandler(err) {
if (err) {
if (typeof err === "string") {
console.error(`Error: ${err}`);
} else if (err instanceof utils.BotError) {
} else if (err instanceof BotError) {
// Leave out stack traces for BotErrors (the message has enough info)
console.error(`Error: ${err.message}`);
} else if (err.message === "Disallowed intents specified") {
@ -72,6 +72,9 @@ function errorHandler(err) {
process.on("uncaughtException", errorHandler);
process.on("unhandledRejection", errorHandler);
const { getPrettyVersion } = require("./botVersion");
console.log(`Starting Modmail ${getPrettyVersion()}`);
let testedPackage = "";
try {
const packageJson = require("../package.json");
@ -85,11 +88,11 @@ try {
process.exit(1);
}
const config = require("./cfg");
const main = require("./main");
const knex = require("./knex");
(async function() {
require("./cfg");
const main = require("./main");
const knex = require("./knex");
// Make sure the database is up to date
const [completed, newMigrations] = await knex.migrate.list();
if (newMigrations.length > 0) {

View File

@ -3,7 +3,7 @@ const fs = require("fs");
const {promisify} = require("util");
const utils = require("../utils");
const updates = require("../data/updates");
const config = require("../cfg");
const { getPrettyVersion } = require("../botVersion");
const access = promisify(fs.access);
const readFile = promisify(fs.readFile);
@ -12,34 +12,7 @@ const GIT_DIR = path.join(__dirname, "..", "..", ".git");
module.exports = ({ bot, knex, config, commands }) => {
commands.addInboxServerCommand("version", [], async (msg, args, thread) => {
const packageJson = require("../../package.json");
const packageVersion = packageJson.version;
let response = `Modmail v${packageVersion}`;
let isGit;
try {
await access(GIT_DIR);
isGit = true;
} catch (e) {
isGit = false;
}
if (isGit) {
let commitHash;
const HEAD = await readFile(path.join(GIT_DIR, "HEAD"), {encoding: "utf8"});
if (HEAD.startsWith("ref:")) {
// Branch
const ref = HEAD.match(/^ref: (.*)$/m)[1];
commitHash = (await readFile(path.join(GIT_DIR, ref), {encoding: "utf8"})).trim();
} else {
// Detached head
commitHash = HEAD.trim();
}
response += ` (${commitHash.slice(0, 7)})`;
}
let response = `Modmail ${getPrettyVersion()}`;
if (config.updateNotifications) {
const availableUpdate = await updates.getAvailableUpdate();

View File

@ -4,8 +4,7 @@ const moment = require("moment");
const humanizeDuration = require("humanize-duration");
const publicIp = require("public-ip");
const config = require("./cfg");
class BotError extends Error {}
const { BotError } = require("./BotError");
const userMentionRegex = /^<@!?([0-9]+?)>$/;
@ -492,8 +491,6 @@ function chunkMessageLines(str, maxChunkLength = 1990) {
}
module.exports = {
BotError,
getInboxGuild,
getMainGuilds,
getLogChannel,