({
args,
workspacePath,
limit = 500,
}: {
args: string[]
workspacePath: string
limit?: number
})
| 10 | export type FileResult = { path: string; type: "file" | "folder"; label?: string } |
| 11 | |
| 12 | export async function executeRipgrep({ |
| 13 | args, |
| 14 | workspacePath, |
| 15 | limit = 500, |
| 16 | }: { |
| 17 | args: string[] |
| 18 | workspacePath: string |
| 19 | limit?: number |
| 20 | }): Promise<FileResult[]> { |
| 21 | const rgPath = await getBinPath(vscode.env.appRoot) |
| 22 | |
| 23 | if (!rgPath) { |
| 24 | throw new Error(`ripgrep not found: ${rgPath}`) |
| 25 | } |
| 26 | |
| 27 | return new Promise((resolve, reject) => { |
| 28 | const rgProcess = childProcess.spawn(rgPath, args) |
| 29 | const rl = readline.createInterface({ input: rgProcess.stdout, crlfDelay: Infinity }) |
| 30 | const fileResults: FileResult[] = [] |
| 31 | const dirSet = new Set<string>() // Track unique directory paths. |
| 32 | |
| 33 | let count = 0 |
| 34 | |
| 35 | rl.on("line", (line) => { |
| 36 | if (count < limit) { |
| 37 | try { |
| 38 | const relativePath = path.relative(workspacePath, line) |
| 39 | |
| 40 | // Add the file itself. |
| 41 | fileResults.push({ path: relativePath, type: "file", label: path.basename(relativePath) }) |
| 42 | |
| 43 | // Extract and store all parent directory paths. |
| 44 | let dirPath = path.dirname(relativePath) |
| 45 | |
| 46 | while (dirPath && dirPath !== "." && dirPath !== "/") { |
| 47 | dirSet.add(dirPath) |
| 48 | dirPath = path.dirname(dirPath) |
| 49 | } |
| 50 | |
| 51 | count++ |
| 52 | } catch (error) { |
| 53 | // Silently ignore errors processing individual paths. |
| 54 | } |
| 55 | } else { |
| 56 | rl.close() |
| 57 | rgProcess.kill() |
| 58 | } |
| 59 | }) |
| 60 | |
| 61 | let errorOutput = "" |
| 62 | |
| 63 | rgProcess.stderr.on("data", (data) => { |
| 64 | errorOutput += data.toString() |
| 65 | }) |
| 66 | |
| 67 | rl.on("close", () => { |
| 68 | if (errorOutput && fileResults.length === 0) { |
| 69 | reject(new Error(`ripgrep process error: ${errorOutput}`)) |
no test coverage detected