(params: {
pattern: string
projectPath: string
cwd?: string
fs: CodebuffFileSystem
})
| 8 | import type { CodebuffFileSystem } from '@codebuff/common/types/filesystem' |
| 9 | |
| 10 | export async function glob(params: { |
| 11 | pattern: string |
| 12 | projectPath: string |
| 13 | cwd?: string |
| 14 | fs: CodebuffFileSystem |
| 15 | }): Promise<CodebuffToolOutput<'glob'>> { |
| 16 | const { pattern, projectPath, cwd, fs } = params |
| 17 | |
| 18 | try { |
| 19 | const fileTree = await getProjectFileTree({ projectRoot: projectPath, fs }) |
| 20 | const flattenedNodes = flattenTree(fileTree) |
| 21 | let allFilePaths = flattenedNodes |
| 22 | .filter((node) => node.type === 'file') |
| 23 | .map((node) => node.filePath) |
| 24 | |
| 25 | if (cwd) { |
| 26 | const cwdPrefix = cwd.endsWith('/') ? cwd : `${cwd}/` |
| 27 | allFilePaths = allFilePaths.filter( |
| 28 | (filePath) => |
| 29 | filePath === cwd || |
| 30 | filePath.startsWith(cwdPrefix) || |
| 31 | filePath === cwd.replace(/\/$/, ''), |
| 32 | ) |
| 33 | } |
| 34 | |
| 35 | const matchingFiles = micromatch(allFilePaths, pattern) |
| 36 | |
| 37 | return [ |
| 38 | { |
| 39 | type: 'json', |
| 40 | value: { |
| 41 | files: matchingFiles, |
| 42 | count: matchingFiles.length, |
| 43 | message: `Found ${matchingFiles.length} file(s) matching pattern "${pattern}"${cwd ? ` in directory "${cwd}"` : ''}`, |
| 44 | }, |
| 45 | }, |
| 46 | ] |
| 47 | } catch (error) { |
| 48 | const errorMessage = error instanceof Error ? error.message : String(error) |
| 49 | return [ |
| 50 | { |
| 51 | type: 'json', |
| 52 | value: { |
| 53 | errorMessage: `Failed to search for files: ${errorMessage}`, |
| 54 | }, |
| 55 | }, |
| 56 | ] |
| 57 | } |
| 58 | } |
no test coverage detected