diff --git a/app.js b/app.js index 918f7fa81..67f013a06 100644 --- a/app.js +++ b/app.js @@ -15,6 +15,7 @@ var WATCH_DELAY = 300; var indexStatic; var router; +var pageMeta = {}; var app = express(); var webpackCompiler = webpack(webpackConfig); @@ -42,7 +43,9 @@ app.use(function(req, res, next) { if (!router.match(req.url)) { return next('route'); } - indexStatic.generate(req.url, {}, function(err, html) { + indexStatic.generate(req.url, { + meta: pageMeta + }, function(err, html) { if (err) { return next(err); } @@ -61,6 +64,9 @@ if (!module.parent) { 'have recently been run with NODE_ENV="production". If this is not', 'the case, some or all static assets may be out of date.' ].join('\n')); + pageMeta['git-rev'] = fs.readFileSync(gulpfile.GIT_REV_FILE, 'utf-8'); + console.log('This server is based on git commit ' + + pageMeta['git-rev'] + '.'); indexStaticWatcher.build(function(err, newIndexStatic) { if (err) { throw err; @@ -73,6 +79,10 @@ if (!module.parent) { } else { require('./lib/developer-help')(); + try { + pageMeta['git-rev'] = gulpfile.getGitRev(); + } catch (e) {} + indexStaticWatcher.watch(WATCH_DELAY, function(newIndexStatic) { updateIndexStatic(newIndexStatic); console.log('Rebuilt server-side bundle.'); diff --git a/gulpfile.js b/gulpfile.js index 0691621e8..fa7341dea 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,5 +1,6 @@ var path = require('path'); var PassThrough = require('stream').PassThrough; +var execSync = require('child_process').execSync; var _ = require('underscore'); var gulp = require('gulp'); var gulpif = require('gulp-if'); @@ -47,6 +48,7 @@ var LINT_DIRS = [ ]; var LESS_FILES = './less/**/*.less'; +var GIT_REV_FILE = path.join(__dirname, 'dist', 'git-rev.txt'); function onError(err) { gutil.log(gutil.colors.red(err)); @@ -60,16 +62,19 @@ function handleError() { }); } +function getGitRev() { + return execSync('git rev-parse HEAD', { + cwd: __dirname, + encoding: 'utf8' + }).slice(0, 40); +} + function createIndexFileStream() { var stream = new PassThrough({ objectMode: true }); var meta = {}; - var execSync = require('child_process').execSync; try { - meta['git-rev'] = execSync('git rev-parse HEAD', { - cwd: __dirname, - encoding: 'utf8' - }).slice(0, 40); + meta['git-rev'] = getGitRev(); } catch (e) {} indexStaticWatcher.build(function(err, indexStatic) { @@ -307,9 +312,13 @@ gulp.task('s3', BUILD_TASKS, function() { }); if (process.env.NODE_ENV === 'production') { - gulp.task('postinstall', MINIMAL_BUILD_TASKS); + gulp.task('postinstall', MINIMAL_BUILD_TASKS, function() { + require('fs').writeFileSync(GIT_REV_FILE, getGitRev()); + }); } else { gulp.task('postinstall'); } module.exports.LESS_FILES = LESS_FILES; +module.exports.GIT_REV_FILE = GIT_REV_FILE; +module.exports.getGitRev = getGitRev;