()
| 307 | // --- Main --- |
| 308 | |
| 309 | async function downloadAndExtract() { |
| 310 | const { target, ext } = getPlatformMapping() |
| 311 | const assetName = `ripgrep-v${RG_VERSION}-${target}.${ext}` |
| 312 | |
| 313 | const binaryPath = getBinaryPath() |
| 314 | const binaryDir = path.dirname(binaryPath) |
| 315 | |
| 316 | const force = process.argv.includes('--force') |
| 317 | if (!force && existsSync(binaryPath)) { |
| 318 | const stat = statSync(binaryPath) |
| 319 | if (stat.size > 0) { |
| 320 | console.log(`[ripgrep] Binary already exists at ${binaryPath}, skipping.`) |
| 321 | return |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | console.log(`[ripgrep] Downloading v${RG_VERSION} for ${target}...`) |
| 326 | |
| 327 | const extractedBinary = process.platform === 'win32' ? 'rg.exe' : 'rg' |
| 328 | |
| 329 | const mirrors = [RELEASE_BASE] |
| 330 | if (RELEASE_BASE === DEFAULT_RELEASE_BASE.replace(/\/$/, '')) { |
| 331 | mirrors.push(MIRROR_RELEASE_BASE.replace(/\/$/, '')) |
| 332 | } |
| 333 | |
| 334 | let buffer |
| 335 | let lastError |
| 336 | for (const base of mirrors) { |
| 337 | const url = `${base}/${assetName}` |
| 338 | try { |
| 339 | console.log(`[ripgrep] Trying ${url}`) |
| 340 | buffer = await downloadUrlToBufferWithFallback(url) |
| 341 | break |
| 342 | } catch (e) { |
| 343 | console.warn( |
| 344 | `[ripgrep] Download from ${base} failed: ${e instanceof Error ? e.message : e}`, |
| 345 | ) |
| 346 | lastError = e |
| 347 | } |
| 348 | } |
| 349 | if (!buffer) { |
| 350 | throw lastError |
| 351 | } |
| 352 | |
| 353 | try { |
| 354 | console.log(`[ripgrep] Downloaded ${Math.round(buffer.length / 1024)} KB`) |
| 355 | |
| 356 | mkdirSync(binaryDir, { recursive: true }) |
| 357 | |
| 358 | if (ext === 'tar.gz') { |
| 359 | await extractTarGz(buffer, binaryPath, extractedBinary, assetName) |
| 360 | } else { |
| 361 | await extractZip(buffer, binaryPath, extractedBinary) |
| 362 | } |
| 363 | |
| 364 | if (process.platform !== 'win32') { |
| 365 | chmodSync(binaryPath, 0o755) |
| 366 | } |
no test coverage detected