pokemon-showdown-client/replays/build
2020-07-21 15:27:21 -07:00

48 lines
1.6 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.
*/
const fs = require('fs');
const crypto = require('crypto');
process.chdir(__dirname);
function updateIndex() {
let indexContents = fs.readFileSync('theme/wrapper.inc.template.php', {encoding: 'utf8'});
// add hashes to js and css files
process.stdout.write("Updating hashes... ");
indexContents = indexContents.replace(/(src|href)="\/(.*?)(\?[a-z0-9]*?)?"/g, runReplace);
console.log("DONE");
process.stdout.write("Writing new `wrapper.inc.php` file... ");
fs.writeFileSync('theme/wrapper.inc.php', indexContents);
console.log("DONE");
}
function runReplace(a, b, c) {
let hash = Math.random(); // just in case creating the hash fails
const routes = JSON.parse(fs.readFileSync('../config/routes.json'));
try {
var filepath = c;
if (c.includes('/' + routes.client + '/')) {
const filename = c.replace('/' + routes.client + '/', '');
filepath = '../' + filename;
}
const fstr = fs.readFileSync(filepath, {encoding: 'utf8'});
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
} catch (e) {}
c = c.replace('/replay.pokemonshowdown.com/', '/' + routes.replays + '/');
c = c.replace('/dex.pokemonshowdown.com/', '/' + routes.dex + '/');
c = c.replace('/play.pokemonshowdown.com/', '/' + routes.client + '/');
c = c.replace('/pokemonshowdown.com/users/', '/' + routes.users + '/');
c = c.replace('/pokemonshowdown.com/', '/' + routes.root + '/');
return b + '="/' + c + '?' + hash + '"';
}
updateIndex();