pokemon-showdown-client/githooks/update
2014-09-01 02:11:36 +02:00

50 lines
1.7 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* This script parses index.html and sets the version query string of each
* resource to be the MD5 hash of that resource.
*
* On the live web server, this script is set as the following git hooks:
* post-commit, post-checkout, post-merge, post-rewrite
*/
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var exec = require('child_process').exec;
var thisDir = __dirname;
var rootDir = path.resolve(thisDir, '..');
var contents = fs.readFileSync(path.resolve(rootDir, 'index.template.html'), { encoding: 'utf8' });
// add hashes to js and css files
contents = contents.replace(/(src|href)="\/(.*?)\?[a-z0-9]*?"/g, function (a, b, c) {
var hash = Math.random(); // just in case creating the hash fails
try {
var filename = c.replace('/play.pokemonshowdown.com/', '');
var fstr = fs.readFileSync(path.resolve(rootDir, filename), { encoding: 'utf8' });
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
} catch (e) {}
return b + '="/' + c + '?' + hash + '"';
});
// add news
exec('php ' + path.resolve(thisDir, 'news-data.php'), function (error, stdout, stderr) {
var newsData = [0, '[failed to retrieve news]'];
if (!error && !stderr) {
try {
newsData = JSON.parse(stdout);
} catch (e) {
console.log("git hook failed to retrieve news (parsing JSON failed):\n" + e.stack);
}
} else {
console.log("git hook failed to retrieve news (exec command failed):\n" + (error || stderr));
}
contents = contents.replace(/<!-- newsid -->/g, newsData[0]);
contents = contents.replace(/<!-- news -->/g, newsData[1]);
fs.writeFileSync(path.resolve(rootDir, 'index.html'), contents);
});