aboutsummaryrefslogtreecommitdiff
path: root/src/modules/version.js
blob: 033fbf0a41b8039a1f8e5a4e749741869eae0951 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const path = require('path');
const fs = require('fs');
const {promisify} = require('util');
const utils = require("../utils");
const updates = require('../data/updates');
const config = require('../config');

const access = promisify(fs.access);
const readFile = promisify(fs.readFile);

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)})`;
    }

    if (config.updateNotifications) {
      const availableUpdate = await updates.getAvailableUpdate();
      if (availableUpdate) {
        response += ` (version ${availableUpdate} available)`;
      }
    }

    utils.postSystemMessageWithFallback(msg.channel, thread, response);
  });
};