(params: {
projectRoot: string
maxFiles?: number
fs: CodebuffFileSystem
})
| 41 | export const DEFAULT_MAX_FILES = 10_000 |
| 42 | |
| 43 | export async function getProjectFileTree(params: { |
| 44 | projectRoot: string |
| 45 | maxFiles?: number |
| 46 | fs: CodebuffFileSystem |
| 47 | }): Promise<FileTreeNode[]> { |
| 48 | const withDefaults = { maxFiles: DEFAULT_MAX_FILES, ...params } |
| 49 | const { projectRoot, fs } = withDefaults |
| 50 | let { maxFiles } = withDefaults |
| 51 | |
| 52 | const _start = Date.now() |
| 53 | const defaultIgnore = ignore.default() |
| 54 | for (const pattern of DEFAULT_IGNORED_PATHS) { |
| 55 | defaultIgnore.add(pattern) |
| 56 | } |
| 57 | |
| 58 | if (!isValidProjectRoot(projectRoot)) { |
| 59 | defaultIgnore.add('.*') |
| 60 | maxFiles = 0 |
| 61 | } |
| 62 | |
| 63 | const root: DirectoryNode = { |
| 64 | name: path.basename(projectRoot), |
| 65 | type: 'directory', |
| 66 | children: [], |
| 67 | filePath: '', |
| 68 | } |
| 69 | const queue: { |
| 70 | node: DirectoryNode |
| 71 | fullPath: string |
| 72 | ignore: ignore.Ignore |
| 73 | }[] = [ |
| 74 | { |
| 75 | node: root, |
| 76 | fullPath: projectRoot, |
| 77 | ignore: defaultIgnore, |
| 78 | }, |
| 79 | ] |
| 80 | let totalFiles = 0 |
| 81 | |
| 82 | while (queue.length > 0 && totalFiles < maxFiles) { |
| 83 | const { node, fullPath, ignore: currentIgnore } = queue.shift()! |
| 84 | const parsedIgnore = await parseGitignore({ |
| 85 | fullDirPath: fullPath, |
| 86 | projectRoot, |
| 87 | fs, |
| 88 | }) |
| 89 | const mergedIgnore = ignore |
| 90 | .default() |
| 91 | .add(currentIgnore) |
| 92 | .add(parsedIgnore) |
| 93 | |
| 94 | try { |
| 95 | const files = await fs.readdir(fullPath) |
| 96 | for (const file of files) { |
| 97 | if (totalFiles >= maxFiles) break |
| 98 | |
| 99 | const filePath = path.join(fullPath, file) |
| 100 | const relativeFilePath = path.relative(projectRoot, filePath) |
no test coverage detected