|
| 1 | +const { default: axios } = require("axios"); |
| 2 | +const util = require("node:util"); |
| 3 | +const fs = require("node:fs"); |
| 4 | + |
| 5 | +const exec = util.promisify(require("node:child_process").exec); |
| 6 | + |
| 7 | +const downloadAria2 = async () => { |
| 8 | + if (fs.existsSync("aria2")) { |
| 9 | + console.log("Aria2 already exists, skipping download..."); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + const file = |
| 14 | + process.platform === "win32" |
| 15 | + ? "aria2-1.37.0-win-64bit-build1.zip" |
| 16 | + : "aria2-1.37.0-1-x86_64.pkg.tar.zst"; |
| 17 | + |
| 18 | + const downloadUrl = |
| 19 | + process.platform === "win32" |
| 20 | + ? `https://github.com/aria2/aria2/releases/download/release-1.37.0/${file}` |
| 21 | + : "https://archlinux.org/packages/extra/x86_64/aria2/download/"; |
| 22 | + |
| 23 | + console.log(`Downloading ${file}...`); |
| 24 | + |
| 25 | + const response = await axios.get(downloadUrl, { responseType: "stream" }); |
| 26 | + |
| 27 | + const stream = response.data.pipe(fs.createWriteStream(file)); |
| 28 | + |
| 29 | + stream.on("finish", async () => { |
| 30 | + console.log(`Downloaded ${file}, extracting...`); |
| 31 | + |
| 32 | + if (process.platform === "win32") { |
| 33 | + await exec(`npx extract-zip ${file}`); |
| 34 | + console.log("Extracted. Renaming folder..."); |
| 35 | + |
| 36 | + fs.renameSync(file.replace(".zip", ""), "aria2"); |
| 37 | + } else { |
| 38 | + await exec(`tar --zstd -xvf ${file} usr/bin/aria2c`); |
| 39 | + console.log("Extracted. Copying binary file..."); |
| 40 | + fs.mkdirSync("aria2"); |
| 41 | + fs.copyFileSync("usr/bin/aria2c", "aria2/aria2c"); |
| 42 | + fs.rmSync("usr", { recursive: true }); |
| 43 | + } |
| 44 | + |
| 45 | + console.log(`Extracted ${file}, removing compressed downloaded file...`); |
| 46 | + fs.rmSync(file); |
| 47 | + }); |
| 48 | +}; |
| 49 | + |
| 50 | +downloadAria2(); |
0 commit comments