mirror of
https://github.com/smogon/pokemon-showdown-client.git
synced 2026-03-21 17:50:29 -05:00
Refactor build tools for readability
(Also fixes the build error.)
This commit is contained in:
parent
d66b080873
commit
7732ec78f3
|
|
@ -2,4 +2,4 @@
|
||||||
date_default_timezone_set('America/Los_Angeles');
|
date_default_timezone_set('America/Los_Angeles');
|
||||||
include __DIR__.'/../pokemonshowdown.com/news/include.php';
|
include __DIR__.'/../pokemonshowdown.com/news/include.php';
|
||||||
|
|
||||||
echo json_encode(array(getNewsId(), renderNews()));
|
echo json_encode([getNewsId(), renderNews()]);
|
||||||
|
|
@ -137,82 +137,77 @@ process.stdout.write("Updating cachebuster and URLs... ");
|
||||||
|
|
||||||
const URL_REGEX = /(src|href)="(.*?)(\?[a-z0-9]*?)?"/g;
|
const URL_REGEX = /(src|href)="(.*?)(\?[a-z0-9]*?)?"/g;
|
||||||
|
|
||||||
function updateURL(a, b, c, d) {
|
function addCachebuster(_, attr, url, urlQuery) {
|
||||||
c = c.replace('/replay.pokemonshowdown.com/', '/' + routes.replays + '/');
|
url = url.replace('/replay.pokemonshowdown.com/', '/' + routes.replays + '/');
|
||||||
c = c.replace('/dex.pokemonshowdown.com/', '/' + routes.dex + '/');
|
url = url.replace('/dex.pokemonshowdown.com/', '/' + routes.dex + '/');
|
||||||
c = c.replace('/play.pokemonshowdown.com/', '/' + routes.client + '/');
|
url = url.replace('/play.pokemonshowdown.com/', '/' + routes.client + '/');
|
||||||
c = c.replace('/pokemonshowdown.com/', '/' + routes.root + '/');
|
url = url.replace('/pokemonshowdown.com/', '/' + routes.root + '/');
|
||||||
|
|
||||||
if (d) {
|
if (urlQuery) {
|
||||||
if (c.startsWith('/')) {
|
if (url.startsWith('/')) {
|
||||||
let hash = Math.random(); // just in case creating the hash fails
|
let hash = Math.random(); // just in case creating the hash fails
|
||||||
try {
|
try {
|
||||||
const filename = c.slice(1).replace('/' + routes.client + '/', '');
|
const filename = url.slice(1).replace('/' + routes.client + '/', '');
|
||||||
const fstr = fs.readFileSync(filename, {encoding: 'utf8'});
|
const fstr = fs.readFileSync(filename, {encoding: 'utf8'});
|
||||||
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
|
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
return b + '="' + c + '?' + hash + '"';
|
return attr + '="' + url + '?' + hash + '"';
|
||||||
} else {
|
} else {
|
||||||
// hardcoded to Replays rn; TODO: generalize
|
// hardcoded to Replays rn; TODO: generalize
|
||||||
let hash;
|
let hash;
|
||||||
try {
|
try {
|
||||||
const fstr = fs.readFileSync('replay.pokemonshowdown.com/' + c, {encoding: 'utf8'});
|
const fstr = fs.readFileSync('replay.pokemonshowdown.com/' + url, {encoding: 'utf8'});
|
||||||
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
|
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
return b + '="' + c + '?' + (hash || 'v1') + '"';
|
return attr + '="' + url + '?' + (hash || 'v1') + '"';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return b + '="' + c + '"';
|
return attr + '="' + url + '"';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeFiles(indexContents, preactIndexContents, crossprotocolContents, replayEmbedContents) {
|
// add hashes to js and css files and rewrite URLs
|
||||||
fs.writeFileSync('play.pokemonshowdown.com/index.html', indexContents);
|
let indexContents = fs.readFileSync('play.pokemonshowdown.com/index.template.html', {encoding: 'utf8'});
|
||||||
fs.writeFileSync('play.pokemonshowdown.com/preactalpha.html', preactIndexContents);
|
indexContents = indexContents.replace(URL_REGEX, addCachebuster);
|
||||||
fs.writeFileSync('play.pokemonshowdown.com/crossprotocol.html', crossprotocolContents);
|
let preactIndexContents = fs.readFileSync('play.pokemonshowdown.com/preactalpha.template.html', {encoding: 'utf8'});
|
||||||
fs.writeFileSync('play.pokemonshowdown.com/js/replay-embed.js', replayEmbedContents);
|
preactIndexContents = preactIndexContents.replace(URL_REGEX, addCachebuster);
|
||||||
|
let crossprotocolContents = fs.readFileSync('play.pokemonshowdown.com/crossprotocol.template.html', {encoding: 'utf8'});
|
||||||
|
crossprotocolContents = crossprotocolContents.replace(URL_REGEX, addCachebuster);
|
||||||
|
let replayEmbedContents = fs.readFileSync('play.pokemonshowdown.com/js/replay-embed.template.js', {encoding: 'utf8'});
|
||||||
|
replayEmbedContents = replayEmbedContents.replace(/play\.pokemonshowdown\.com/g, routes.client);
|
||||||
|
|
||||||
let replaysContents = fs.readFileSync('replay.pokemonshowdown.com/index.template.php', {encoding: 'utf8'});
|
// add news, only if it's actually likely to exist
|
||||||
replaysContents = replaysContents.replace(URL_REGEX, updateURL);
|
process.stdout.write("and news... ");
|
||||||
fs.writeFileSync('replay.pokemonshowdown.com/index.php', replaysContents);
|
let stdout = '';
|
||||||
|
let newsid = 0;
|
||||||
console.log("DONE");
|
let news = '[failed to retrieve news]';
|
||||||
|
try {
|
||||||
|
stdout = child_process.execSync('php ' + path.resolve(thisDir, 'news-embed.php'));
|
||||||
|
} catch (e) {
|
||||||
|
console.log("git hook failed to retrieve news (exec command failed):\n" + (e.error + e.stderr + e.stdout));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (stdout) [newsid, news] = JSON.parse(stdout);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("git hook failed to retrieve news (parsing JSON failed):\n" + e.stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateFiles() {
|
indexContents = indexContents.replace(/<!-- newsid -->/g, newsid);
|
||||||
// add hashes to js and css files and rewrite URLs
|
indexContents = indexContents.replace(/<!-- news -->/g, news);
|
||||||
let indexContents = fs.readFileSync('play.pokemonshowdown.com/index.template.html', {encoding: 'utf8'});
|
|
||||||
indexContents = indexContents.replace(URL_REGEX, updateURL);
|
|
||||||
let preactIndexContents = fs.readFileSync('play.pokemonshowdown.com/preactalpha.template.html', {encoding: 'utf8'});
|
|
||||||
preactIndexContents = preactIndexContents.replace(URL_REGEX, updateURL);
|
|
||||||
let crossprotocolContents = fs.readFileSync('play.pokemonshowdown.com/crossprotocol.template.html', {encoding: 'utf8'});
|
|
||||||
crossprotocolContents = crossprotocolContents.replace(URL_REGEX, updateURL);
|
|
||||||
let replayEmbedContents = fs.readFileSync('play.pokemonshowdown.com/js/replay-embed.template.js', {encoding: 'utf8'});
|
|
||||||
replayEmbedContents = replayEmbedContents.replace(/play\.pokemonshowdown\.com/g, routes.client);
|
|
||||||
|
|
||||||
// add news, only if it's actually likely to exist
|
try {
|
||||||
process.stdout.write("and news... ");
|
indexContents = indexContents.replace(/<!-- head custom -->/g, '' + fs.readFileSync('config/head-custom.html'));
|
||||||
child_process.exec('php ' + path.resolve(thisDir, 'news-data.php'), function (error, stdout, stderr) {
|
} catch {}
|
||||||
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]);
|
fs.writeFileSync('play.pokemonshowdown.com/index.html', indexContents);
|
||||||
indexContents = indexContents.replace(/<!-- news -->/g, newsData[1]);
|
fs.writeFileSync('play.pokemonshowdown.com/preactalpha.html', preactIndexContents);
|
||||||
|
fs.writeFileSync('play.pokemonshowdown.com/crossprotocol.html', crossprotocolContents);
|
||||||
|
fs.writeFileSync('play.pokemonshowdown.com/js/replay-embed.js', replayEmbedContents);
|
||||||
|
|
||||||
indexContents = indexContents.replace(/<!-- head custom -->/g, '' + fs.readFileSync('config/head-custom.html'));
|
let replaysContents = fs.readFileSync('replay.pokemonshowdown.com/index.template.php', {encoding: 'utf8'});
|
||||||
|
replaysContents = replaysContents.replace(URL_REGEX, addCachebuster);
|
||||||
|
fs.writeFileSync('replay.pokemonshowdown.com/index.php', replaysContents);
|
||||||
|
|
||||||
writeFiles(indexContents, preactIndexContents, crossprotocolContents, replayEmbedContents);
|
console.log("DONE");
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
updateFiles();
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user