Files

41 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
// Copies the vpinball-wasm engine's build output into public/vendor/vpinball-wasm/
// so it's served as plain static files (see lib/pinball/usePinballInstance.ts for why:
// the package's own loadPinball() does a dynamic import() of its glue script that must
// never pass through webpack's module graph).
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { existsSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const rootDir = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const pkgDir = path.join(rootDir, "node_modules", "@valknar", "vpinball-wasm");
const pkgDistDir = path.join(pkgDir, "dist");
const targetDir = path.join(rootDir, "public", "vendor", "vpinball-wasm");
if (!existsSync(pkgDistDir)) {
console.error(
`vpinball-wasm not found at ${pkgDistDir} — run "pnpm install" first.`,
);
process.exit(1);
}
await rm(targetDir, { recursive: true, force: true });
await mkdir(targetDir, { recursive: true });
await cp(pkgDistDir, targetDir, { recursive: true });
console.log(`Copied vpinball-wasm engine assets to ${path.relative(rootDir, targetDir)}/`);
// Keep the service worker's engine cache key in lockstep with the installed
// engine version, so a version bump always busts stale caches for returning
// visitors — see sw.js's ENGINE_VERSION comment for why this must not be a
// manual step.
const { version: engineVersion } = JSON.parse(await readFile(path.join(pkgDir, "package.json"), "utf8"));
const swPath = path.join(rootDir, "public", "sw.js");
const swContents = await readFile(swPath, "utf8");
const updatedSw = swContents.replace(/ENGINE_VERSION = "[^"]*"/, `ENGINE_VERSION = "${engineVersion}"`);
if (updatedSw !== swContents) {
await writeFile(swPath, updatedSw);
console.log(`Set sw.js ENGINE_VERSION to ${engineVersion}`);
}