forked from anastarawneh/Pokemon-Showdown-Dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·54 lines (43 loc) · 1.81 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
47
48
49
50
51
52
53
54
#!/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');
const Config = require('./config/config');
process.chdir(__dirname);
if (process.argv[2] === 'full') {
child_process.execSync('../play.pokemonshowdown.com/build-tools/build-indexes', {stdio: 'inherit'});
}
function updateFiles() {
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 + '"';
});
indexContents = indexContents.replace(/(src|href)="(.*?)\/(.*?)[a-z0-9]*?"/g, (match) => replaceRoutes(match));
console.log("DONE");
process.stdout.write("Writing new `index.php` file... ");
fs.writeFileSync('index.php', indexContents);
console.log("DONE");
}
function replaceRoutes(url) {
return url.replace('/replay.pokemonshowdown.com/', '/' + Config.routes.replays + '/')
.replace('/play.pokemonshowdown.com/', '/' + Config.routes.client + '/')
.replace('/pokemonshowdown.com/', '/' + Config.routes.root + '/');
}
updateFiles();
child_process.execSync('node ./cms/build', {stdio: 'inherit'});