// Fails when a package's `solid-js` peer major differs from its latest published release but the // next version is not a major bump. `@solid-primitives/timer` shipped Solid 2 as `1.4.5` (a patch) // and `1.5.0` (a minor), which broke Solid 1 projects that accept `^1.x` (#1151). // // A package passes when its peer major is unchanged, when it is not published yet, or when a // major bump is already in place (`package.json` version) or pending (a `major` changeset). // Network problems only warn, so a registry outage never blocks CI. // // Usage: pnpm run check:peer-major import * as fs from "node:fs"; import * as path from "node:path"; const repoRoot = path.resolve(import.meta.dirname, ".."); export type Published = { version: string; solidPeer: string | undefined }; export type Input = { name: string; version: string; solidPeer: string | undefined; published: Published | undefined; pendingBumps: readonly string[]; }; export const majorOf = (spec: string | undefined): number | undefined => { const match = spec?.match(/\d+/); return match ? Number(match[0]) : undefined; }; /** Returns a message when the package breaks the rule, otherwise `undefined`. */ export function evaluate(input: Input): string | undefined { const { name, version, solidPeer, published, pendingBumps } = input; if (!published) return undefined; const localPeer = majorOf(solidPeer); const publishedPeer = majorOf(published.solidPeer); if (localPeer === undefined || publishedPeer === undefined || localPeer === publishedPeer) { return undefined; } if (pendingBumps.includes("major")) return undefined; if ((majorOf(version) ?? 0) > (majorOf(published.version) ?? 0)) return undefined; return ( `${name}: the solid-js peer moves from ${published.solidPeer} (published ${published.version}) ` + `to ${solidPeer} (local ${version}) without a major bump. ` + `Add a changeset with "${name}": major.` ); } const stable = /^\d+\.\d+\.\d+$/; const compare = (a: string, b: string) => { const [x, y] = [a, b].map(v => v.split(".").map(Number)); return x![0]! - y![0]! || x![1]! - y![1]! || x![2]! - y![2]!; }; function resolvePeer( spec: string | undefined, catalog: Record, ): string | undefined { return spec?.startsWith("catalog:") ? catalog["solid-js"] : spec; } /** Reads the `catalogs.peer` entries of a pnpm-workspace.yaml (no YAML parser needed). */ export function parsePeerCatalog(yaml: string): Record { const lines = yaml.split("\n"); const unquote = (value: string) => value.replace(/^['"]|['"]$/g, ""); const entries: Record = {}; let inCatalogs = false; let inPeer = false; for (const line of lines) { const text = line.trim(); if (!text) continue; const indent = line.length - line.trimStart().length; if (indent === 0) { inCatalogs = text === "catalogs:"; inPeer = false; } else if (inCatalogs && indent === 2) { inPeer = text === "peer:"; } else if (inPeer && indent >= 4) { const colon = text.indexOf(":"); if (colon > 0) entries[unquote(text.slice(0, colon))] = unquote(text.slice(colon + 1).trim()); } } return entries; } const readPeerCatalog = () => parsePeerCatalog(fs.readFileSync(path.join(repoRoot, "pnpm-workspace.yaml"), "utf8")); function readPendingBumps(): Map { const dir = path.join(repoRoot, ".changeset"); const bumps = new Map(); for (const file of fs.readdirSync(dir)) { if (!file.endsWith(".md") || file === "README.md") continue; const front = fs.readFileSync(path.join(dir, file), "utf8").match(/^---\n([\s\S]*?)\n---/)?.[1]; for (const line of front?.split("\n") ?? []) { const m = line.match(/^["']?([^"':]+)["']?:\s*(major|minor|patch)\s*$/); if (m) bumps.set(m[1]!, [...(bumps.get(m[1]!) ?? []), m[2]!]); } } return bumps; } async function fetchPublished(name: string): Promise { try { const res = await fetch(`https://registry.npmjs.org/${name.replaceAll("/", "%2f")}`, { headers: { accept: "application/vnd.npm.install-v1+json" }, signal: AbortSignal.timeout(20_000), }); if (res.status === 404) return undefined; if (!res.ok) return "error"; const doc = (await res.json()) as { versions: Record }>; }; const version = Object.keys(doc.versions) .filter(v => stable.test(v)) .sort(compare) .at(-1); if (!version) return undefined; return { version, solidPeer: doc.versions[version]!.peerDependencies?.["solid-js"] }; } catch { return "error"; } } async function main() { const catalog = readPeerCatalog(); const pending = readPendingBumps(); const packages = fs .readdirSync(path.join(repoRoot, "packages"), { withFileTypes: true }) .filter( d => d.isDirectory() && fs.existsSync(path.join(repoRoot, "packages", d.name, "package.json")), ) .map(d => JSON.parse(fs.readFileSync(path.join(repoRoot, "packages", d.name, "package.json"), "utf8")), ) .filter(pkg => !pkg.private); const problems: string[] = []; let skipped = 0; const queue = [...packages]; await Promise.all( Array.from({ length: 8 }, async () => { for (let pkg = queue.shift(); pkg; pkg = queue.shift()) { const published = await fetchPublished(pkg.name); if (published === "error") { skipped++; continue; } const message = evaluate({ name: pkg.name, version: pkg.version, solidPeer: resolvePeer(pkg.peerDependencies?.["solid-js"], catalog), published, pendingBumps: pending.get(pkg.name) ?? [], }); if (message) problems.push(message); } }), ); if (skipped) console.warn(`check-solid-peer-major: skipped ${skipped} package(s), registry unreachable.`); if (problems.length) { console.error(`\n${problems.sort().join("\n")}\n`); process.exit(1); } console.log(`check-solid-peer-major: ok (${packages.length - skipped} packages checked).`); } if (process.argv[1] === import.meta.filename) await main();