|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import * as fs from 'fs/promises'; |
|
|
import * as path from 'path'; |
|
|
import { Dirent } from 'fs'; |
|
|
import { FileDiscoveryService } from '../services/fileDiscoveryService.js'; |
|
|
|
|
|
|
|
|
|
|
|
const logger = { |
|
|
|
|
|
debug: (...args: any[]) => console.debug('[DEBUG] [BfsFileSearch]', ...args), |
|
|
}; |
|
|
|
|
|
interface BfsFileSearchOptions { |
|
|
fileName: string; |
|
|
ignoreDirs?: string[]; |
|
|
maxDirs?: number; |
|
|
debug?: boolean; |
|
|
fileService?: FileDiscoveryService; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function bfsFileSearch( |
|
|
rootDir: string, |
|
|
options: BfsFileSearchOptions, |
|
|
): Promise<string[]> { |
|
|
const { |
|
|
fileName, |
|
|
ignoreDirs = [], |
|
|
maxDirs = Infinity, |
|
|
debug = false, |
|
|
fileService, |
|
|
} = options; |
|
|
const foundFiles: string[] = []; |
|
|
const queue: string[] = [rootDir]; |
|
|
const visited = new Set<string>(); |
|
|
let scannedDirCount = 0; |
|
|
|
|
|
while (queue.length > 0 && scannedDirCount < maxDirs) { |
|
|
const currentDir = queue.shift()!; |
|
|
if (visited.has(currentDir)) { |
|
|
continue; |
|
|
} |
|
|
visited.add(currentDir); |
|
|
scannedDirCount++; |
|
|
|
|
|
if (debug) { |
|
|
logger.debug(`Scanning [${scannedDirCount}/${maxDirs}]: ${currentDir}`); |
|
|
} |
|
|
|
|
|
let entries: Dirent[]; |
|
|
try { |
|
|
entries = await fs.readdir(currentDir, { withFileTypes: true }); |
|
|
} catch { |
|
|
|
|
|
continue; |
|
|
} |
|
|
|
|
|
for (const entry of entries) { |
|
|
const fullPath = path.join(currentDir, entry.name); |
|
|
if (fileService?.shouldGitIgnoreFile(fullPath)) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
if (entry.isDirectory()) { |
|
|
if (!ignoreDirs.includes(entry.name)) { |
|
|
queue.push(fullPath); |
|
|
} |
|
|
} else if (entry.isFile() && entry.name === fileName) { |
|
|
foundFiles.push(fullPath); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return foundFiles; |
|
|
} |
|
|
|