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",
"startcase",
"strvk",
"tankaverages",
"tankopedia",
"tankstats",
"Unicum",
+2 -2
View File
@@ -5,8 +5,8 @@
"main": "src/index.ts",
"type": "module",
"scripts": {
"build:bot": "yarn lint && node scripts/build --build=bot",
"build:server": "yarn lint && node scripts/build --build=server",
"build:bot": "node scripts/build --build=bot",
"build:server": "node scripts/build --build=server",
"build": "yarn lint && node scripts/build --build=all",
"dev:bot": "yarn build:bot --dev && yarn start:bot --dev",
"dev:server": "yarn build:server --dev && yarn start:server --dev",
+38 -4
View File
@@ -1,5 +1,5 @@
import { build } from 'esbuild';
import { rmSync } from 'fs';
import { mkdirSync, rmSync, writeFileSync } from 'fs';
import { argv } from 'process';
const isProd = !argv.includes('--dev');
@@ -7,8 +7,37 @@ const buildAll = argv.includes('--build=all');
const buildBot = argv.includes('--build=bot') | buildAll;
const buildServer = argv.includes('--build=server') | buildAll;
console.log('Removing dist...');
rmSync('dist', { recursive: true, force: true });
if (isProd) {
// 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 = {
platform: 'node',
@@ -26,21 +55,26 @@ const commonOptions = {
logLevel: isProd ? 'info' : 'silent',
};
console.log('Building...');
if (buildBot) {
console.log('Building bot...');
build({
...commonOptions,
entryPoints: ['src/bot.ts'],
outfile: 'dist/bot.cjs',
}).then(() => {
console.log('Bot built');
});
}
if (buildServer) {
console.log('Building server...');
build({
...commonOptions,
entryPoints: ['src/server.ts'],
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';
@@ -18,30 +19,15 @@ export interface Tankopedia {
[id: number]: TankopediaEntry;
}
export const tankopedia: Tankopedia = {};
export const TANK_IDS: number[] = [];
export const TANKS: TankopediaEntry[] = [];
export const TANK_NAMES: string[] = [];
console.log('Caching tankopedia...');
fetch('https://www.blitzstars.com/bs-tankopedia.json')
.then(
(response) =>
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 tankopedia = (
JSON.parse(
readFileSync(join(__dirname, 'tankopedia.json')) as unknown as string,
) as { data: Tankopedia }
).data;
const entries = Object.entries(tankopedia);
export const TANK_IDS = entries.map(([, { tank_id }]) => tank_id as number);
export const TANKS = entries.map(([, entry]) => entry as TankopediaEntry);
export const TANK_NAMES = entries.map(([, { name }]) => name);
export const TANK_ICONS: Record<TankType, string> = {
'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';
export interface SpecialStats {
@@ -78,14 +80,8 @@ export interface IndividualTankAverage {
last_update: number;
}
export const tankAverages: Record<number, IndividualTankAverage> = {};
export type TankAverages = Record<number, IndividualTankAverage>;
console.log('Caching tank averages...');
fetch('https://www.blitzstars.com/api/tankaverages.json')
.then((response) => response.json() as Promise<IndividualTankAverage[]>)
.then((individualTankAverages) => {
individualTankAverages.forEach((individualTankAverage) => {
tankAverages[individualTankAverage.tank_id] = individualTankAverage;
});
console.log('Tank averages cached');
});
export const tankAverages = JSON.parse(
readFileSync(join(__dirname, 'tankaverages.json')) as unknown as string,
) as TankAverages;
-2
View File
@@ -3,5 +3,3 @@ const isDevBoolean = process.env.NODE_ENV === 'development';
export default function isDev() {
return isDevBoolean;
}
console.log(process.env.NODE_ENV);
+1 -1
View File
@@ -15,4 +15,4 @@ app.get('/', async (req, res) => {
res.end(svg);
});
app.listen(process.env.PORT || 3000);
app.listen(process.env.PORT ?? 3000);