( importMetaUrl?: string, env: SdkEnv = getSdkEnv(), )
| 12 | * @returns Path to the ripgrep binary |
| 13 | */ |
| 14 | export function getBundledRgPath( |
| 15 | importMetaUrl?: string, |
| 16 | env: SdkEnv = getSdkEnv(), |
| 17 | ): string { |
| 18 | // Allow override via environment variable |
| 19 | if (env.CODEBUFF_RG_PATH) { |
| 20 | return env.CODEBUFF_RG_PATH |
| 21 | } |
| 22 | |
| 23 | // Determine platform-specific directory name |
| 24 | const platform = process.platform |
| 25 | const arch = process.arch |
| 26 | |
| 27 | let platformDir: string |
| 28 | if (platform === 'win32' && arch === 'x64') { |
| 29 | platformDir = 'x64-win32' |
| 30 | } else if (platform === 'darwin' && arch === 'arm64') { |
| 31 | platformDir = 'arm64-darwin' |
| 32 | } else if (platform === 'darwin' && arch === 'x64') { |
| 33 | platformDir = 'x64-darwin' |
| 34 | } else if (platform === 'linux' && arch === 'arm64') { |
| 35 | platformDir = 'arm64-linux' |
| 36 | } else if (platform === 'linux' && arch === 'x64') { |
| 37 | platformDir = 'x64-linux' |
| 38 | } else { |
| 39 | throw new Error(`Unsupported platform: ${platform}-${arch}`) |
| 40 | } |
| 41 | |
| 42 | const binaryName = platform === 'win32' ? 'rg.exe' : 'rg' |
| 43 | |
| 44 | // Try to find the bundled binary relative to this module |
| 45 | let vendorPath: string | undefined |
| 46 | |
| 47 | // Use the SDK's own import.meta.url if none is provided |
| 48 | const metaUrl = importMetaUrl || import.meta.url |
| 49 | |
| 50 | if (metaUrl) { |
| 51 | // ESM context - use import.meta.url to find relative path |
| 52 | const currentFile = fileURLToPath(metaUrl) |
| 53 | const currentDir = dirname(currentFile) |
| 54 | |
| 55 | // Try relative to current file (development - from src/native/ripgrep.ts to vendor/) |
| 56 | const devPath = join( |
| 57 | currentDir, |
| 58 | '..', |
| 59 | '..', |
| 60 | 'vendor', |
| 61 | 'ripgrep', |
| 62 | platformDir, |
| 63 | binaryName, |
| 64 | ) |
| 65 | if (existsSync(devPath)) { |
| 66 | vendorPath = devPath |
| 67 | } |
| 68 | |
| 69 | // Try relative to bundled dist file (production - from dist/index.mjs to dist/vendor/) |
| 70 | const distPath = join( |
| 71 | currentDir, |
no test coverage detected