repo-files.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /** Shared repository file discovery and line-oriented reference scanning. */
  2. import { globSync, readFileSync, realpathSync } from 'node:fs'
  3. import { relative, resolve, sep } from 'node:path'
  4. /** One authored path plus its canonical target for symlink deduplication. */
  5. export interface RepoFile {
  6. /** Absolute path matched by the caller's glob. */
  7. abs: string
  8. /** Absolute canonical path used only for deduplication. */
  9. real: string
  10. }
  11. /** A rejected line-oriented repository reference. */
  12. export interface ReferenceViolation {
  13. /** Repo-relative file containing the reference. */
  14. file: string
  15. /** 1-based line containing the reference. */
  16. line: number
  17. /** Normalized reference text. */
  18. ref: string
  19. }
  20. /**
  21. * Expand repository-relative globs and deduplicate symlinked files.
  22. * @param root - absolute repository root.
  23. * @param patterns - repository-relative glob patterns, processed in order.
  24. * @param isExcluded - optional predicate over each matched relative path.
  25. * @returns matched files in stable first-seen order.
  26. */
  27. export function uniqueRepoFiles(
  28. root: string,
  29. patterns: readonly string[],
  30. isExcluded: (relativePath: string) => boolean = () => false,
  31. ): RepoFile[] {
  32. const seen = new Set<string>()
  33. const files: RepoFile[] = []
  34. for (const pattern of patterns) {
  35. for (const match of globSync(pattern, { cwd: root })) {
  36. const repoPath = match.split(sep).join('/')
  37. if (isExcluded(repoPath)) continue
  38. const abs = resolve(root, repoPath)
  39. const real = realpathSync(abs)
  40. if (seen.has(real)) continue
  41. seen.add(real)
  42. files.push({ abs, real })
  43. }
  44. }
  45. return files
  46. }
  47. /**
  48. * Scan regex matches line by line and return the normalized matches rejected by
  49. * a caller predicate.
  50. * @param root - absolute repository root used for violation paths.
  51. * @param absPath - absolute text-file path to scan.
  52. * @param pattern - global regex matched independently against each line.
  53. * @param normalize - maps raw regex text to the reference the gate evaluates.
  54. * @param isViolation - returns true when the normalized reference is invalid.
  55. * @returns every rejected reference in source order.
  56. */
  57. export function findReferenceViolations(
  58. root: string,
  59. absPath: string,
  60. pattern: RegExp,
  61. normalize: (raw: string) => string,
  62. isViolation: (ref: string) => boolean,
  63. ): ReferenceViolation[] {
  64. const file = relative(root, absPath).split(sep).join('/')
  65. const out: ReferenceViolation[] = []
  66. const lines = readFileSync(absPath, 'utf8').split('\n')
  67. for (let i = 0; i < lines.length; i++) {
  68. const line = lines[i]
  69. if (line === undefined) continue
  70. for (const match of line.matchAll(pattern)) {
  71. const ref = normalize(match[0])
  72. if (isViolation(ref)) out.push({ file, line: i + 1, ref })
  73. }
  74. }
  75. return out
  76. }