|
| 1 | +// Post-build step: package the built site (dist/) into dist/api-validator.zip so |
| 2 | +// the "Run Locally" button can hand a user the whole app as a static bundle they |
| 3 | +// serve themselves. Runs after `vite build`; uses the system `zip` (present on |
| 4 | +// macOS and the ubuntu CI runner). |
| 5 | +import { execSync } from 'node:child_process'; |
| 6 | +import { existsSync, writeFileSync } from 'node:fs'; |
| 7 | + |
| 8 | +const OUT = 'api-validator.zip'; |
| 9 | + |
| 10 | +if (!existsSync('dist')) { |
| 11 | + console.error('zip-dist: dist/ not found — run `vite build` first.'); |
| 12 | + process.exit(1); |
| 13 | +} |
| 14 | + |
| 15 | +// A short how-to that travels inside the archive. |
| 16 | +writeFileSync( |
| 17 | + 'dist/RUN-LOCALLY.txt', |
| 18 | + [ |
| 19 | + 'API Validator — run locally', |
| 20 | + '', |
| 21 | + 'This is the fully built, static app. It runs entirely in your browser; there', |
| 22 | + 'is no backend and no keys are required.', |
| 23 | + '', |
| 24 | + '1. Unzip this archive.', |
| 25 | + '2. From the unzipped folder, start any static web server at the folder ROOT:', |
| 26 | + '', |
| 27 | + ' python3 -m http.server 8000', |
| 28 | + ' or', |
| 29 | + ' npx serve .', |
| 30 | + '', |
| 31 | + '3. Open http://localhost:8000/ in your browser.', |
| 32 | + '', |
| 33 | + '(Opening index.html directly via file:// will not work — ES modules need to be', |
| 34 | + 'served over http, so use a local server as above.)', |
| 35 | + '', |
| 36 | + 'Source: https://github.com/api-commons/api-validator', |
| 37 | + '', |
| 38 | + ].join('\n'), |
| 39 | +); |
| 40 | + |
| 41 | +// Build the archive from inside dist/ (so paths are relative), excluding any |
| 42 | +// pre-existing zip. -X drops extra macOS metadata for a clean cross-platform zip. |
| 43 | +// Never fail the build over the optional archive — the site still deploys. |
| 44 | +try { |
| 45 | + execSync(`cd dist && rm -f ${OUT} && zip -r -q -X ${OUT} . -x '*.zip'`, { stdio: 'inherit' }); |
| 46 | + console.log(`zip-dist: wrote dist/${OUT}`); |
| 47 | +} catch (e) { |
| 48 | + console.warn(`zip-dist: could not create ${OUT} (is \`zip\` installed?) — skipping. ${e?.message || e}`); |
| 49 | +} |
0 commit comments