verify-package-paths.ts 3.6 KB

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