check-ui-build.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env node
  2. /**
  3. * Assert that the browser viewer actually built.
  4. *
  5. * `codegraph ui` serves dist/viewer/ as static files. If that tree is missing
  6. * or half-written, the CLI still starts and the browser gets a 404 — a failure
  7. * that would otherwise surface after the release is published. So the build
  8. * fails here instead: index.html must exist, be non-trivial, and every local
  9. * asset it references must be on disk next to it.
  10. *
  11. * It also re-asserts that the compiled engine is still there. The viewer build
  12. * empties its own output directory, and `dist/ui/` — the obvious name — is
  13. * where tsc puts the TERMINAL ui, so a mis-pointed outDir silently deletes
  14. * modules the CLI requires at startup.
  15. *
  16. * Usage: node scripts/check-ui-build.mjs [--root <dir>]
  17. * --root directory holding dist/ (default: the repo root). The release
  18. * bundler points this at its staging dir to verify the copy.
  19. */
  20. import { existsSync, readFileSync, statSync } from 'node:fs';
  21. import { dirname, join, resolve, sep } from 'node:path';
  22. import { fileURLToPath } from 'node:url';
  23. const argv = process.argv.slice(2);
  24. const rootFlag = argv.indexOf('--root');
  25. const staged = rootFlag >= 0 && Boolean(argv[rootFlag + 1]);
  26. const root = staged
  27. ? resolve(argv[rootFlag + 1])
  28. : resolve(dirname(fileURLToPath(import.meta.url)), '..');
  29. const viewerDir = join(root, 'dist', 'viewer');
  30. const indexHtml = join(viewerDir, 'index.html');
  31. function fail(message, hint) {
  32. console.error(`[check-ui-build] ${message}`);
  33. if (hint) console.error(`[check-ui-build] ${hint}`);
  34. process.exit(1);
  35. }
  36. if (!existsSync(indexHtml)) {
  37. fail(
  38. `missing ${indexHtml}`,
  39. staged
  40. ? 'this bundle predates the UI or was assembled from a stale archive — rebuild it with scripts/build-bundle.sh'
  41. : 'the UI workspace did not build — run `npm run build:ui` (or `npm ci` if ui/ has no node_modules)'
  42. );
  43. }
  44. const html = readFileSync(indexHtml, 'utf8');
  45. if (html.length < 200 || !/<div id="app">/.test(html)) {
  46. fail(`${indexHtml} does not look like the built viewer (${html.length} bytes)`);
  47. }
  48. // Every local src=/href= in the document must resolve inside dist/ui. This is
  49. // what catches a partial write: index.html naming a hashed bundle that the
  50. // build never emitted.
  51. const referenced = [...html.matchAll(/\s(?:src|href)="([^"]+)"/g)].map((m) => m[1]);
  52. const local = referenced.filter(
  53. (url) => !/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(url) && !url.startsWith('#')
  54. );
  55. const missing = [];
  56. let assets = 0;
  57. for (const url of local) {
  58. const rel = url.replace(/^\.\//, '').replace(/[?#].*$/, '');
  59. if (!rel) continue;
  60. const onDisk = join(viewerDir, ...rel.split('/'));
  61. if (!existsSync(onDisk) || !statSync(onDisk).isFile()) missing.push(rel);
  62. else assets += 1;
  63. }
  64. if (missing.length > 0) {
  65. fail(
  66. `index.html references ${missing.length} file(s) that are not in dist/viewer: ${missing.join(', ')}`,
  67. 'the UI build was interrupted or dist/viewer was copied incompletely'
  68. );
  69. }
  70. if (assets === 0) {
  71. fail('index.html references no bundled assets — the UI build produced no JS/CSS');
  72. }
  73. // The viewer build must never have eaten the tsc output next door.
  74. for (const compiled of [join('bin', 'codegraph.js'), 'index.js', join('ui', 'shimmer-progress.js')]) {
  75. if (!existsSync(join(root, 'dist', compiled))) {
  76. fail(
  77. `dist/${compiled.split(sep).join('/')} is missing — the compiled engine is incomplete`,
  78. "if this appeared with a UI change, check ui/vite.config.ts: build.outDir must stay dist/viewer, and emptyOutDir must never point at a directory tsc writes (dist/ui is the TERMINAL ui)"
  79. );
  80. }
  81. }
  82. console.log(`[check-ui-build] dist/viewer ok (index.html + ${assets} referenced asset(s)); dist/ engine intact`);