| 51 | * Node.js implementation of FileSystem with gitignore support |
| 52 | */ |
| 53 | export class NodeFileSystem implements FileSystem { |
| 54 | private customIgnoreFilter: ReturnType<typeof ignore>; |
| 55 | private ignoreCache = new Map<string, ReturnType<typeof ignore>>(); |
| 56 | |
| 57 | constructor( |
| 58 | private git: Git, |
| 59 | options: FileSystemOptions, |
| 60 | ) { |
| 61 | this.customIgnoreFilter = ignore(); |
| 62 | this.customIgnoreFilter.add(options.ignorePatterns); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Checks if a file is a hidden file (starts with .) |
| 67 | */ |
| 68 | private isHiddenFile(filePath: string, root: string): boolean { |
| 69 | const relativePath = path.relative(root, filePath); |
| 70 | const parts = relativePath.split(path.sep); |
| 71 | return parts.some( |
| 72 | (part) => part.startsWith(".") && part !== "." && part !== "..", |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets all files recursively from a directory |
| 78 | */ |
| 79 | private *getAllFilesRecursive(dir: string, root: string): Generator<string> { |
| 80 | try { |
| 81 | const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 82 | for (const entry of entries) { |
| 83 | const fullPath = path.join(dir, entry.name); |
| 84 | |
| 85 | if (this.isHiddenFile(fullPath, root)) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (this.isIgnored(fullPath, root)) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if (entry.isDirectory()) { |
| 94 | yield* this.getAllFilesRecursive(fullPath, root); |
| 95 | } else if (entry.isFile()) { |
| 96 | yield fullPath; |
| 97 | } |
| 98 | } |
| 99 | } catch (error) { |
| 100 | // Log permission or other filesystem errors |
| 101 | console.error(`Warning: Failed to read directory ${dir}:`, error); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | *getFiles(dirRoot: string): Generator<string> { |
| 106 | // Preload root .mgrepignore to ensure it's cached |
| 107 | this.getDirectoryIgnoreFilter(dirRoot); |
| 108 | |
| 109 | if (this.git.isGitRepository(dirRoot)) { |
| 110 | yield* this.git.getGitFiles(dirRoot); |
nothing calls this directly
no outgoing calls
no test coverage detected