forked from Zarel/Pokemon-Showdown-Dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·46 lines (36 loc) · 1.4 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
36
37
38
39
40
41
42
43
44
45
46
#!/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');
const child_process = require('child_process');
process.chdir(__dirname);
if (process.argv[2] === 'full') {
child_process.execSync('../play.pokemonshowdown.com/build-tools/build-indexes', {stdio: 'inherit'});
}
function updateIndex() {
let indexContents = fs.readFileSync('index.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, function (a, b, c) {
let hash = Math.random(); // just in case creating the hash fails
try {
var filepath = c;
if (c.includes('/play.pokemonshowdown.com/')) {
const filename = c.replace('/play.pokemonshowdown.com/', '');
filepath = '../play.pokemonshowdown.com/' + filename;
}
const fstr = fs.readFileSync(filepath, {encoding: 'utf8'});
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
} catch (e) {}
return b + '="/' + c + '?' + hash + '"';
});
console.log("DONE");
process.stdout.write("Writing new `index.php` file... ");
fs.writeFileSync('index.php', indexContents);
console.log("DONE");
}
updateIndex();
child_process.execSync('node ./cms/build', {stdio: 'inherit'});