|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execSync } from "node:child_process"; |
| 3 | +import { readFileSync } 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 SCAN_ROOTS = ["packages", "apps"]; |
| 9 | +const ALLOWED_THEMES_IMPORTS = new Set(["Box", "Flex", "Text"]); |
| 10 | + |
| 11 | +const USAGE = `check-radix-imports — Radix is frozen; new UI comes from @posthog/quill. |
| 12 | +
|
| 13 | + node scripts/check-radix-imports.mjs [base] fail on Radix imports added since <base> |
| 14 | + (default: merge-base with origin/main) |
| 15 | +
|
| 16 | +Only Box, Flex, and Text from @radix-ui/themes are permitted in new code. Every |
| 17 | +other Radix component and every other @radix-ui/* package is frozen: existing |
| 18 | +imports may stay until migrated, but a changed file must not gain any.`; |
| 19 | + |
| 20 | +// Matches static imports, re-exports, and dynamic imports of Radix packages. |
| 21 | +const IMPORT_RE = |
| 22 | + /(?:import|export)\s+(?:type\s+)?([\w$]+|\*\s+as\s+[\w$]+|\{[^}]*\}|[\w$]+\s*,\s*\{[^}]*\}|\*)?\s*(?:from\s*)?["'](@radix-ui\/[^"']+|radix-ui(?:\/[^"']*)?)["']/g; |
| 23 | +const DYNAMIC_RE = |
| 24 | + /import\s*\(\s*["'](@radix-ui\/[^"']+|radix-ui(?:\/[^"']*)?)["']\s*\)/g; |
| 25 | + |
| 26 | +function git(cmd) { |
| 27 | + return execSync(`git -C "${ROOT}" ${cmd}`, { encoding: "utf8" }); |
| 28 | +} |
| 29 | + |
| 30 | +function importedNames(clause) { |
| 31 | + if (!clause) return ["*"]; // bare `import "pkg"` — side-effect import |
| 32 | + const names = []; |
| 33 | + const braces = clause.match(/\{([^}]*)\}/); |
| 34 | + if (braces) { |
| 35 | + for (let n of braces[1].split(",")) { |
| 36 | + n = n.trim().replace(/^type\s+/, ""); |
| 37 | + if (!n) continue; |
| 38 | + names.push(n.split(/\s+as\s+/)[0].trim()); |
| 39 | + } |
| 40 | + } |
| 41 | + const outside = clause |
| 42 | + .replace(/\{[^}]*\}/, "") |
| 43 | + .replace(/,\s*$/, "") |
| 44 | + .trim(); |
| 45 | + if (outside) names.push("*"); // default, namespace, or star import — all names reachable |
| 46 | + return names.length ? names : ["*"]; |
| 47 | +} |
| 48 | + |
| 49 | +function frozenImports(src) { |
| 50 | + const hits = new Set(); |
| 51 | + IMPORT_RE.lastIndex = 0; |
| 52 | + for (let m = IMPORT_RE.exec(src); m; m = IMPORT_RE.exec(src)) { |
| 53 | + const [, clause, spec] = m; |
| 54 | + if (spec === "@radix-ui/themes") { |
| 55 | + for (const name of importedNames(clause)) { |
| 56 | + if (!ALLOWED_THEMES_IMPORTS.has(name)) hits.add(`${spec}#${name}`); |
| 57 | + } |
| 58 | + } else { |
| 59 | + hits.add(spec); |
| 60 | + } |
| 61 | + } |
| 62 | + DYNAMIC_RE.lastIndex = 0; |
| 63 | + for (let m = DYNAMIC_RE.exec(src); m; m = DYNAMIC_RE.exec(src)) { |
| 64 | + hits.add(m[1] === "@radix-ui/themes" ? `${m[1]}#*` : m[1]); |
| 65 | + } |
| 66 | + return hits; |
| 67 | +} |
| 68 | + |
| 69 | +const arg = process.argv[2]; |
| 70 | +if (arg === "--help" || arg === "-h") { |
| 71 | + console.log(USAGE); |
| 72 | + process.exit(0); |
| 73 | +} |
| 74 | + |
| 75 | +let base = arg; |
| 76 | +if (!base) { |
| 77 | + try { |
| 78 | + base = git("merge-base origin/main HEAD").trim(); |
| 79 | + } catch { |
| 80 | + base = "origin/main"; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +const isSource = (f) => |
| 85 | + /\.tsx?$/.test(f) && |
| 86 | + !f.endsWith(".d.ts") && |
| 87 | + !f.includes("/generated") && |
| 88 | + SCAN_ROOTS.some((r) => f.startsWith(`${r}/`)); |
| 89 | + |
| 90 | +const changed = new Set( |
| 91 | + [ |
| 92 | + ...git(`diff --name-only --diff-filter=ACMR ${base}`).split("\n"), |
| 93 | + ...git("ls-files --others --exclude-standard").split("\n"), |
| 94 | + ] |
| 95 | + .map((f) => f.trim()) |
| 96 | + .filter(Boolean) |
| 97 | + .filter(isSource), |
| 98 | +); |
| 99 | + |
| 100 | +const fresh = []; |
| 101 | +for (const file of changed) { |
| 102 | + let head; |
| 103 | + try { |
| 104 | + head = readFileSync(join(ROOT, file), "utf8"); |
| 105 | + } catch { |
| 106 | + continue; // deleted in worktree |
| 107 | + } |
| 108 | + if (!head.includes("radix-ui")) continue; |
| 109 | + let baseSrc = ""; |
| 110 | + try { |
| 111 | + baseSrc = git(`show ${base}:"${file}"`); |
| 112 | + } catch { |
| 113 | + // new file — everything counts as added |
| 114 | + } |
| 115 | + const before = frozenImports(baseSrc); |
| 116 | + for (const entry of frozenImports(head)) { |
| 117 | + if (!before.has(entry)) fresh.push({ file, entry }); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +if (fresh.length) { |
| 122 | + console.error( |
| 123 | + `\n✗ ${fresh.length} NEW Radix import(s) since ${base.slice(0, 12)} — Radix is frozen; new UI comes from @posthog/quill:\n`, |
| 124 | + ); |
| 125 | + for (const { file, entry } of fresh) console.error(` ${file}\n ${entry}`); |
| 126 | + console.error( |
| 127 | + `\nOnly Box, Flex, and Text from @radix-ui/themes are allowed in new code. Use the |
| 128 | +@posthog/quill equivalent (Button, Dialog*, Tooltip*, DropdownMenu*, ...) instead.`, |
| 129 | + ); |
| 130 | + process.exit(1); |
| 131 | +} |
| 132 | + |
| 133 | +console.log( |
| 134 | + `✓ No new Radix imports in ${changed.size} changed file(s) vs ${base.slice(0, 12)}.`, |
| 135 | +); |
0 commit comments