From 99218341ec2f7be28971d9ecca464ddf9d03fd72 Mon Sep 17 00:00:00 2001
From: William Boman <william@redwill.se>
Date: Mon, 5 Sep 2016 14:37:27 +0200
Subject: [PATCH] consolidate version numbers throughout all interfaces

---
 src/client.js             |  2 +-
 src/command-line/index.js |  3 +--
 src/helper.js             | 26 ++++++++++++++++++++++++++
 src/server.js             | 19 +++++--------------
 4 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/src/client.js b/src/client.js
index e18ed33a..02d15f16 100644
--- a/src/client.js
+++ b/src/client.js
@@ -257,7 +257,7 @@ Client.prototype.connect = function(args) {
 	});
 
 	network.irc.connect({
-		version: pkg.name + " " + pkg.version + " -- " + pkg.homepage,
+		version: pkg.name + " " + Helper.getVersion() + " -- " + pkg.homepage,
 		host: network.host,
 		port: network.port,
 		nick: nick,
diff --git a/src/command-line/index.js b/src/command-line/index.js
index 5b6ea728..6df07f09 100644
--- a/src/command-line/index.js
+++ b/src/command-line/index.js
@@ -3,13 +3,12 @@
 global.log = require("../log.js");
 
 var program = require("commander");
-var pkg = require("../../package.json");
 var fs = require("fs");
 var fsextra = require("fs-extra");
 var path = require("path");
 var Helper = require("../helper");
 
-program.version(pkg.version, "-v, --version");
+program.version(Helper.getVersion(), "-v, --version");
 program.option("");
 program.option("    --home <path>" , "home path");
 
diff --git a/src/helper.js b/src/helper.js
index 21e99fcb..3db79ca1 100644
--- a/src/helper.js
+++ b/src/helper.js
@@ -1,5 +1,6 @@
 "use strict";
 
+const pkg = require("../package.json");
 var _ = require("lodash");
 var path = require("path");
 var os = require("os");
@@ -11,6 +12,8 @@ var Helper = {
 	getUserConfigPath: getUserConfigPath,
 	getUserLogsPath: getUserLogsPath,
 	setHome: setHome,
+	getVersion: getVersion,
+	getGitCommit: getGitCommit,
 };
 
 module.exports = Helper;
@@ -22,6 +25,29 @@ Helper.config = require(path.resolve(path.join(
 	"config.js"
 )));
 
+function getVersion() {
+	const gitCommit = getGitCommit();
+	return gitCommit ? `source (${gitCommit})` : `v${pkg.version}`;
+}
+
+let _gitCommit;
+function getGitCommit() {
+	if (_gitCommit !== undefined) {
+		return _gitCommit;
+	}
+	try {
+		_gitCommit = require("child_process")
+			.execSync("git rev-parse --short HEAD 2> /dev/null") // Returns hash of current commit
+			.toString()
+			.trim();
+		return _gitCommit;
+	} catch (e) {
+		// Not a git repository or git is not installed
+		_gitCommit = null;
+		return null;
+	}
+}
+
 function setHome(homePath) {
 	this.HOME = expandHome(homePath || "~/.lounge");
 	this.CONFIG_PATH = path.join(this.HOME, "config.js");
diff --git a/src/server.js b/src/server.js
index c95102b2..2ce7be08 100644
--- a/src/server.js
+++ b/src/server.js
@@ -81,8 +81,10 @@ module.exports = function() {
 
 	manager.sockets = sockets;
 
-	var protocol = config.https.enable ? "https" : "http";
-	log.info("The Lounge v" + pkg.version + " is now running on", protocol + "://" + (config.host || "*") + ":" + config.port + "/", (config.public ? "in public mode" : "in private mode"));
+	let protocol = config.https.enable ? "https" : "http";
+	let host = config.host || "*";
+	log.info("The Lounge", Helper.getVersion(), "is now running");
+	log.info(`Available on: ${protocol}://${host}:${config.port}/ in ${config.public ? "public" : "private"} mode`);
 	log.info("Press ctrl-c to stop\n");
 
 	if (!config.public) {
@@ -110,17 +112,6 @@ function allRequests(req, res, next) {
 	return next();
 }
 
-// Information to populate the About section in UI, either from npm or from git
-var gitCommit = null;
-try {
-	gitCommit = require("child_process")
-		.execSync("git rev-parse --short HEAD 2> /dev/null") // Returns hash of current commit
-		.toString()
-		.trim();
-} catch (e) {
-	// Not a git repository or git is not installed: treat it as npm release
-}
-
 function index(req, res, next) {
 	if (req.url.split("?")[0] !== "/") {
 		return next();
@@ -135,7 +126,7 @@ function index(req, res, next) {
 			pkg,
 			Helper.config
 		);
-		data.gitCommit = gitCommit;
+		data.gitCommit = Helper.getGitCommit();
 		data.themes = fs.readdirSync("client/themes/").filter(function(themeFile) {
 			return themeFile.endsWith(".css");
 		}).map(function(css) {