-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·35 lines (29 loc) · 1.23 KB
/
build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const exec = require('child_process').exec;
const thisFile = __filename;
const thisDir = __dirname;
const rootDir = '.';
// update version
function makeCachebuster(template, output) {
let indexContents = fs.readFileSync(path.resolve(rootDir, template), {encoding: 'utf8'});
// add hashes to js and css files
process.stdout.write("Updating hashes... ");
indexContents = indexContents.replace(/(src|href)="\.\/(.*?)\?[a-z0-9]*?"/g, function (a, b, c) {
let hash = Math.random(); // just in case creating the hash fails
try {
const filename = c.replace('//play.pokemonshowdown.com/', '../../play.pokemonshowdown.com/');
const 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 + '"';
});
process.stdout.write("Writing new `" + output + "` file... ");
fs.writeFileSync(path.resolve(rootDir, output), indexContents);
console.log("DONE");
}
makeCachebuster('honkalculate.template.html', 'honkalculate.html');
makeCachebuster('index.template.html', 'index.html');