feat: cache tankopedia and tankaverages during build

This commit is contained in:
TresAbhi committed 2023-06-26 18:08:07 +00:00
1 parent 79343438f0
commit 0f46f333b5
7 files changed
+59 -44

No files matched your search

+1
View File
@@ -21,6 +21,7 @@
"Smri", "Smri",
"startcase", "startcase",
"strvk", "strvk",
"tankaverages",
"tankopedia", "tankopedia",
"tankstats", "tankstats",
"Unicum", "Unicum",
+2 -2
View File
@@ -5,8 +5,8 @@
"main": "src/index.ts", "main": "src/index.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"build:bot": "yarn lint && node scripts/build --build=bot", "build:bot": "node scripts/build --build=bot",
"build:server": "yarn lint && node scripts/build --build=server", "build:server": "node scripts/build --build=server",
"build": "yarn lint && node scripts/build --build=all", "build": "yarn lint && node scripts/build --build=all",
"dev:bot": "yarn build:bot --dev && yarn start:bot --dev", "dev:bot": "yarn build:bot --dev && yarn start:bot --dev",
"dev:server": "yarn build:server --dev && yarn start:server --dev", "dev:server": "yarn build:server --dev && yarn start:server --dev",
+38 -4
View File
@@ -1,5 +1,5 @@
import { build } from 'esbuild'; import { build } from 'esbuild';
import { rmSync } from 'fs'; import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { argv } from 'process'; import { argv } from 'process';
const isProd = !argv.includes('--dev'); const isProd = !argv.includes('--dev');
@@ -7,8 +7,37 @@ const buildAll = argv.includes('--build=all');
const buildBot = argv.includes('--build=bot') | buildAll; const buildBot = argv.includes('--build=bot') | buildAll;
const buildServer = argv.includes('--build=server') | buildAll; const buildServer = argv.includes('--build=server') | buildAll;
console.log('Removing dist...'); if (isProd) {
rmSync('dist', { recursive: true, force: true }); // only remove in production to ensure no time is wasted in dev
console.log('Removing dist...');
rmSync('dist', { recursive: true, force: true });
console.log('Creating dist...');
mkdirSync('dist');
console.log('Caching tankopedia...');
fetch('https://www.blitzstars.com/bs-tankopedia.json')
.then((response) => response.text())
.then((tankopedia) => {
writeFileSync('./dist/tankopedia.json', tankopedia);
console.log('Tankopedia cached');
});
console.log('Caching tank averages...');
fetch('https://www.blitzstars.com/api/tankaverages.json')
.then((response) => response.json())
.then((individualTankAverages) => {
const tankAverages = {};
individualTankAverages.forEach((individualTankAverage) => {
tankAverages[individualTankAverage.tank_id] = individualTankAverage;
});
writeFileSync('./dist/tankaverages.json', JSON.stringify(tankAverages));
console.log('Tank averages cached');
});
}
const commonOptions = { const commonOptions = {
platform: 'node', platform: 'node',
@@ -26,21 +55,26 @@ const commonOptions = {
logLevel: isProd ? 'info' : 'silent', logLevel: isProd ? 'info' : 'silent',
}; };
console.log('Building...');
if (buildBot) { if (buildBot) {
console.log('Building bot...');
build({ build({
...commonOptions, ...commonOptions,
entryPoints: ['src/bot.ts'], entryPoints: ['src/bot.ts'],
outfile: 'dist/bot.cjs', outfile: 'dist/bot.cjs',
}).then(() => {
console.log('Bot built');
}); });
} }
if (buildServer) { if (buildServer) {
console.log('Building server...');
build({ build({
...commonOptions, ...commonOptions,
entryPoints: ['src/server.ts'], entryPoints: ['src/server.ts'],
outfile: 'dist/server.cjs', outfile: 'dist/server.cjs',
}).then(() => {
console.log('Server built');
}); });
} }
+11 -25
View File
@@ -1,4 +1,5 @@
import { WargamingResponse } from '../../types/wargamingResponse.js'; import { readFileSync } from 'fs';
import { join } from 'path';
export type TankType = 'AT-SPG' | 'lightTank' | 'mediumTank' | 'heavyTank'; export type TankType = 'AT-SPG' | 'lightTank' | 'mediumTank' | 'heavyTank';
@@ -18,30 +19,15 @@ export interface Tankopedia {
[id: number]: TankopediaEntry; [id: number]: TankopediaEntry;
} }
export const tankopedia: Tankopedia = {}; export const tankopedia = (
export const TANK_IDS: number[] = []; JSON.parse(
export const TANKS: TankopediaEntry[] = []; readFileSync(join(__dirname, 'tankopedia.json')) as unknown as string,
export const TANK_NAMES: string[] = []; ) as { data: Tankopedia }
).data;
console.log('Caching tankopedia...'); const entries = Object.entries(tankopedia);
fetch('https://www.blitzstars.com/bs-tankopedia.json') export const TANK_IDS = entries.map(([, { tank_id }]) => tank_id as number);
.then( export const TANKS = entries.map(([, entry]) => entry as TankopediaEntry);
(response) => export const TANK_NAMES = entries.map(([, { name }]) => name);
response.json() as Promise<
WargamingResponse<Tankopedia> & { status: 'ok' }
>,
)
.then((wargamingResponse) => {
Object.assign(tankopedia, wargamingResponse.data);
Object.entries(tankopedia).forEach(([idString, entry]) => {
const id = Number(idString);
TANK_IDS.push(id);
TANKS.push(entry);
TANK_NAMES.push(entry.name);
}),
console.log('Cached tankopedia');
});
export const TANK_ICONS: Record<TankType, string> = { export const TANK_ICONS: Record<TankType, string> = {
'AT-SPG': 'https://i.imgur.com/BIHSEH0.png', 'AT-SPG': 'https://i.imgur.com/BIHSEH0.png',
+6 -10
View File
@@ -1,3 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { AllStats } from '../../types/accountInfo.js'; import { AllStats } from '../../types/accountInfo.js';
export interface SpecialStats { export interface SpecialStats {
@@ -78,14 +80,8 @@ export interface IndividualTankAverage {
last_update: number; last_update: number;
} }
export const tankAverages: Record<number, IndividualTankAverage> = {}; export type TankAverages = Record<number, IndividualTankAverage>;
console.log('Caching tank averages...'); export const tankAverages = JSON.parse(
fetch('https://www.blitzstars.com/api/tankaverages.json') readFileSync(join(__dirname, 'tankaverages.json')) as unknown as string,
.then((response) => response.json() as Promise<IndividualTankAverage[]>) ) as TankAverages;
.then((individualTankAverages) => {
individualTankAverages.forEach((individualTankAverage) => {
tankAverages[individualTankAverage.tank_id] = individualTankAverage;
});
console.log('Tank averages cached');
});
-2
View File
@@ -3,5 +3,3 @@ const isDevBoolean = process.env.NODE_ENV === 'development';
export default function isDev() { export default function isDev() {
return isDevBoolean; return isDevBoolean;
} }
console.log(process.env.NODE_ENV);
+1 -1
View File
@@ -15,4 +15,4 @@ app.get('/', async (req, res) => {
res.end(svg); res.end(svg);
}); });
app.listen(process.env.PORT || 3000); app.listen(process.env.PORT ?? 3000);