forked from smogon/pokemon-showdown-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate
executable file
·103 lines (82 loc) · 3.38 KB
/
update
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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.
* It also updates news and the learnsets-g6.js file.
*
* On the live web server, this script is set as the following git hooks:
* post-commit, post-checkout, post-merge, post-rewrite
*/
"use strict";
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const child_process = require('child_process');
const thisDir = __dirname;
const rootDir = path.resolve(thisDir, '..');
process.chdir(rootDir);
/*********************************************************
* Update version number
*********************************************************/
process.stdout.write("Updating version... ");
let version = require('../package.json').version;
let configBuf = fs.readFileSync('config/config.js', {encoding: 'utf8'});
configBuf = configBuf.replace(/\/\* version \*\/[^;\n]*;/, `/* version */ Config.version = ${JSON.stringify(version)};`);
fs.writeFileSync('config/config.js', configBuf);
console.log("DONE");
/*********************************************************
* Compile TS files
*********************************************************/
child_process.execSync(`node node_modules/@babel/cli/bin/babel.js src --out-dir js --extensions ".ts,.tsx"`);
fs.writeFileSync(
'js/battledata.js',
fs.readFileSync('js/battle-dex.js') + '\n\n' +
fs.readFileSync('js/battle-dex-data.js') + '\n\n' +
fs.readFileSync('src/battle-dex-misc.js')
);
fs.writeFileSync(
'data/graphics.js',
fs.readFileSync('js/battle-animations.js') + '\n\n' +
fs.readFileSync('js/battle-animations-moves.js')
);
/*********************************************************
* Update cachebuster and News
*********************************************************/
function updateIndex() {
let indexContents = fs.readFileSync('index.template.html', {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/', '');
const fstr = fs.readFileSync(filename, {encoding: 'utf8'});
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
} catch (e) {}
return b + '="/' + c + '?' + hash + '"';
});
console.log("DONE");
// add news, only if it's actually likely to exist
if (__dirname.endsWith('play.pokemonshowdown.com/build-tools')) {
process.stdout.write("Updating news... ");
child_process.exec('php ' + path.resolve(thisDir, 'news-data.php'), function (error, stdout, stderr) {
let 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 + stdout));
}
indexContents = indexContents.replace(/<!-- newsid -->/g, newsData[0]);
indexContents = indexContents.replace(/<!-- news -->/g, newsData[1]);
console.log("DONE");
process.stdout.write("Writing new `index.html` file... ");
fs.writeFileSync('index.html', indexContents);
console.log("DONE");
});
}
}
updateIndex();