MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / execRipgrep

Function execRipgrep

src/services/ripgrep/index.ts:123–161  ·  view source on GitHub ↗
(bin: string, args: string[])

Source from the content-addressed store, hash-verified

121}
122
123async function execRipgrep(bin: string, args: string[]): Promise<string> {
124 return new Promise((resolve, reject) => {
125 const rgProcess = childProcess.spawn(bin, args)
126 // cross-platform alternative to head, which is ripgrep author's recommendation for limiting output.
127 const rl = readline.createInterface({
128 input: rgProcess.stdout,
129 crlfDelay: Infinity, // treat \r\n as a single line break even if it's split across chunks. This ensures consistent behavior across different operating systems.
130 })
131
132 let output = ""
133 let lineCount = 0
134 const maxLines = MAX_RESULTS * 5 // limiting ripgrep output with max lines since there's no other way to limit results. it's okay that we're outputting as json, since we're parsing it line by line and ignore anything that's not part of a match. This assumes each result is at most 5 lines.
135
136 rl.on("line", (line) => {
137 if (lineCount < maxLines) {
138 output += line + "\n"
139 lineCount++
140 } else {
141 rl.close()
142 rgProcess.kill()
143 }
144 })
145
146 let errorOutput = ""
147 rgProcess.stderr.on("data", (data) => {
148 errorOutput += data.toString()
149 })
150 rl.on("close", () => {
151 if (errorOutput) {
152 reject(new Error(`ripgrep process error: ${errorOutput}`))
153 } else {
154 resolve(output)
155 }
156 })
157 rgProcess.on("error", (error) => {
158 reject(new Error(`ripgrep process error: ${error.message}`))
159 })
160 })
161}
162
163export async function regexSearchFiles(
164 cwd: string,

Callers 1

regexSearchFilesFunction · 0.70

Calls 3

onMethod · 0.65
closeMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected