|
| 1 | +import { spawn } from "bun" |
| 2 | +import { |
| 3 | + resolveGrepCli, |
| 4 | + DEFAULT_TIMEOUT_MS, |
| 5 | + DEFAULT_LIMIT, |
| 6 | + DEFAULT_MAX_DEPTH, |
| 7 | + DEFAULT_MAX_OUTPUT_BYTES, |
| 8 | + RG_FILES_FLAGS, |
| 9 | +} from "./constants" |
| 10 | +import type { GlobOptions, GlobResult, FileMatch } from "./types" |
| 11 | +import { stat } from "node:fs/promises" |
| 12 | + |
| 13 | +function buildRgArgs(options: GlobOptions): string[] { |
| 14 | + const args: string[] = [ |
| 15 | + ...RG_FILES_FLAGS, |
| 16 | + `--max-depth=${Math.min(options.maxDepth ?? DEFAULT_MAX_DEPTH, DEFAULT_MAX_DEPTH)}`, |
| 17 | + ] |
| 18 | + |
| 19 | + if (options.hidden) args.push("--hidden") |
| 20 | + if (options.noIgnore) args.push("--no-ignore") |
| 21 | + |
| 22 | + args.push(`--glob=${options.pattern}`) |
| 23 | + |
| 24 | + return args |
| 25 | +} |
| 26 | + |
| 27 | +function buildFindArgs(options: GlobOptions): string[] { |
| 28 | + const args: string[] = ["."] |
| 29 | + |
| 30 | + const maxDepth = Math.min(options.maxDepth ?? DEFAULT_MAX_DEPTH, DEFAULT_MAX_DEPTH) |
| 31 | + args.push("-maxdepth", String(maxDepth)) |
| 32 | + |
| 33 | + args.push("-type", "f") |
| 34 | + args.push("-name", options.pattern) |
| 35 | + |
| 36 | + if (!options.hidden) { |
| 37 | + args.push("-not", "-path", "*/.*") |
| 38 | + } |
| 39 | + |
| 40 | + return args |
| 41 | +} |
| 42 | + |
| 43 | +async function getFileMtime(filePath: string): Promise<number> { |
| 44 | + try { |
| 45 | + const stats = await stat(filePath) |
| 46 | + return stats.mtime.getTime() |
| 47 | + } catch { |
| 48 | + return 0 |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export async function runRgFiles(options: GlobOptions): Promise<GlobResult> { |
| 53 | + const cli = resolveGrepCli() |
| 54 | + const timeout = Math.min(options.timeout ?? DEFAULT_TIMEOUT_MS, DEFAULT_TIMEOUT_MS) |
| 55 | + const limit = Math.min(options.limit ?? DEFAULT_LIMIT, DEFAULT_LIMIT) |
| 56 | + |
| 57 | + const isRg = cli.backend === "rg" |
| 58 | + const args = isRg ? buildRgArgs(options) : buildFindArgs(options) |
| 59 | + |
| 60 | + const paths = options.paths?.length ? options.paths : ["."] |
| 61 | + if (isRg) { |
| 62 | + args.push(...paths) |
| 63 | + } |
| 64 | + |
| 65 | + const cwd = paths[0] || "." |
| 66 | + |
| 67 | + const proc = spawn([cli.path, ...args], { |
| 68 | + stdout: "pipe", |
| 69 | + stderr: "pipe", |
| 70 | + cwd: isRg ? undefined : cwd, |
| 71 | + }) |
| 72 | + |
| 73 | + const timeoutPromise = new Promise<never>((_, reject) => { |
| 74 | + const id = setTimeout(() => { |
| 75 | + proc.kill() |
| 76 | + reject(new Error(`Glob search timeout after ${timeout}ms`)) |
| 77 | + }, timeout) |
| 78 | + proc.exited.then(() => clearTimeout(id)) |
| 79 | + }) |
| 80 | + |
| 81 | + try { |
| 82 | + const stdout = await Promise.race([new Response(proc.stdout).text(), timeoutPromise]) |
| 83 | + const stderr = await new Response(proc.stderr).text() |
| 84 | + const exitCode = await proc.exited |
| 85 | + |
| 86 | + if (exitCode > 1 && stderr.trim()) { |
| 87 | + return { |
| 88 | + files: [], |
| 89 | + totalFiles: 0, |
| 90 | + truncated: false, |
| 91 | + error: stderr.trim(), |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const truncatedOutput = stdout.length >= DEFAULT_MAX_OUTPUT_BYTES |
| 96 | + const outputToProcess = truncatedOutput ? stdout.substring(0, DEFAULT_MAX_OUTPUT_BYTES) : stdout |
| 97 | + |
| 98 | + const lines = outputToProcess.trim().split("\n").filter(Boolean) |
| 99 | + |
| 100 | + const files: FileMatch[] = [] |
| 101 | + let truncated = false |
| 102 | + |
| 103 | + for (const line of lines) { |
| 104 | + if (files.length >= limit) { |
| 105 | + truncated = true |
| 106 | + break |
| 107 | + } |
| 108 | + |
| 109 | + const filePath = isRg ? line : `${cwd}/${line}` |
| 110 | + const mtime = await getFileMtime(filePath) |
| 111 | + files.push({ path: filePath, mtime }) |
| 112 | + } |
| 113 | + |
| 114 | + files.sort((a, b) => b.mtime - a.mtime) |
| 115 | + |
| 116 | + return { |
| 117 | + files, |
| 118 | + totalFiles: files.length, |
| 119 | + truncated: truncated || truncatedOutput, |
| 120 | + } |
| 121 | + } catch (e) { |
| 122 | + return { |
| 123 | + files: [], |
| 124 | + totalFiles: 0, |
| 125 | + truncated: false, |
| 126 | + error: e instanceof Error ? e.message : String(e), |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments