import { mkdirSync, writeFileSync } from 'node:fs'; import { dirname, join, sep } from 'node:path'; import { createServer } from 'vite'; const outputPath = process.argv[2] ?? join('tmp', 'battle-map-v2-runtime-data.json'); const battleAssetMarker = '/src/assets/images/battle/'; const server = await createServer({ logLevel: 'error', server: { middlewareMode: true }, appType: 'custom' }); try { const { battleScenarios } = await server.ssrLoadModule('/src/game/data/battles.ts'); const { battleMapAssets } = await server.ssrLoadModule('/src/game/data/battleMapAssets.ts'); const battles = Object.values(battleScenarios).map((scenario, index) => { const assetUrl = battleMapAssets[scenario.mapTextureKey]; const markerIndex = typeof assetUrl === 'string' ? assetUrl.indexOf(battleAssetMarker) : -1; if (markerIndex < 0) { throw new Error(`${scenario.id}: cannot resolve battle map asset URL ${JSON.stringify(assetUrl)}`); } const fileName = assetUrl.slice(markerIndex + battleAssetMarker.length); return { number: index + 1, id: scenario.id, title: scenario.title, mapTextureKey: scenario.mapTextureKey, assetPath: join('src', 'assets', 'images', 'battle', fileName).split(sep).join('/'), width: scenario.map.width, height: scenario.map.height, terrain: scenario.map.terrain, units: scenario.units.map((unit) => ({ id: unit.id, faction: unit.faction, x: unit.x, y: unit.y })) }; }); mkdirSync(dirname(outputPath), { recursive: true }); writeFileSync(outputPath, `${JSON.stringify({ generatedAt: new Date().toISOString(), battles }, null, 2)}\n`, 'utf8'); console.log(`Wrote ${outputPath} with ${battles.length} runtime battle maps.`); } finally { await server.close(); }