Elffuss BraidHub

kikocis/continue public

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