Skip to content

Commit

Permalink
feat: axios; re-throw more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pikdum committed Aug 22, 2023
1 parent 69e97d6 commit a17029e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 43 deletions.
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/config.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
38 changes: 11 additions & 27 deletions src/lib/proton.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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)) {
Expand Down Expand Up @@ -108,6 +94,7 @@ export const setProton = (protonBuild) => {
console.log(`Proton set to: ${protonBuild}`);
} catch (error) {
console.error("Error setting Proton:", error);
throw error;
}
};

Expand All @@ -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)) {
Expand Down
39 changes: 39 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
18 changes: 6 additions & 12 deletions src/lib/vortex.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}
};

Expand Down
5 changes: 2 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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,
launchVortex,
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) {
Expand Down

0 comments on commit a17029e

Please sign in to comment.