import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, writeFileSync, rmSync, } from 'node:fs'; import { fileURLToPath } from 'node:url'; const source = new URL('../dist/build/h5/', import.meta.url); const destination = new URL('../../server/public/water-mini/', import.meta.url); if (!existsSync(new URL('index.html', source))) throw new Error('Run npm run build:h5 before publishing the local preview.'); mkdirSync(fileURLToPath(destination), { recursive: true }); // Publish assets before switching HTML; keep previous hashed assets for already-open pages. const html = readFileSync(new URL('index.html', source), 'utf8'); for (const match of html.matchAll(/(?:src|href)="(\/water-mini\/[^"?#]+)"/g)) { const relative = match[1].slice('/water-mini/'.length); if (!existsSync(new URL(relative, source))) throw new Error('Missing build asset: ' + relative); } for (const entry of readdirSync(source)) { if (entry !== 'index.html') cpSync(new URL(entry, source), new URL(entry, destination), { recursive: true }); } const temporaryIndex = new URL('index.html.' + process.pid + '.tmp', destination); try { writeFileSync(temporaryIndex, html); renameSync(temporaryIndex, new URL('index.html', destination)); } finally { rmSync(temporaryIndex, { force: true }); } console.log('Local preview: http://127.0.0.1:8787/water-mini/index.html');