|
1 | 1 | import { mkdirSync } from 'fs' |
2 | 2 | import path from 'path' |
3 | 3 |
|
4 | | -import { rgPath as vscodeRgPath } from '@vscode/ripgrep' |
5 | 4 | import { spawnSync } from 'bun' |
| 5 | +import { getBundledRgPath } from '@codebuff/sdk' |
6 | 6 |
|
7 | 7 | import { CONFIG_DIR } from '../credentials' |
8 | 8 | import { logger } from '../utils/logger' |
9 | 9 |
|
10 | 10 | const getRipgrepPath = async (): Promise<string> => { |
11 | | - // In dev mode, use the vscode ripgrep binary |
| 11 | + let bundledRgPath: string |
| 12 | + try { |
| 13 | + bundledRgPath = getBundledRgPath(import.meta.url) |
| 14 | + } catch (error) { |
| 15 | + logger.error({ error }, 'Failed to resolve bundled ripgrep path') |
| 16 | + throw error |
| 17 | + } |
| 18 | + |
| 19 | + // In dev mode, use the bundled path directly |
12 | 20 | if (!process.env.IS_BINARY) { |
13 | | - return vscodeRgPath |
| 21 | + return bundledRgPath |
14 | 22 | } |
15 | 23 |
|
16 | | - // Compiled mode - self-extract the embedded binary |
| 24 | + // Compiled mode - stage the bundled binary in the config directory |
17 | 25 | const rgFileName = process.platform === 'win32' ? 'rg.exe' : 'rg' |
18 | 26 | const outPath = path.join(CONFIG_DIR, rgFileName) |
19 | 27 |
|
20 | | - // Check if already extracted |
21 | | - if (await Bun.file(outPath).exists()) { |
22 | | - return outPath |
23 | | - } |
24 | | - |
25 | | - // Extract the embedded binary |
26 | 28 | try { |
27 | | - // Use require() on a static string path to make sure rg is included in the compiled binary |
28 | | - const embeddedRgPath = |
29 | | - process.platform === 'win32' |
30 | | - ? require('../../../node_modules/@vscode/ripgrep/bin/rg.exe') |
31 | | - : require('../../../node_modules/@vscode/ripgrep/bin/rg') |
| 29 | + if (await Bun.file(outPath).exists()) { |
| 30 | + return outPath |
| 31 | + } |
32 | 32 |
|
33 | | - // Create cache directory |
34 | 33 | mkdirSync(path.dirname(outPath), { recursive: true }) |
| 34 | + await Bun.write(outPath, await Bun.file(bundledRgPath).arrayBuffer()) |
35 | 35 |
|
36 | | - // Copy embedded binary to cache location |
37 | | - await Bun.write(outPath, await Bun.file(embeddedRgPath).arrayBuffer()) |
38 | | - |
39 | | - // Make executable on Unix systems |
40 | 36 | if (process.platform !== 'win32') { |
41 | 37 | spawnSync(['chmod', '+x', outPath]) |
42 | 38 | } |
43 | 39 |
|
44 | 40 | return outPath |
45 | 41 | } catch (error) { |
46 | | - logger.error({ error }, 'Failed to extract ripgrep binary') |
47 | | - // Fallback to vscode ripgrep if extraction fails |
48 | | - return vscodeRgPath |
| 42 | + logger.error( |
| 43 | + { error }, |
| 44 | + 'Failed to stage bundled ripgrep binary, using fallback path', |
| 45 | + ) |
| 46 | + return bundledRgPath |
49 | 47 | } |
50 | 48 | } |
51 | 49 |
|
|
0 commit comments