|
| 1 | +import { readFileSync, readdirSync, statSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +import { parsePackageJson } from "../../parsers/package-json"; |
| 5 | +import type { IMetaRule, IViolation } from "../../types"; |
| 6 | + |
| 7 | +/* |
| 8 | + * Dev tooling that must stay in lockstep across every app that declares it. |
| 9 | + * Drift here means two apps lint/format/typecheck with different rule sets — |
| 10 | + * a class of defect that has bitten this repo before. Only apps that actually |
| 11 | + * declare a tool are compared, so an app legitimately omitting one (e.g. the |
| 12 | + * static docs site has no ESLint) is never forced to add it. |
| 13 | + */ |
| 14 | +const SHARED_TOOLS = [ |
| 15 | + "eslint", |
| 16 | + "typescript", |
| 17 | + "prettier", |
| 18 | + "knip", |
| 19 | + "typescript-eslint", |
| 20 | + "eslint-config-prettier", |
| 21 | + "eslint-plugin-import", |
| 22 | + "eslint-plugin-promise", |
| 23 | + "eslint-plugin-sonarjs", |
| 24 | + "eslint-plugin-unicorn", |
| 25 | + "@eslint/js", |
| 26 | + "husky", |
| 27 | +] as const; |
| 28 | + |
| 29 | +interface IAppDeps { |
| 30 | + readonly app: string; |
| 31 | + readonly file: string; |
| 32 | + readonly deps: Record<string, string>; |
| 33 | +} |
| 34 | + |
| 35 | +function readApps(appsDir: string): IAppDeps[] { |
| 36 | + const out: IAppDeps[] = []; |
| 37 | + let entries: string[]; |
| 38 | + |
| 39 | + try { |
| 40 | + entries = readdirSync(appsDir); |
| 41 | + } catch { |
| 42 | + return out; |
| 43 | + } |
| 44 | + |
| 45 | + for (const entry of entries) { |
| 46 | + const dir = join(appsDir, entry); |
| 47 | + |
| 48 | + let isDir: boolean; |
| 49 | + |
| 50 | + try { |
| 51 | + isDir = statSync(dir).isDirectory(); |
| 52 | + } catch { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if (!isDir) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + const file = join(dir, "package.json"); |
| 61 | + let text: string; |
| 62 | + |
| 63 | + try { |
| 64 | + text = readFileSync(file, "utf8"); |
| 65 | + } catch { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + const pkg = parsePackageJson(text); |
| 70 | + |
| 71 | + if (pkg === null) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + out.push({ |
| 76 | + app: entry, |
| 77 | + file, |
| 78 | + deps: { ...pkg.dependencies, ...pkg.devDependencies }, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + return out; |
| 83 | +} |
| 84 | + |
| 85 | +interface IDeclarer { |
| 86 | + readonly app: string; |
| 87 | + readonly file: string; |
| 88 | + readonly version: string; |
| 89 | +} |
| 90 | + |
| 91 | +export function checkSharedToolVersionParity(appsDir: string): IViolation[] { |
| 92 | + const violations: IViolation[] = []; |
| 93 | + const apps = readApps(appsDir); |
| 94 | + |
| 95 | + for (const tool of SHARED_TOOLS) { |
| 96 | + const declarers: IDeclarer[] = apps |
| 97 | + .map((app) => ({ app: app.app, file: app.file, version: app.deps[tool] })) |
| 98 | + .filter((entry): entry is IDeclarer => typeof entry.version === "string"); |
| 99 | + |
| 100 | + if (declarers.length < 2) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + const versions = new Set(declarers.map((entry) => entry.version)); |
| 105 | + |
| 106 | + if (versions.size === 1) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + const summary = declarers |
| 111 | + .map((entry) => `${entry.app}@${entry.version}`) |
| 112 | + .join(", "); |
| 113 | + |
| 114 | + for (const declarer of declarers) { |
| 115 | + violations.push({ |
| 116 | + file: declarer.file, |
| 117 | + rule: "shared-tool-version-parity", |
| 118 | + message: `${tool} version drifts across apps (${summary}) — shared dev tooling must be pinned to one version in every app that declares it.`, |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return violations; |
| 124 | +} |
| 125 | + |
| 126 | +/** Shared dev tooling must be pinned to the same version across apps. */ |
| 127 | +export const sharedToolVersionParityRule: IMetaRule = { |
| 128 | + id: "shared-tool-version-parity", |
| 129 | + category: "supply-chain", |
| 130 | + description: |
| 131 | + "Shared dev tooling (ESLint, TypeScript, Prettier, knip, …) must be pinned to the same version in every app that declares it.", |
| 132 | + run({ root }) { |
| 133 | + return checkSharedToolVersionParity(join(root, "..")); |
| 134 | + }, |
| 135 | +}; |
0 commit comments