|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { existsSync, readFileSync, readdirSync } from "node:fs"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const ROOT = join(import.meta.dirname, ".."); |
| 8 | +const RUNTIME_DEPENDENCY_FIELDS = ["dependencies", "optionalDependencies", "peerDependencies"]; |
| 9 | + |
| 10 | +/** |
| 11 | + * The deprecated @hyperframes/core/studio-api forwarding surface is public and |
| 12 | + * cannot disappear before a breaking release. Keep its one known package SCC |
| 13 | + * explicit so every new cycle still fails CI. Delete this exception together |
| 14 | + * with the forwarding surface in the next breaking release. |
| 15 | + */ |
| 16 | +export const ALLOWED_COMPATIBILITY_CYCLES = [ |
| 17 | + { |
| 18 | + packages: ["@hyperframes/core", "@hyperframes/studio-server"], |
| 19 | + reason: "Deprecated core/studio-api forwarding exports; remove at the next breaking release.", |
| 20 | + }, |
| 21 | +]; |
| 22 | + |
| 23 | +function canonicalComponent(names) { |
| 24 | + return [...names].sort().join(" -> "); |
| 25 | +} |
| 26 | + |
| 27 | +export function listRuntimePackageCycles(packages) { |
| 28 | + const byName = new Map(packages.map((pkg) => [pkg.name, pkg])); |
| 29 | + const edges = new Map( |
| 30 | + // Collecting three dependency classes in one expression keeps the graph |
| 31 | + // construction declarative; focused tests cover runtime versus dev edges. |
| 32 | + // fallow-ignore-next-line complexity |
| 33 | + packages.map((pkg) => { |
| 34 | + const targets = new Set(); |
| 35 | + for (const field of RUNTIME_DEPENDENCY_FIELDS) { |
| 36 | + for (const dependency of Object.keys(pkg[field] ?? {})) { |
| 37 | + if (byName.has(dependency)) targets.add(dependency); |
| 38 | + } |
| 39 | + } |
| 40 | + return [pkg.name, [...targets].sort()]; |
| 41 | + }), |
| 42 | + ); |
| 43 | + |
| 44 | + let nextIndex = 0; |
| 45 | + const indexes = new Map(); |
| 46 | + const lowLinks = new Map(); |
| 47 | + const stack = []; |
| 48 | + const onStack = new Set(); |
| 49 | + const components = []; |
| 50 | + |
| 51 | + // Tarjan's strongly-connected-component walk is intentionally branchy; the |
| 52 | + // state transitions mirror the algorithm and are covered by focused tests. |
| 53 | + // fallow-ignore-next-line complexity |
| 54 | + function visit(name) { |
| 55 | + indexes.set(name, nextIndex); |
| 56 | + lowLinks.set(name, nextIndex); |
| 57 | + nextIndex += 1; |
| 58 | + stack.push(name); |
| 59 | + onStack.add(name); |
| 60 | + |
| 61 | + for (const target of edges.get(name) ?? []) { |
| 62 | + if (!indexes.has(target)) { |
| 63 | + visit(target); |
| 64 | + lowLinks.set(name, Math.min(lowLinks.get(name), lowLinks.get(target))); |
| 65 | + } else if (onStack.has(target)) { |
| 66 | + lowLinks.set(name, Math.min(lowLinks.get(name), indexes.get(target))); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (lowLinks.get(name) !== indexes.get(name)) return; |
| 71 | + const component = []; |
| 72 | + let member; |
| 73 | + do { |
| 74 | + member = stack.pop(); |
| 75 | + onStack.delete(member); |
| 76 | + component.push(member); |
| 77 | + } while (member !== name); |
| 78 | + |
| 79 | + const selfCycle = component.length === 1 && (edges.get(name) ?? []).includes(name); |
| 80 | + if (component.length > 1 || selfCycle) components.push(component.sort()); |
| 81 | + } |
| 82 | + |
| 83 | + for (const name of [...byName.keys()].sort()) { |
| 84 | + if (!indexes.has(name)) visit(name); |
| 85 | + } |
| 86 | + return components.sort((a, b) => canonicalComponent(a).localeCompare(canonicalComponent(b))); |
| 87 | +} |
| 88 | + |
| 89 | +export function listPackageCycleIssues(packages, allowed = ALLOWED_COMPATIBILITY_CYCLES) { |
| 90 | + const allowedKeys = new Set(allowed.map((entry) => canonicalComponent(entry.packages))); |
| 91 | + return listRuntimePackageCycles(packages) |
| 92 | + .map(canonicalComponent) |
| 93 | + .filter((component) => !allowedKeys.has(component)) |
| 94 | + .map((component) => `runtime workspace dependency cycle: ${component}`); |
| 95 | +} |
| 96 | + |
| 97 | +export function readWorkspacePackages(root = ROOT) { |
| 98 | + return readdirSync(join(root, "packages")) |
| 99 | + .sort() |
| 100 | + .map((directory) => join(root, "packages", directory, "package.json")) |
| 101 | + .filter(existsSync) |
| 102 | + .map((path) => JSON.parse(readFileSync(path, "utf8"))); |
| 103 | +} |
| 104 | + |
| 105 | +function main() { |
| 106 | + const packages = readWorkspacePackages(); |
| 107 | + const cycles = listRuntimePackageCycles(packages); |
| 108 | + const issues = listPackageCycleIssues(packages); |
| 109 | + if (issues.length > 0) { |
| 110 | + console.error("Package cycle violations:"); |
| 111 | + issues.forEach((issue) => console.error(`- ${issue}`)); |
| 112 | + process.exitCode = 1; |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const allowedKeys = new Set( |
| 117 | + ALLOWED_COMPATIBILITY_CYCLES.map((entry) => canonicalComponent(entry.packages)), |
| 118 | + ); |
| 119 | + const activeCompatibilityCycles = cycles.filter((cycle) => |
| 120 | + allowedKeys.has(canonicalComponent(cycle)), |
| 121 | + ); |
| 122 | + if (activeCompatibilityCycles.length > 0) { |
| 123 | + console.log( |
| 124 | + `Package graph verified; ${activeCompatibilityCycles.length} explicit compatibility cycle remains until the next breaking release.`, |
| 125 | + ); |
| 126 | + return; |
| 127 | + } |
| 128 | + console.log("Package graph verified: runtime workspace dependencies are acyclic."); |
| 129 | +} |
| 130 | + |
| 131 | +if (process.argv[1] === fileURLToPath(import.meta.url)) main(); |
0 commit comments