Skip to content

Commit

Permalink
include git-rev meta tag in dynamically-generated webpages.
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed May 21, 2015
1 parent 1920bd7 commit d735664
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 11 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var WATCH_DELAY = 300;

var indexStatic;
var router;
var pageMeta = {};
var app = express();
var webpackCompiler = webpack(webpackConfig);

Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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.');
Expand Down
21 changes: 15 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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));
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

0 comments on commit d735664

Please sign in to comment.