import { readFile, writeFile, cp, mkdir } from 'node:fs/promises'; import { resolve, join } from 'node:path'; // Run after npm run build. Use a new asset ID for every deployment to avoid // mixing cached JavaScript from different versions on the static server. const [version, assetId, destination] = process.argv.slice(2); if (!/^v\d+$/.test(version || '') || !/^[a-zA-Z0-9-]+$/.test(assetId || '') || !destination) { throw new Error('Usage: node scripts/package-release.mjs v2 '); } const output = resolve(destination); const prefix = `/games/hextrails/${assetId}/`; await mkdir(output, { recursive: true }); await cp('dist', join(output, assetId), { recursive: true, errorOnExist: true, force: false }); const html = (await readFile('dist/index.html', 'utf8')).replace(/(href|src)="(?!#|https?:|\/)([^"]+)"/g, (_, attr, path) => `${attr}="${prefix}${path}"`); await writeFile(join(output, `${version}.html`), html); for (const name of ['src/sprites.js', 'src/ui-skin.js']) { const path = join(output, assetId, name); await writeFile(path, (await readFile(path, 'utf8')).replaceAll("'assets/", `'${prefix}assets/`)); } console.log(`Release package: ${output}/${version}.html`);