#!/usr/bin/env node import { access, readFile, readdir, writeFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const shippingRoot = path.join(projectRoot, "public", "assets", "game", "dungeons"); const packageFile = path.join( projectRoot, "src", "game", "generated", "manastormAssetPackages.json", ); const registryFile = path.join( projectRoot, "src", "game", "generated", "fivePlayerDungeonImports.json", ); const exists = async (file) => access(file).then(() => true, () => false); const readJson = async (file) => JSON.parse(await readFile(file, "utf8")); const writeJson = (file, value) => writeFile(file, `${JSON.stringify(value, null, 2)}\n`); const packageRegistry = await readJson(packageFile); const packagesByMap = new Map( packageRegistry.packages.map((assetPackage) => [Number(assetPackage.mapId), assetPackage]), ); const imports = []; let relinked = 0; for (const entry of await readdir(shippingRoot, { withFileTypes: true })) { if (!entry.isDirectory()) continue; const manifestFile = path.join(shippingRoot, entry.name, "dungeon-pack.json"); if (!await exists(manifestFile)) continue; const manifest = await readJson(manifestFile); const canonicalEnvironment = packagesByMap.get(Number(manifest.mapId)); if (!canonicalEnvironment) { throw new Error(`${manifest.slug}: optimized canonical map package is missing.`); } manifest.environment = canonicalEnvironment; await writeJson(manifestFile, manifest); imports.push(manifest); relinked += 1; } imports.sort((left, right) => left.slug.localeCompare(right.slug)); await writeJson(registryFile, { schemaVersion: 1, generatedAt: "deterministic", imports, }); console.log(JSON.stringify({ status: "green", relinked, registry: path.relative(projectRoot, registryFile).replaceAll("\\", "/"), }, null, 2));