| 1 | const fs = require("fs"); |
| 2 | const path = require("path"); |
| 3 | const { rimrafSync } = require("rimraf"); |
| 4 | const tar = require("tar"); |
| 5 | const { RIPGREP_VERSION, TARGET_TO_RIPGREP_RELEASE } = require("./targets"); |
| 6 | const AdmZip = require("adm-zip"); |
| 7 | const { ProxyAgent } = require("undici"); |
| 8 | |
| 9 | const RIPGREP_BASE_URL = `https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}`; |
| 10 | |
| 11 | /** |
| 12 | * Downloads a file from a URL to a specified path |
| 13 | * |
| 14 | * @param {string} url - The URL to download from |
| 15 | * @param {string} destPath - The destination path for the downloaded file |
| 16 | * @returns {Promise<void>} |
| 17 | */ |
| 18 | async function downloadFile(url, destPath) { |
| 19 | // Use the built-in fetch API instead of node-fetch |
| 20 | // Use proxy if set in environment variables |
| 21 | const proxy = process.env.https_proxy || process.env.HTTPS_PROXY; |
| 22 | const agent = proxy ? new ProxyAgent(proxy) : undefined; |
| 23 | |
| 24 | const response = await fetch(url, { |
| 25 | redirect: "follow", // Automatically follow redirects |
| 26 | dispatcher: agent, |
| 27 | }); |
| 28 | |
| 29 | if (!response.ok) { |
| 30 | throw new Error(`Failed to download file, status code: ${response.status}`); |
| 31 | } |
| 32 | |
| 33 | // Get the response as an array buffer and write it to the file |
| 34 | const buffer = await response.arrayBuffer(); |
| 35 | fs.writeFileSync(destPath, Buffer.from(buffer)); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Extracts an archive to a specified directory |
| 40 | * |
| 41 | * @param {string} archivePath - Path to the archive file |
| 42 | * @param {string} targetDir - Directory to extract the archive to |
| 43 | * @param {string} platform - Platform identifier (e.g., 'darwin', 'linux', 'win32') |
| 44 | * @returns {Promise<void>} |
| 45 | */ |
| 46 | async function extractArchive(archivePath, targetDir, platform) { |
| 47 | if (platform === "win32" || archivePath.endsWith(".zip")) { |
| 48 | // Simple zip extraction for Windows - extract rg.exe |
| 49 | const zip = new AdmZip(archivePath); |
| 50 | |
| 51 | const rgEntry = zip |
| 52 | .getEntries() |
| 53 | .find((entry) => entry.entryName.endsWith("rg.exe")); |
| 54 | |
| 55 | if (!rgEntry) { |
| 56 | throw new Error("Could not find rg.exe in the downloaded archive"); |
| 57 | } |
| 58 | |
| 59 | // Extract the found rg.exe file to the target directory |
| 60 | const entryData = rgEntry.getData(); |
| 61 | fs.writeFileSync(path.join(targetDir, "rg.exe"), entryData); |
| 62 | } else { |
| 63 | await tar.extract({ |
| 64 | file: archivePath, |
| 65 | cwd: targetDir, |
| 66 | strip: 1, // Strip the top-level directory |
| 67 | filter: (path) => path.endsWith("/rg"), |
| 68 | }); |
| 69 | } |
| 70 | } |
| 71 | /** |
| 72 | * Downloads and installs ripgrep for the specified target |
| 73 | * |
| 74 | * @param {string} target - Target platform-arch (e.g., 'darwin-x64') |
| 75 | * @param {string} targetDir - Directory to install ripgrep to |
| 76 | * @returns {Promise<string>} - Path to the installed ripgrep binary |
| 77 | */ |
| 78 | async function downloadRipgrep(target, targetDir) { |
| 79 | // Get the ripgrep release file name for the target |
| 80 | const releaseFile = TARGET_TO_RIPGREP_RELEASE[target]; |
| 81 | if (!releaseFile) { |
| 82 | throw new Error(`Unsupported target: ${target}`); |
| 83 | } |
| 84 | |
| 85 | const platform = target.split("-")[0]; |
| 86 | const downloadUrl = `${RIPGREP_BASE_URL}/${releaseFile}`; |
| 87 | const tempDir = path.join(targetDir, "temp"); |
| 88 | |
| 89 | // Create temp directory |
| 90 | fs.mkdirSync(tempDir, { recursive: true }); |
| 91 | |
| 92 | const archivePath = path.join(tempDir, releaseFile); |
| 93 | |
| 94 | try { |
| 95 | // Download the ripgrep release |
| 96 | console.log(`[info] Downloading ripgrep from ${downloadUrl}`); |
| 97 | await downloadFile(downloadUrl, archivePath); |
| 98 | |
| 99 | // Extract the archive |
| 100 | console.log(`[info] Extracting ripgrep to ${targetDir}`); |
| 101 | await extractArchive(archivePath, targetDir, platform); |
| 102 | |
| 103 | // Make the binary executable on Unix-like systems |
| 104 | if (platform !== "win32") { |
| 105 | const rgPath = path.join(targetDir, "rg"); |
| 106 | fs.chmodSync(rgPath, 0o755); |
| 107 | } |
| 108 | |
| 109 | // Clean up |
| 110 | rimrafSync(tempDir); |
| 111 | |
| 112 | // Return the path to the ripgrep binary |
| 113 | const binName = platform === "win32" ? "rg.exe" : "rg"; |
| 114 | return path.join(targetDir, binName); |
| 115 | } catch (error) { |
| 116 | console.error(`[error] Failed to download ripgrep for ${target}:`, error); |
| 117 | // Clean up temp directory on error |
| 118 | rimrafSync(tempDir); |
| 119 | throw error; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | module.exports = { |
| 124 | downloadRipgrep, |
| 125 | RIPGREP_VERSION, |
| 126 | }; |