Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

lint-restyle-ceiling.ts

66 lines2.3 KB

1#!/usr/bin/env node
2// @effect-diagnostics nodeBuiltinImport:off - one-shot CI gate over a child process.
3import * as NodeChildProcess from "node:child_process";
4import * as NodePath from "node:path";
5import * 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.
11export const RESTYLE_CEILING = 627;
12​
13const RULE = "shadcn(no-restyle)";
14​
15const repoRoot = NodePath.resolve(NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)), "..");
16​
17interface LintReport {
18 readonly diagnostics: ReadonlyArray<{ readonly code: string }>;
20​
21export function countRestyleFindings(report: LintReport): number {
22 return report.diagnostics.filter((diagnostic) => diagnostic.code === RULE).length;
24​
25export 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 };
47​
48function 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;
60​
61if (
62 process.argv[1] !== undefined &&
63 NodePath.resolve(process.argv[1]) === NodeURL.fileURLToPath(import.meta.url)
64) {
65 main();