|
| 1 | +#!/usr/bin/env node |
| 2 | +// @effect-diagnostics nodeBuiltinImport:off - one-shot CI gate over a child process. |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import * as NodeURL from "node:url"; |
| 6 | + |
| 7 | +// `shadcn/no-restyle` stays a warning while the existing className overrides on |
| 8 | +// components/ui exports are migrated to variants (see vite.config.ts). This gate keeps the |
| 9 | +// count from growing: CI fails when findings exceed the ceiling. Lower the ceiling when you |
| 10 | +// migrate a file, and delete this script when the rule becomes an error. |
| 11 | +export const RESTYLE_CEILING = 1247; |
| 12 | + |
| 13 | +const RULE = "shadcn(no-restyle)"; |
| 14 | + |
| 15 | +const repoRoot = NodePath.resolve(NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)), ".."); |
| 16 | + |
| 17 | +interface LintReport { |
| 18 | + readonly diagnostics: ReadonlyArray<{ readonly code: string }>; |
| 19 | +} |
| 20 | + |
| 21 | +export function countRestyleFindings(report: LintReport): number { |
| 22 | + return report.diagnostics.filter((diagnostic) => diagnostic.code === RULE).length; |
| 23 | +} |
| 24 | + |
| 25 | +export function evaluateCeiling( |
| 26 | + count: number, |
| 27 | + ceiling: number, |
| 28 | +): { readonly ok: boolean; readonly message: string } { |
| 29 | + if (count > ceiling) { |
| 30 | + return { |
| 31 | + ok: false, |
| 32 | + message: |
| 33 | + `${RULE}: ${count} findings exceed the ceiling of ${ceiling}. ` + |
| 34 | + "Use a variant or size on the components/ui export instead of a className override " + |
| 35 | + "(run `vp lint apps/web/src` for the list).", |
| 36 | + }; |
| 37 | + } |
| 38 | + const slack = ceiling - count; |
| 39 | + return { |
| 40 | + ok: true, |
| 41 | + message: |
| 42 | + slack === 0 |
| 43 | + ? `${RULE}: ${count} findings, at the ceiling.` |
| 44 | + : `${RULE}: ${count} findings, ${slack} below the ceiling of ${ceiling}. Lower RESTYLE_CEILING in scripts/lint-restyle-ceiling.ts to ${count}.`, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +function main() { |
| 49 | + const result = NodeChildProcess.spawnSync("vp", ["lint", "--format", "json", "apps/web/src"], { |
| 50 | + cwd: repoRoot, |
| 51 | + encoding: "utf8", |
| 52 | + maxBuffer: 256 * 1024 * 1024, |
| 53 | + }); |
| 54 | + if (result.error) throw result.error; |
| 55 | + const report = JSON.parse(result.stdout) as LintReport; |
| 56 | + const verdict = evaluateCeiling(countRestyleFindings(report), RESTYLE_CEILING); |
| 57 | + process.stdout.write(`${verdict.message}\n`); |
| 58 | + process.exitCode = verdict.ok ? 0 : 1; |
| 59 | +} |
| 60 | + |
| 61 | +if ( |
| 62 | + process.argv[1] !== undefined && |
| 63 | + NodePath.resolve(process.argv[1]) === NodeURL.fileURLToPath(import.meta.url) |
| 64 | +) { |
| 65 | + main(); |
| 66 | +} |
0 commit comments