MCPcopy Create free account
hub / github.com/Noumena-Network/code / ripGrepFileCount

Function ripGrepFileCount

src/utils/ripgrep.ts:238–273  ·  view source on GitHub ↗

* Stream-count lines from `rg --files` without buffering stdout. * * On large repos (e.g. 247k files, 16MB of paths), calling `ripGrep()` just * to read `.length` materializes the full stdout string plus a 247k-element * array. This counts newline bytes per chunk instead; peak memory is one * s

(
  args: string[],
  target: string,
  abortSignal: AbortSignal,
)

Source from the content-addressed store, hash-verified

236 * timeout (callers pass AbortSignal.timeout; spawn's signal option kills rg).
237 */
238async function ripGrepFileCount(
239 args: string[],
240 target: string,
241 abortSignal: AbortSignal,
242): Promise<number> {
243 await codesignRipgrepIfNecessary()
244 const { rgPath, rgArgs, argv0 } = ripgrepCommand()
245
246 return new Promise<number>((resolve, reject) => {
247 const child = spawn(rgPath, [...rgArgs, ...args, target], {
248 argv0,
249 signal: abortSignal,
250 windowsHide: true,
251 stdio: ['ignore', 'pipe', 'ignore'],
252 })
253
254 let lines = 0
255 child.stdout?.on('data', (chunk: Buffer) => {
256 lines += countCharInString(chunk, '\n')
257 })
258
259 // On Windows, both 'close' and 'error' can fire for the same process.
260 let settled = false
261 child.on('close', code => {
262 if (settled) return
263 settled = true
264 if (code === 0 || code === 1) resolve(lines)
265 else reject(new Error(`rg --files exited ${code}`))
266 })
267 child.on('error', err => {
268 if (settled) return
269 settled = true
270 reject(err)
271 })
272 })
273}
274
275/**
276 * Stream lines from ripgrep as they arrive, calling `onLines` per stdout chunk.

Callers 1

ripgrep.tsFile · 0.85

Calls 5

ripgrepCommandFunction · 0.85
spawnFunction · 0.85
countCharInStringFunction · 0.85
resolveFunction · 0.70

Tested by

no test coverage detected