|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { mkdir, writeFile } from 'fs/promises' |
| 4 | +import { existsSync } from 'fs' |
| 5 | +import { join } from 'path' |
| 6 | +import AdmZip from 'adm-zip' |
| 7 | +import fetch from 'node-fetch' |
| 8 | + |
| 9 | +// Ripgrep version to download |
| 10 | +const RIPGREP_VERSION = '14.1.1' |
| 11 | + |
| 12 | +// Platform configurations |
| 13 | +const PLATFORMS = [ |
| 14 | + { |
| 15 | + name: 'x64-win32', |
| 16 | + url: `https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}/ripgrep-${RIPGREP_VERSION}-x86_64-pc-windows-msvc.zip`, |
| 17 | + executable: 'rg.exe', |
| 18 | + archivePath: `ripgrep-${RIPGREP_VERSION}-x86_64-pc-windows-msvc/rg.exe` |
| 19 | + }, |
| 20 | + { |
| 21 | + name: 'x64-darwin', |
| 22 | + url: `https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}/ripgrep-${RIPGREP_VERSION}-x86_64-apple-darwin.tar.gz`, |
| 23 | + executable: 'rg', |
| 24 | + archivePath: `ripgrep-${RIPGREP_VERSION}-x86_64-apple-darwin/rg` |
| 25 | + }, |
| 26 | + { |
| 27 | + name: 'arm64-darwin', |
| 28 | + url: `https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}/ripgrep-${RIPGREP_VERSION}-aarch64-apple-darwin.tar.gz`, |
| 29 | + executable: 'rg', |
| 30 | + archivePath: `ripgrep-${RIPGREP_VERSION}-aarch64-apple-darwin/rg` |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'x64-linux', |
| 34 | + url: `https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}/ripgrep-${RIPGREP_VERSION}-x86_64-unknown-linux-musl.tar.gz`, |
| 35 | + executable: 'rg', |
| 36 | + archivePath: `ripgrep-${RIPGREP_VERSION}-x86_64-unknown-linux-musl/rg` |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'arm64-linux', |
| 40 | + url: `https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}/ripgrep-${RIPGREP_VERSION}-aarch64-unknown-linux-gnu.tar.gz`, |
| 41 | + executable: 'rg', |
| 42 | + archivePath: `ripgrep-${RIPGREP_VERSION}-aarch64-unknown-linux-gnu/rg` |
| 43 | + } |
| 44 | +] |
| 45 | + |
| 46 | +async function downloadAndExtract(platform: typeof PLATFORMS[0]): Promise<void> { |
| 47 | + const vendorDir = join('vendor', 'ripgrep', platform.name) |
| 48 | + const outputPath = join(vendorDir, platform.executable) |
| 49 | + |
| 50 | + // Skip if already exists |
| 51 | + if (existsSync(outputPath)) { |
| 52 | + console.log(`β ${platform.name} already exists, skipping`) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + console.log(`π₯ Downloading ${platform.name}...`) |
| 57 | + |
| 58 | + const response = await fetch(platform.url) |
| 59 | + if (!response.ok) { |
| 60 | + throw new Error(`Failed to download ${platform.url}: ${response.statusText}`) |
| 61 | + } |
| 62 | + |
| 63 | + const arrayBuffer = await response.arrayBuffer() |
| 64 | + const buffer = Buffer.from(arrayBuffer) |
| 65 | + |
| 66 | + // Create vendor directory |
| 67 | + await mkdir(vendorDir, { recursive: true }) |
| 68 | + |
| 69 | + if (platform.url.endsWith('.zip')) { |
| 70 | + // Handle ZIP files |
| 71 | + const zip = AdmZip(buffer) |
| 72 | + const entry = zip.getEntry(platform.archivePath) |
| 73 | + if (!entry) { |
| 74 | + throw new Error(`Could not find ${platform.archivePath} in archive`) |
| 75 | + } |
| 76 | + await writeFile(outputPath, entry.getData()) |
| 77 | + } else { |
| 78 | + // Handle tar.gz files |
| 79 | + const { spawn } = require('child_process') |
| 80 | + const tempFile = join(vendorDir, 'temp.tar.gz') |
| 81 | + |
| 82 | + // Write temp file |
| 83 | + await writeFile(tempFile, buffer) |
| 84 | + |
| 85 | + // Extract using tar |
| 86 | + await new Promise<void>((resolve, reject) => { |
| 87 | + const tar = spawn('tar', ['-xzf', tempFile, '-C', vendorDir, '--strip-components=1', platform.archivePath], { |
| 88 | + stdio: 'inherit' |
| 89 | + }) |
| 90 | + |
| 91 | + tar.on('close', (code) => { |
| 92 | + if (code === 0) { |
| 93 | + resolve() |
| 94 | + } else { |
| 95 | + reject(new Error(`tar extraction failed with code ${code}`)) |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + tar.on('error', reject) |
| 100 | + }) |
| 101 | + |
| 102 | + // Clean up temp file |
| 103 | + try { |
| 104 | + await Bun.file(tempFile).delete() |
| 105 | + } catch {} |
| 106 | + } |
| 107 | + |
| 108 | + // Make executable on Unix systems |
| 109 | + if (platform.executable === 'rg') { |
| 110 | + const { spawn } = require('child_process') |
| 111 | + await new Promise<void>((resolve, reject) => { |
| 112 | + const chmod = spawn('chmod', ['+x', outputPath]) |
| 113 | + chmod.on('close', (code) => { |
| 114 | + if (code === 0) { |
| 115 | + resolve() |
| 116 | + } else { |
| 117 | + reject(new Error(`chmod failed with code ${code}`)) |
| 118 | + } |
| 119 | + }) |
| 120 | + chmod.on('error', reject) |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + console.log(`β ${platform.name} downloaded and extracted`) |
| 125 | +} |
| 126 | + |
| 127 | +async function main() { |
| 128 | + console.log(`π§ Fetching ripgrep v${RIPGREP_VERSION} binaries...`) |
| 129 | + |
| 130 | + // Create base vendor directory |
| 131 | + await mkdir(join('vendor', 'ripgrep'), { recursive: true }) |
| 132 | + |
| 133 | + // Download all platforms in parallel |
| 134 | + await Promise.all(PLATFORMS.map(downloadAndExtract)) |
| 135 | + |
| 136 | + // Copy COPYING file |
| 137 | + console.log('π Downloading COPYING file...') |
| 138 | + const copyingResponse = await fetch(`https://raw.githubusercontent.com/BurntSushi/ripgrep/main/COPYING`) |
| 139 | + if (copyingResponse.ok) { |
| 140 | + const copyingText = await copyingResponse.text() |
| 141 | + await writeFile(join('vendor', 'ripgrep', 'COPYING'), copyingText) |
| 142 | + console.log('β COPYING file downloaded') |
| 143 | + } |
| 144 | + |
| 145 | + console.log('π All ripgrep binaries downloaded successfully!') |
| 146 | + console.log('π Files are located in vendor/ripgrep/') |
| 147 | +} |
| 148 | + |
| 149 | +if (import.meta.main) { |
| 150 | + main().catch((error) => { |
| 151 | + console.error('β Error:', error.message) |
| 152 | + process.exit(1) |
| 153 | + }) |
| 154 | +} |
0 commit comments