|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Lint registry blocks/components the way a user actually receives them. |
| 4 | + * |
| 5 | + * `hyperframes lint <dir>` needs an `index.html`, but registry items ship as |
| 6 | + * `<name>.html`, so pointing the linter at an item directory fails with "No |
| 7 | + * composition found" — which means registry items were never linted at all. |
| 8 | + * Two `gsap_non_transform_motion` errors reached main that way. |
| 9 | + * |
| 10 | + * This mounts each item into a throwaway project (exactly where `hyperframes |
| 11 | + * add` would put it) and lints that, reporting only findings for the item's |
| 12 | + * own file so the host scaffold's noise is ignored. |
| 13 | + * |
| 14 | + * Usage: |
| 15 | + * node scripts/lint-registry-items.mjs # every item |
| 16 | + * node scripts/lint-registry-items.mjs mk-background … # named items |
| 17 | + */ |
| 18 | +import { |
| 19 | + readdirSync, |
| 20 | + existsSync, |
| 21 | + mkdtempSync, |
| 22 | + mkdirSync, |
| 23 | + copyFileSync, |
| 24 | + writeFileSync, |
| 25 | + rmSync, |
| 26 | +} from "node:fs"; |
| 27 | +import { join, resolve, dirname } from "node:path"; |
| 28 | +import { tmpdir } from "node:os"; |
| 29 | +import { fileURLToPath } from "node:url"; |
| 30 | +import { spawnSync } from "node:child_process"; |
| 31 | + |
| 32 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); |
| 33 | +const cli = join(repoRoot, "packages/cli/src/cli.ts"); |
| 34 | + |
| 35 | +// A deliberately boring host: one clip, one registered timeline, so the only |
| 36 | +// findings that can appear are the item's own. |
| 37 | +const HOST = `<!doctype html> |
| 38 | +<html lang="en"> |
| 39 | + <head> |
| 40 | + <meta charset="UTF-8" /> |
| 41 | + <meta name="viewport" content="width=1920, height=1080" /> |
| 42 | + <script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script> |
| 43 | + </head> |
| 44 | + <body> |
| 45 | + <div id="root" data-composition-id="main" data-start="0" data-width="1920" data-height="1080" data-duration="5"> |
| 46 | + <section id="host-slot" class="clip" data-start="0" data-duration="5" data-track-index="1"></section> |
| 47 | + </div> |
| 48 | + <script> |
| 49 | + window.__timelines = window.__timelines || {}; |
| 50 | + const tl = gsap.timeline({ paused: true }); |
| 51 | + tl.to("#host-slot", { opacity: 1, duration: 0.1 }, 0); |
| 52 | + window.__timelines["main"] = tl; |
| 53 | + </script> |
| 54 | + </body> |
| 55 | +</html> |
| 56 | +`; |
| 57 | + |
| 58 | +/** |
| 59 | + * Rules that assume a standalone composition. Many components ship as |
| 60 | + * paste-able fragments with no root element at all, so these fire on ~47 |
| 61 | + * long-standing items and would drown the signal. The host provides the root; |
| 62 | + * the item is not supposed to. |
| 63 | + */ |
| 64 | +const IGNORED = [ |
| 65 | + "root_missing_composition_id", |
| 66 | + "root_missing_dimensions", |
| 67 | + "multiple_root_compositions", |
| 68 | +]; |
| 69 | + |
| 70 | +function discover() { |
| 71 | + const out = []; |
| 72 | + for (const kind of ["blocks", "components"]) { |
| 73 | + const dir = join(repoRoot, "registry", kind); |
| 74 | + if (!existsSync(dir)) continue; |
| 75 | + for (const name of readdirSync(dir)) { |
| 76 | + const html = join(dir, name, `${name}.html`); |
| 77 | + if (existsSync(html)) out.push({ name, kind, html }); |
| 78 | + } |
| 79 | + } |
| 80 | + return out; |
| 81 | +} |
| 82 | + |
| 83 | +const only = process.argv.slice(2); |
| 84 | +const items = discover().filter((i) => only.length === 0 || only.includes(i.name)); |
| 85 | +if (items.length === 0) { |
| 86 | + console.error( |
| 87 | + only.length ? `No registry item matches: ${only.join(", ")}` : "No registry items found.", |
| 88 | + ); |
| 89 | + process.exit(1); |
| 90 | +} |
| 91 | + |
| 92 | +let failed = 0; |
| 93 | +for (const item of items) { |
| 94 | + const proj = mkdtempSync(join(tmpdir(), `hf-lint-${item.name}-`)); |
| 95 | + try { |
| 96 | + mkdirSync(join(proj, "compositions"), { recursive: true }); |
| 97 | + writeFileSync(join(proj, "index.html"), HOST); |
| 98 | + copyFileSync(item.html, join(proj, "compositions", `${item.name}.html`)); |
| 99 | + |
| 100 | + const res = spawnSync("bun", [cli, "lint", proj], { encoding: "utf-8" }); |
| 101 | + const lines = `${res.stdout ?? ""}${res.stderr ?? ""}`.split("\n"); |
| 102 | + // Only the item's own file — the host scaffold is not under review. |
| 103 | + const hits = lines.filter( |
| 104 | + (l) => |
| 105 | + l.includes("✗") && l.includes(`${item.name}.html`) && !IGNORED.some((r) => l.includes(r)), |
| 106 | + ); |
| 107 | + if (hits.length) { |
| 108 | + failed++; |
| 109 | + console.error(`\n✗ ${item.kind}/${item.name}`); |
| 110 | + for (const h of hits) console.error(` ${h.trim()}`); |
| 111 | + } |
| 112 | + } finally { |
| 113 | + rmSync(proj, { recursive: true, force: true }); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +if (failed) { |
| 118 | + console.error(`\n${failed} registry item(s) have lint errors.`); |
| 119 | + process.exit(1); |
| 120 | +} |
| 121 | +console.log(`Registry item lint passed — ${items.length} item(s), 0 errors.`); |
0 commit comments