From a17029e9e52dce3285258e1d0a74210fefb2d239 Mon Sep 17 00:00:00 2001 From: pikdum Date: Mon, 21 Aug 2023 23:44:06 -0500 Subject: [PATCH] feat: axios; re-throw more errors --- package-lock.json | 91 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/lib/config.js | 2 +- src/lib/proton.js | 38 ++++++-------------- src/lib/util.js | 39 ++++++++++++++++++++ src/lib/vortex.js | 18 ++++------ src/main.js | 5 ++- 7 files changed, 151 insertions(+), 43 deletions(-) create mode 100644 src/lib/util.js diff --git a/package-lock.json b/package-lock.json index aa935c0..e00880f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.2.3", "license": "ISC", "dependencies": { + "axios": "^1.4.0", "commander": "^11.0.0", "esbuild": "^0.19.0", "postject": "^1.0.0-alpha.6" @@ -645,6 +646,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/available-typed-arrays": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz", @@ -657,6 +663,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/axios": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", + "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -729,6 +745,17 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "11.0.0", "resolved": "https://registry.npmjs.org/commander/-/commander-11.0.0.tgz", @@ -802,6 +829,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -1303,6 +1338,25 @@ "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", + "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -1312,6 +1366,19 @@ "is-callable": "^1.1.3" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -1910,6 +1977,25 @@ "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", "dev": true }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -2186,6 +2272,11 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", diff --git a/package.json b/package.json index bd67dd2..c89eaa7 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "pikdum", "license": "ISC", "dependencies": { + "axios": "^1.4.0", "commander": "^11.0.0", "esbuild": "^0.19.0", "postject": "^1.0.0-alpha.6" diff --git a/src/lib/config.js b/src/lib/config.js index ded8f68..b2b04bb 100644 --- a/src/lib/config.js +++ b/src/lib/config.js @@ -1,5 +1,5 @@ -import { homedir } from "os"; import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; +import { homedir } from "os"; import path from "path"; export const BASE_DIR = path.join(homedir(), ".vortex-linux"); diff --git a/src/lib/proton.js b/src/lib/proton.js index a63aaf4..c82800a 100644 --- a/src/lib/proton.js +++ b/src/lib/proton.js @@ -1,20 +1,12 @@ import { exec } from "child_process"; -import path from "path"; -import { - createWriteStream, - existsSync, - lstatSync, - mkdirSync, - symlinkSync, - unlinkSync, -} from "fs"; +import { existsSync, lstatSync, mkdirSync, symlinkSync, unlinkSync } from "fs"; import os from "os"; +import path from "path"; import { promisify } from "util"; -import { pipeline } from "stream"; import { BASE_DIR, getConfig } from "./config.js"; +import { downloadFile, extractFile } from "./util.js"; -const pipelineAsync = promisify(pipeline); const execAsync = promisify(exec); export const downloadProton = async (downloadUrl) => { @@ -27,19 +19,13 @@ export const downloadProton = async (downloadUrl) => { mkdirSync(extractPath, { recursive: true }); } - // Download the file - console.log("Downloading Proton..."); - const response = await fetch(downloadUrl); - await pipelineAsync(response.body, createWriteStream(tempFilePath)); - - // Extract the file using GNU tar - console.log("Extracting Proton..."); - const extractCommand = `tar -xf ${tempFilePath} -C ${extractPath}`; - await execAsync(extractCommand); + await downloadFile(downloadUrl, tempFilePath); + await extractFile(tempFilePath, extractPath); console.log("Proton downloaded and extracted successfully!"); } catch (error) { console.error("Error downloading and extracting Proton:", error); + throw error; } finally { // Clean up the temporary file if (existsSync(tempFilePath)) { @@ -108,6 +94,7 @@ export const setProton = (protonBuild) => { console.log(`Proton set to: ${protonBuild}`); } catch (error) { console.error("Error setting Proton:", error); + throw error; } }; @@ -121,17 +108,14 @@ export const protonRunUrl = async (downloadUrl, args) => { try { // Download the file - const response = await fetch(downloadUrl); - if (!response.ok) { - throw new Error( - `Failed to download file (${response.status} ${response.statusText})`, - ); - } - await pipelineAsync(response.body, createWriteStream(tempFilePath)); + await downloadFile(downloadUrl, tempFilePath); // Run protonRun with the command console.log(`Running: ${command}`); await protonRun(command); + } catch (error) { + console.error("Error running protonRun:", error); + throw error; } finally { // Clean up the temporary file if (existsSync(tempFilePath)) { diff --git a/src/lib/util.js b/src/lib/util.js new file mode 100644 index 0000000..1b65a78 --- /dev/null +++ b/src/lib/util.js @@ -0,0 +1,39 @@ +import axios from "axios"; +import { exec } from "child_process"; +import { createWriteStream } from "fs"; +import util from "util"; + +const execAsync = util.promisify(exec); + +export const downloadFile = async (url, targetPath) => { + try { + console.log(`Downloading ${url}...`); + const response = await axios({ + method: "get", + url: url, + responseType: "stream", + }); + + const writer = createWriteStream(targetPath); + response.data.pipe(writer); + + await new Promise((resolve, reject) => { + writer.on("finish", resolve); + writer.on("error", reject); + }); + } catch (error) { + console.error("Error downloading file:", error); + throw error; + } +}; + +export const extractFile = async (file, directory) => { + try { + console.log(`Extracting ${file}...`); + const extractCommand = `tar -xf ${file} -C ${directory}`; + await execAsync(extractCommand); + } catch (error) { + console.error("Error extracting archive:", error); + throw error; + } +}; diff --git a/src/lib/vortex.js b/src/lib/vortex.js index c4f1f4f..525541b 100644 --- a/src/lib/vortex.js +++ b/src/lib/vortex.js @@ -1,22 +1,17 @@ -import path from "path"; import { chmodSync, - createWriteStream, existsSync, mkdirSync, unlinkSync, writeFileSync, } from "fs"; -import { promisify } from "util"; -import { pipeline } from "stream"; +import path from "path"; +import vortexDesktop from "../assets/vortex.desktop"; +import vortexIcon from "../assets/vortex.ico"; import { BASE_DIR } from "./config.js"; import { protonRun } from "./proton.js"; - -import vortexIcon from "../assets/vortex.ico"; -import vortexDesktop from "../assets/vortex.desktop"; - -const pipelineAsync = promisify(pipeline); +import { downloadFile } from "./util.js"; const VORTEX_DIR = path.join( BASE_DIR, @@ -45,13 +40,12 @@ export const downloadVortex = async (downloadUrl) => { unlinkSync(targetPath); } - // Download the file - const response = await fetch(downloadUrl); - await pipelineAsync(response.body, createWriteStream(targetPath)); + await downloadFile(downloadUrl, targetPath); console.log("Vortex downloaded successfully!"); } catch (error) { console.error("Error downloading Vortex:", error); + throw error; } }; diff --git a/src/main.js b/src/main.js index 0be1c9f..f972ca4 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,7 @@ import { program } from "commander"; -import { downloadProton, setProton, protonRunUrl } from "./lib/proton.js"; +import { getConfig, setConfig } from "./lib/config.js"; +import { downloadProton, protonRunUrl, setProton } from "./lib/proton.js"; import { downloadVortex, installVortex, @@ -8,8 +9,6 @@ import { setupVortexDesktop, } from "./lib/vortex.js"; -import { getConfig, setConfig } from "./lib/config.js"; - const originalEmit = process.emit; // eslint-disable-next-line process.emit = function (name, data, ...args) {