verify-package-paths.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * outside its own explaining group directory; globs, placeholders, hypothetical
  5. * packages, and unbuilt `lib/` output are 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. // A leading segment that is itself an existing group directory is explained by
  62. // the group, not by a relocated leaf sharing its name (`client` is both the
  63. // client-modules group and the sdk leaf), so only later segments count.
  64. const segments = ref.split('/').slice(1)
  65. const [group] = segments
  66. const scanned = group !== undefined && segments.length > 1 && existsSync(resolve(root, 'packages', group))
  67. ? segments.slice(1)
  68. : segments
  69. return scanned.some(segment => packageNames.has(segment))
  70. }
  71. /** Find missing package references whose path names a live package; bare paths, typos, and illustrative skeletons do not count. */
  72. function findViolations(absPath: string): Violation[] {
  73. return findReferenceViolations(
  74. root,
  75. absPath,
  76. PKG_REF,
  77. // Remove trailing separators or sentence punctuation matched greedily.
  78. ref => ref.replace(/[./]+$/, ''),
  79. isDriftedPackageReference,
  80. )
  81. }
  82. const files = uniqueRepoFiles(root, PATTERNS, isExcluded)
  83. const all = files.flatMap(file => findViolations(file.real))
  84. const checked = files.length
  85. if (all.length === 0) {
  86. console.log(`verify-package-paths: ${checked} file(s) checked, all packages/* references resolve.`)
  87. process.exit(0)
  88. }
  89. console.error('verify-package-paths: broken packages/* references found (target does not exist):')
  90. for (const v of all) {
  91. console.error(` ${v.file}:${v.line} ${v.ref}`)
  92. }
  93. process.exit(1)