From 3f3de280910b8e6d55f9dfe552ae52acbb6236e0 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 22 Nov 2020 12:54:44 +0200 Subject: [PATCH] Show bot version in console on start-up --- src/BotError.js | 5 +++++ src/botVersion.js | 40 ++++++++++++++++++++++++++++++++++++++++ src/index.js | 15 +++++++++------ src/modules/version.js | 31 ++----------------------------- src/utils.js | 5 +---- 5 files changed, 57 insertions(+), 39 deletions(-) create mode 100644 src/BotError.js create mode 100644 src/botVersion.js diff --git a/src/BotError.js b/src/BotError.js new file mode 100644 index 0000000..2629e16 --- /dev/null +++ b/src/BotError.js @@ -0,0 +1,5 @@ +class BotError extends Error {} + +module.exports = { + BotError, +}; diff --git a/src/botVersion.js b/src/botVersion.js new file mode 100644 index 0000000..cdfa315 --- /dev/null +++ b/src/botVersion.js @@ -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, +}; diff --git a/src/index.js b/src/index.js index a989886..bc67f95 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { diff --git a/src/modules/version.js b/src/modules/version.js index b198297..42f6b4e 100644 --- a/src/modules/version.js +++ b/src/modules/version.js @@ -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(); diff --git a/src/utils.js b/src/utils.js index ee8e390..1cfa685 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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,