|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execSync } from "node:child_process"; |
| 3 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 8 | +const ALLOWLIST = join(ROOT, "scripts", "radix-allowlist.json"); |
| 9 | +const SCAN_ROOTS = ["packages", "apps"]; |
| 10 | + |
| 11 | +const ALLOWED_THEMES_IMPORTS = new Set(["Box", "Flex", "Text"]); |
| 12 | + |
| 13 | +const USAGE = `check-radix-imports — freeze Radix UI usage; new UI comes from @posthog/quill. |
| 14 | +
|
| 15 | + node scripts/check-radix-imports.mjs verify: fail on any Radix import not in the allowlist |
| 16 | + node scripts/check-radix-imports.mjs --init (re)generate the baseline allowlist from current imports |
| 17 | + node scripts/check-radix-imports.mjs --prune drop allowlist entries no longer imported (after migrating) |
| 18 | +
|
| 19 | +Only Box, Flex, and Text from @radix-ui/themes are permitted in new code. Every |
| 20 | +other Radix component and every other @radix-ui/* package is frozen: existing |
| 21 | +imports are baselined in scripts/radix-allowlist.json and must not grow. The |
| 22 | +allowlist size is the migration debt. Goal: 0.`; |
| 23 | + |
| 24 | +// Matches static imports, re-exports, and dynamic imports of Radix packages. |
| 25 | +const IMPORT_RE = |
| 26 | + /(?:import|export)\s+(?:type\s+)?([\w$]+|\*\s+as\s+[\w$]+|\{[^}]*\}|[\w$]+\s*,\s*\{[^}]*\}|\*)?\s*(?:from\s*)?["'](@radix-ui\/[^"']+|radix-ui(?:\/[^"']*)?)["']/g; |
| 27 | +const DYNAMIC_RE = |
| 28 | + /import\s*\(\s*["'](@radix-ui\/[^"']+|radix-ui(?:\/[^"']*)?)["']\s*\)/g; |
| 29 | + |
| 30 | +function listFiles() { |
| 31 | + const globs = SCAN_ROOTS.flatMap((r) => [ |
| 32 | + `"${r}/**/*.ts"`, |
| 33 | + `"${r}/**/*.tsx"`, |
| 34 | + ]); |
| 35 | + const out = execSync(`git -C "${ROOT}" ls-files ${globs.join(" ")}`, { |
| 36 | + encoding: "utf8", |
| 37 | + }); |
| 38 | + return out |
| 39 | + .split("\n") |
| 40 | + .map((f) => f.trim()) |
| 41 | + .filter(Boolean) |
| 42 | + .filter((f) => !f.endsWith(".d.ts") && !f.includes("/generated")); |
| 43 | +} |
| 44 | + |
| 45 | +function importedNames(clause) { |
| 46 | + if (!clause) return ["*"]; // bare `import "pkg"` — side-effect import |
| 47 | + const names = []; |
| 48 | + const braces = clause.match(/\{([^}]*)\}/); |
| 49 | + if (braces) { |
| 50 | + for (let n of braces[1].split(",")) { |
| 51 | + n = n.trim().replace(/^type\s+/, ""); |
| 52 | + if (!n) continue; |
| 53 | + names.push(n.split(/\s+as\s+/)[0].trim()); |
| 54 | + } |
| 55 | + } |
| 56 | + const outsideBraces = clause |
| 57 | + .replace(/\{[^}]*\}/, "") |
| 58 | + .trim() |
| 59 | + .replace(/,$/, "") |
| 60 | + .trim(); |
| 61 | + if (outsideBraces) names.push("*"); // default, namespace, or star import — all names reachable |
| 62 | + return names.length ? names : ["*"]; |
| 63 | +} |
| 64 | + |
| 65 | +function violationsInSource(src) { |
| 66 | + const hits = new Set(); |
| 67 | + IMPORT_RE.lastIndex = 0; |
| 68 | + for (let m = IMPORT_RE.exec(src); m; m = IMPORT_RE.exec(src)) { |
| 69 | + const [, clause, spec] = m; |
| 70 | + if (spec === "@radix-ui/themes") { |
| 71 | + for (const name of importedNames(clause)) { |
| 72 | + if (!ALLOWED_THEMES_IMPORTS.has(name)) hits.add(`${spec}#${name}`); |
| 73 | + } |
| 74 | + } else { |
| 75 | + hits.add(spec); |
| 76 | + } |
| 77 | + } |
| 78 | + DYNAMIC_RE.lastIndex = 0; |
| 79 | + for (let m = DYNAMIC_RE.exec(src); m; m = DYNAMIC_RE.exec(src)) { |
| 80 | + hits.add(m[1] === "@radix-ui/themes" ? `${m[1]}#*` : m[1]); |
| 81 | + } |
| 82 | + return [...hits].sort(); |
| 83 | +} |
| 84 | + |
| 85 | +function findViolations() { |
| 86 | + const violations = {}; |
| 87 | + for (const path of listFiles()) { |
| 88 | + let src; |
| 89 | + try { |
| 90 | + src = readFileSync(join(ROOT, path), "utf8"); |
| 91 | + } catch { |
| 92 | + continue; |
| 93 | + } |
| 94 | + if (!src.includes("radix-ui")) continue; |
| 95 | + const hits = violationsInSource(src); |
| 96 | + if (hits.length) violations[path] = hits; |
| 97 | + } |
| 98 | + return violations; |
| 99 | +} |
| 100 | + |
| 101 | +function loadAllowlist() { |
| 102 | + if (!existsSync(ALLOWLIST)) return {}; |
| 103 | + return JSON.parse(readFileSync(ALLOWLIST, "utf8")).files ?? {}; |
| 104 | +} |
| 105 | + |
| 106 | +function saveAllowlist(files) { |
| 107 | + const sorted = Object.fromEntries( |
| 108 | + Object.keys(files) |
| 109 | + .sort() |
| 110 | + .map((k) => [k, files[k]]), |
| 111 | + ); |
| 112 | + writeFileSync( |
| 113 | + ALLOWLIST, |
| 114 | + `${JSON.stringify({ note: "Radix imports frozen at baseline. Only Box/Flex/Text from @radix-ui/themes are allowed in new code; use @posthog/quill instead. Remove entries as you migrate. Goal: empty.", files: sorted }, null, 2)}\n`, |
| 115 | + ); |
| 116 | + try { |
| 117 | + execSync(`pnpm exec biome format --write "${ALLOWLIST}"`, { |
| 118 | + cwd: ROOT, |
| 119 | + stdio: "ignore", |
| 120 | + }); |
| 121 | + } catch { |
| 122 | + // biome unavailable (e.g. before pnpm install) — commit hooks/CI will format |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +const mode = process.argv[2]; |
| 127 | +if (mode === "--help" || mode === "-h") { |
| 128 | + console.log(USAGE); |
| 129 | + process.exit(0); |
| 130 | +} |
| 131 | + |
| 132 | +const current = findViolations(); |
| 133 | +const allow = loadAllowlist(); |
| 134 | + |
| 135 | +if (mode === "--init") { |
| 136 | + saveAllowlist(current); |
| 137 | + console.log( |
| 138 | + `Baseline written: ${Object.keys(current).length} file(s) with frozen Radix imports.`, |
| 139 | + ); |
| 140 | + process.exit(0); |
| 141 | +} |
| 142 | + |
| 143 | +if (mode === "--prune") { |
| 144 | + const kept = {}; |
| 145 | + for (const f of Object.keys(allow)) { |
| 146 | + if (!current[f]) continue; |
| 147 | + kept[f] = allow[f].filter((e) => current[f].includes(e)); |
| 148 | + if (!kept[f].length) delete kept[f]; |
| 149 | + } |
| 150 | + const before = Object.values(allow).flat().length; |
| 151 | + const after = Object.values(kept).flat().length; |
| 152 | + saveAllowlist(kept); |
| 153 | + console.log( |
| 154 | + `Pruned. ${before - after} import(s) migrated, ${after} remaining.`, |
| 155 | + ); |
| 156 | + process.exit(0); |
| 157 | +} |
| 158 | + |
| 159 | +const fresh = []; |
| 160 | +for (const [file, entries] of Object.entries(current)) { |
| 161 | + const allowed = new Set(allow[file] ?? []); |
| 162 | + for (const e of entries) if (!allowed.has(e)) fresh.push({ file, entry: e }); |
| 163 | +} |
| 164 | + |
| 165 | +const migrated = []; |
| 166 | +for (const [file, entries] of Object.entries(allow)) { |
| 167 | + const now = new Set(current[file] ?? []); |
| 168 | + for (const e of entries) if (!now.has(e)) migrated.push({ file, entry: e }); |
| 169 | +} |
| 170 | + |
| 171 | +if (migrated.length) { |
| 172 | + console.log( |
| 173 | + `\n✓ ${migrated.length} Radix import(s) migrated since baseline — run --prune to shrink the allowlist.`, |
| 174 | + ); |
| 175 | +} |
| 176 | + |
| 177 | +if (fresh.length) { |
| 178 | + console.error( |
| 179 | + `\n✗ ${fresh.length} NEW Radix import(s) — Radix is frozen; new UI comes from @posthog/quill:\n`, |
| 180 | + ); |
| 181 | + for (const { file, entry } of fresh) console.error(` ${file}\n ${entry}`); |
| 182 | + console.error( |
| 183 | + `\nOnly Box, Flex, and Text from @radix-ui/themes are allowed in new code. Use the |
| 184 | +@posthog/quill equivalent (Button, Dialog*, Tooltip*, DropdownMenu*, ...) instead. |
| 185 | +If you are only moving already-baselined code between files, update |
| 186 | +scripts/radix-allowlist.json to match and justify it in review.`, |
| 187 | + ); |
| 188 | + process.exit(1); |
| 189 | +} |
| 190 | + |
| 191 | +console.log( |
| 192 | + `\n✓ No new Radix imports. ${Object.values(allow).flat().length} frozen import(s) across ${Object.keys(allow).length} file(s) (baseline). Goal: 0.`, |
| 193 | +); |
| 194 | +process.exit(0); |
0 commit comments