diff --git a/build-tools/news-data.php b/build-tools/news-embed.php similarity index 69% rename from build-tools/news-data.php rename to build-tools/news-embed.php index 16af212d4..a30352e44 100644 --- a/build-tools/news-data.php +++ b/build-tools/news-embed.php @@ -2,4 +2,4 @@ date_default_timezone_set('America/Los_Angeles'); include __DIR__.'/../pokemonshowdown.com/news/include.php'; -echo json_encode(array(getNewsId(), renderNews())); +echo json_encode([getNewsId(), renderNews()]); diff --git a/build-tools/update b/build-tools/update index 2cfedf191..b255490cf 100755 --- a/build-tools/update +++ b/build-tools/update @@ -137,82 +137,77 @@ process.stdout.write("Updating cachebuster and URLs... "); const URL_REGEX = /(src|href)="(.*?)(\?[a-z0-9]*?)?"/g; -function updateURL(a, b, c, d) { - c = c.replace('/replay.pokemonshowdown.com/', '/' + routes.replays + '/'); - c = c.replace('/dex.pokemonshowdown.com/', '/' + routes.dex + '/'); - c = c.replace('/play.pokemonshowdown.com/', '/' + routes.client + '/'); - c = c.replace('/pokemonshowdown.com/', '/' + routes.root + '/'); +function addCachebuster(_, attr, url, urlQuery) { + url = url.replace('/replay.pokemonshowdown.com/', '/' + routes.replays + '/'); + url = url.replace('/dex.pokemonshowdown.com/', '/' + routes.dex + '/'); + url = url.replace('/play.pokemonshowdown.com/', '/' + routes.client + '/'); + url = url.replace('/pokemonshowdown.com/', '/' + routes.root + '/'); - if (d) { - if (c.startsWith('/')) { + if (urlQuery) { + if (url.startsWith('/')) { let hash = Math.random(); // just in case creating the hash fails try { - const filename = c.slice(1).replace('/' + routes.client + '/', ''); + const filename = url.slice(1).replace('/' + routes.client + '/', ''); const fstr = fs.readFileSync(filename, {encoding: 'utf8'}); hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8); } catch (e) {} - return b + '="' + c + '?' + hash + '"'; + return attr + '="' + url + '?' + hash + '"'; } else { // hardcoded to Replays rn; TODO: generalize let hash; 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); } catch (e) {} - return b + '="' + c + '?' + (hash || 'v1') + '"'; + return attr + '="' + url + '?' + (hash || 'v1') + '"'; } } else { - return b + '="' + c + '"'; + return attr + '="' + url + '"'; } } -function writeFiles(indexContents, preactIndexContents, crossprotocolContents, replayEmbedContents) { - fs.writeFileSync('play.pokemonshowdown.com/index.html', indexContents); - 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); +// add hashes to js and css files and rewrite URLs +let indexContents = fs.readFileSync('play.pokemonshowdown.com/index.template.html', {encoding: 'utf8'}); +indexContents = indexContents.replace(URL_REGEX, addCachebuster); +let preactIndexContents = fs.readFileSync('play.pokemonshowdown.com/preactalpha.template.html', {encoding: 'utf8'}); +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'}); - replaysContents = replaysContents.replace(URL_REGEX, updateURL); - fs.writeFileSync('replay.pokemonshowdown.com/index.php', replaysContents); - - console.log("DONE"); +// add news, only if it's actually likely to exist +process.stdout.write("and news... "); +let stdout = ''; +let newsid = 0; +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() { - // add hashes to js and css files and rewrite URLs - 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); +indexContents = indexContents.replace(//g, newsid); +indexContents = indexContents.replace(//g, news); - // add news, only if it's actually likely to exist - process.stdout.write("and 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)); - } +try { + indexContents = indexContents.replace(//g, '' + fs.readFileSync('config/head-custom.html')); +} catch {} - indexContents = indexContents.replace(//g, newsData[0]); - indexContents = indexContents.replace(//g, newsData[1]); +fs.writeFileSync('play.pokemonshowdown.com/index.html', indexContents); +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(//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); - }); -} - -updateFiles(); +console.log("DONE");