import { readFileSync, existsSync } from "node:fs"; import { resolve, dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { dependencyIdentity } from "./package-rollout-gate.mjs"; import { measure, overCeiling } from "./experience-audit.mjs"; const root = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); // Names mid-migration to @bitbaum/*: a plan may still name the old one until its // repository is renamed (scripts/packages/rename-package.mjs). Retire an entry // when no adopter uses the old name. const LEGACY_NAMES = { "bip-kit": "@bitbaum/bip-kit" }; const sameBlock = (declared, required) => declared === required || LEGACY_NAMES[declared] === required || LEGACY_NAMES[required] === declared; export function auditComposition( dir, baseline = JSON.parse( readFileSync(join(root, "registers/product-building-blocks.json"), "utf8"), ), ) { const findings = []; const read = (file) => readFileSync(resolve(dir, file), "utf8"); if (!existsSync(resolve(dir, "package-plan.json"))) return [ "Missing package-plan.json: inventory shared building blocks before implementation.", ]; let plan, manifest; try { plan = JSON.parse(read("package-plan.json")); manifest = JSON.parse(read("package.json")); } catch { return ["Package plan and package manifest must be valid JSON."]; } const deps = { ...manifest.dependencies, ...manifest.devDependencies }; const decisions = { ...Object.fromEntries( Object.entries(plan.blocks ?? {}).map(([key, entry]) => [ key, { package: entry.package }, ]), ), ...baseline, }; for (const [feature, required] of Object.entries(decisions)) { const entry = plan.blocks?.[feature]; if (!entry) { findings.push(`${feature}: missing building-block decision`); continue; } if (entry.exception) { if ( typeof entry.exception !== "string" || entry.exception.trim().length < 40 ) findings.push( `${feature}: explain the actual incompatibility or why the feature is absent`, ); continue; } if (!sameBlock(entry.package, required.package)) findings.push( `${feature}: expected ${required.package}, or document the specific exception`, ); const installedNames = Object.entries(deps) .filter( ([name, spec]) => dependencyIdentity(name, spec) === entry.package, ) .map(([name]) => name); if (!installedNames.length) findings.push(`${feature}: ${entry.package} is not installed`); if (!Array.isArray(entry.bindings) || entry.bindings.length === 0) { findings.push(`${feature}: no rendered/runtime binding declared`); continue; } for (const binding of entry.bindings) { if ( !binding.file || resolve(dir, binding.file).startsWith(resolve(dir) + "/") === false || !existsSync(resolve(dir, binding.file)) ) { findings.push( `${feature}: binding file is missing or outside this repository`, ); continue; } const source = read(binding.file); // `via`: the block reaches the product through another shared package // that wraps it (account mail is mail-kit through accountkit's // createAuthMailer). The wrapper must be installed and imported here. const via = typeof binding.via === "string" ? binding.via : null; const viaNames = via ? Object.entries(deps) .filter(([name, spec]) => dependencyIdentity(name, spec) === via) .map(([name]) => name) : []; if (via && !viaNames.length) findings.push(`${feature}: ${via} (via) is not installed`); const importNames = [entry.package, ...installedNames, ...viaNames].map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), ); const importsPackage = importNames.some((name) => new RegExp(`(?:from\\s*|import\\s*)['"]${name}(?:/[^'"]*)?['"]`).test( source, ), ); if (!importsPackage) findings.push( `${feature}: ${binding.file} does not import its declared package`, ); const usageSources = []; for (const file of binding.usageFiles ?? []) { if ( !file || file.startsWith("/") || file.split("/").includes("..") || !existsSync(resolve(dir, file)) ) { findings.push(`${feature}: usage file is missing or outside this repository`); continue; } usageSources.push(read(file)); } const bindingSources = [source, ...usageSources]; for (const symbol of binding.symbols ?? []) { const escaped = String(symbol).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const used = new RegExp(`(?:<${escaped}\\b|\\b${escaped}\\s*\\()`); if (!bindingSources.some((content) => used.test(content))) findings.push(`${feature}: ${symbol} is not used in ${binding.file}`); } } for (const surface of entry.surfaces ?? []) { if ( !surface || surface.startsWith("/") || surface.split("/").includes("..") || !existsSync(resolve(dir, surface)) ) findings.push(`${feature}: required product surface is missing or unsafe: ${surface}`); } } if ( !existsSync(resolve(dir, ".github/dependabot.yml")) || !/package-ecosystem:\s*["']?npm/.test(read(".github/dependabot.yml")) ) findings.push( "No npm dependency-update path: shared releases cannot propagate.", ); // The merge path is the forge merge check (forge/verify), which runs the // repo's own `verify` on the merge result — no per-repo sweep file since // fleet#570. Without a verify script the forge cannot check a PR at all. if (!manifest.scripts?.verify) findings.push( "No checked merge path for shared dependency updates: package.json has no `verify` script for the forge merge check.", ); if (!existsSync(resolve(dir, ".github/workflows/deploy.yml"))) findings.push("No deployment path for merged shared dependency updates."); // The experience: a product that sets `experience.ceiling` in its plan may // not ship more files that stand where a package owns the behaviour // (bare text fields, its own chrome, composers, comments, article editors). // Without a ceiling it is only reported by experience-audit.mjs. findings.push(...overCeiling(measure(dir).counts, plan)); return findings; } if ( process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url) ) { const at = process.argv.indexOf("--dir"); const dir = resolve(at >= 0 ? process.argv[at + 1] : "."); const findings = auditComposition(dir); if (findings.length) { for (const finding of findings) process.stderr.write(finding + "\n"); process.exitCode = 1; } else process.stdout.write( "Shared building blocks are installed and wired; dependency updates are configured.\n", ); }