Newer
Older
Lloyd Hilaiel
committed
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// determine the software 'version'. This is the 7 char abbrevieated SHA
// we try to read this from ver.txt at the top level, then try to use git,
// then finally fall back to a randomly generated 7 char string
// this version will be used for features like cache busting
const
fs = require('fs'),
path = require('path'),
logger = require('./logging.js').logger,
spawn = require('child_process').spawn,
secrets = require('./secrets.js'),
config = require('./configuration');
Lloyd Hilaiel
committed
var sha;
// first try ver.txt which by convention is placed in repo root at
// deployment time
if (config.get('env') === 'production') {
try {
var contents = fs.readFileSync(path.join(__dirname, '..', 'resources', 'static', 'ver.txt'));
sha = contents.toString().split(' ')[0];
if (sha.length != 7) throw "bad sha in ver.txt";
} catch(e) {
sha = undefined;
logger.debug('cannot read code version from ver.txt: ' + e);
}
Lloyd Hilaiel
committed
}
// now set the SHA to either the read SHA or a random string
module.exports = function() { return sha; };
Lloyd Hilaiel
committed
// if ver.txt discovery failed, try using git to get the sha.
if (!sha) {
Lloyd Hilaiel
committed
sha = secrets.weakGenerate(7);
Lloyd Hilaiel
committed
// next try using git
var p = spawn('git', [ 'log', '--pretty=%h', '-1' ]);
var buf = "";
p.stdout.on('data', function(d) {
buf += d;
});
Zachary Carter
committed
p.stdout.on('end', function(code, signal) {
Lloyd Hilaiel
committed
var gitsha = buf.toString().trim();
if (gitsha && gitsha.length === 7) {
Zachary Carter
committed
sha = gitsha;
Lloyd Hilaiel
committed
logger.info('code version (via git) is: ' + module.exports());
Lloyd Hilaiel
committed
} else {
Lloyd Hilaiel
committed
logger.warn('code version (randomly generated) is: ' + module.exports());
Lloyd Hilaiel
committed
}
});
} else {
Lloyd Hilaiel
committed
logger.info('code version (from ver.txt) is: ' + module.exports());