MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / getRipgrepPath

Function getRipgrepPath

cli/src/native/ripgrep.ts:9–62  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7import { logger } from '../utils/logger'
8
9const getRipgrepPath = async (): Promise<string> => {
10 const env = getCliEnv()
11 // In dev mode, use the SDK's bundled ripgrep binary
12 if (!env.CODEBUFF_IS_BINARY) {
13 return getBundledRgPath()
14 }
15
16 // Compiled mode - self-extract the embedded binary to the same directory as the current binary
17 const binaryDir = path.dirname(process.execPath)
18 const rgFileName = process.platform === 'win32' ? 'rg.exe' : 'rg'
19 const outPath = path.join(binaryDir, rgFileName)
20
21 // Check if already extracted
22 const outPathExists = await Bun.file(outPath).exists()
23 if (outPathExists) {
24 return outPath
25 }
26
27 // Extract the embedded binary
28 try {
29 // Use require() with literal paths to ensure the binary gets bundled into the compiled CLI
30 // This is necessary for Bun's binary compilation to include the ripgrep binary
31 let embeddedRgPath: string
32
33 if (process.platform === 'darwin' && process.arch === 'arm64') {
34 embeddedRgPath = require('../../../sdk/dist/vendor/ripgrep/arm64-darwin/rg')
35 } else if (process.platform === 'darwin' && process.arch === 'x64') {
36 embeddedRgPath = require('../../../sdk/dist/vendor/ripgrep/x64-darwin/rg')
37 } else if (process.platform === 'linux' && process.arch === 'arm64') {
38 embeddedRgPath = require('../../../sdk/dist/vendor/ripgrep/arm64-linux/rg')
39 } else if (process.platform === 'linux' && process.arch === 'x64') {
40 embeddedRgPath = require('../../../sdk/dist/vendor/ripgrep/x64-linux/rg')
41 } else if (process.platform === 'win32' && process.arch === 'x64') {
42 embeddedRgPath = require('../../../sdk/dist/vendor/ripgrep/x64-win32/rg.exe')
43 } else {
44 throw new Error(`Unsupported platform: ${process.platform}-${process.arch}`)
45 }
46
47 // Copy SDK's bundled binary to binary directory for portability
48 const embeddedBuffer = await Bun.file(embeddedRgPath).arrayBuffer()
49 await Bun.write(outPath, embeddedBuffer)
50
51 // Make executable on Unix systems
52 if (process.platform !== 'win32') {
53 spawnSync(['chmod', '+x', outPath])
54 }
55
56 return outPath
57 } catch (error) {
58 logger.error({ error }, 'Failed to extract ripgrep binary')
59 // Fallback to SDK's bundled ripgrep if extraction fails
60 return getBundledRgPath()
61 }
62}
63
64// Cache the promise to avoid multiple extractions
65let rgPathPromise: Promise<string> | null = null

Callers 1

getRgPathFunction · 0.85

Calls 2

getCliEnvFunction · 0.90
getBundledRgPathFunction · 0.90

Tested by

no test coverage detected