verify-package-paths.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Find stale root-relative `packages/...` references in repo-authored prose and
  3. * TypeScript. A missing path is reported only when it names a real package leaf;
  4. * globs, placeholders, hypothetical packages, and unbuilt `lib/` output are
  5. * outside the check.
  6. */
  7. import { existsSync, globSync } from 'node:fs'
  8. import { resolve } from 'node:path'
  9. import {
  10. findReferenceViolations,
  11. isArchivedAgentNotePath,
  12. uniqueRepoFiles,
  13. type ReferenceViolation as Violation,
  14. } from './repo-files.ts'
  15. const root = resolve(import.meta.dirname, '..')
  16. /** Markdown + repo-authored TypeScript that may cite package paths. */
  17. const PATTERNS = [
  18. 'README.md',
  19. '.agents/notes/**/*.md',
  20. 'docs/**/*.md',
  21. 'packages/*/*.md',
  22. 'packages/*/*/*.md',
  23. 'AGENTS.md',
  24. 'packages/AGENTS.md',
  25. 'packages/**/*.ts',
  26. 'examples/**/*.ts',
  27. ]
  28. /** Paths excluded from the scan: built output and vendored upstream source. */
  29. const isExcluded = (p: string): boolean =>
  30. isArchivedAgentNotePath(p) || p.includes('/lib/') || p.endsWith('.d.ts') || p.startsWith('vendor/')
  31. /**
  32. * Directory names of every real package, `packages/<group>/<pkg>`. A broken
  33. * reference is only flagged when one of its segments is in this set — that is
  34. * what scopes the gate to DRIFT (a moved real package) rather than typos or
  35. * not-yet-existing packages named in a proposal.
  36. */
  37. function realPackageNames(): Set<string> {
  38. const names = new Set<string>()
  39. for (const pkg of globSync('packages/*/*', { cwd: root, withFileTypes: true })) {
  40. if (pkg.isDirectory()) names.add(pkg.name)
  41. }
  42. return names
  43. }
  44. const packageNames = realPackageNames()
  45. /**
  46. * Match a `packages/<path>` reference token. The character class is plain path
  47. * characters only, so a glob (`*`), placeholder (`<`, `>`), or brace expansion
  48. * (`{`, `}`, `,`) terminates the match before those chars and is never probed —
  49. * those are patterns, not real paths. A trailing `.`/`/` (e.g. a sentence-ending
  50. * period) is trimmed before the existence check.
  51. */
  52. const PKG_REF = /\bpackages\/[A-Za-z0-9._/-]+/g
  53. function isDriftedPackageReference(ref: string): boolean {
  54. if (existsSync(resolve(root, ref))) return false
  55. // Ignore unbuilt `lib/` paths only under an existing depth-two package root:
  56. // CI runs this gate before build, while stale group-less paths must still fail.
  57. const parts = ref.split('/')
  58. const libAt = parts.indexOf('lib')
  59. if (libAt === 3 && existsSync(resolve(root, parts.slice(0, 3).join('/')))) return false
  60. // A missing reference is drift only when a path segment names a live package.
  61. return ref.split('/').slice(1).some(segment => packageNames.has(segment))
  62. }
  63. /** Find missing package references whose path names a live package; bare paths, typos, and illustrative skeletons do not count. */
  64. function findViolations(absPath: string): Violation[] {
  65. return findReferenceViolations(
  66. root,
  67. absPath,
  68. PKG_REF,
  69. // Remove trailing separators or sentence punctuation matched greedily.
  70. ref => ref.replace(/[./]+$/, ''),
  71. isDriftedPackageReference,
  72. )
  73. }
  74. const files = uniqueRepoFiles(root, PATTERNS, isExcluded)
  75. const all = files.flatMap(file => findViolations(file.real))
  76. const checked = files.length
  77. if (all.length === 0) {
  78. console.log(`verify-package-paths: ${checked} file(s) checked, all packages/* references resolve.`)
  79. process.exit(0)
  80. }
  81. console.error('verify-package-paths: broken packages/* references found (target does not exist):')
  82. for (const v of all) {
  83. console.error(` ${v.file}:${v.line} ${v.ref}`)
  84. }
  85. process.exit(1)