Add Vencord Loading & tray icon

This commit is contained in:
Vendicated
2023-04-04 00:41:52 +02:00
parent 307051141d
commit f45d6482ac
12 changed files with 176 additions and 11 deletions

41
src/main/utils/http.ts Normal file
View File

@@ -0,0 +1,41 @@
import { createWriteStream } from "fs";
import type { IncomingMessage } from "http";
import { RequestOptions, get } from "https";
import { finished } from "stream/promises";
export async function downloadFile(url: string, file: string, options: RequestOptions = {}) {
const res = await simpleReq(url, options);
await finished(
res.pipe(createWriteStream(file, {
autoClose: true
}))
);
}
export function simpleReq(url: string, options: RequestOptions = {}) {
return new Promise<IncomingMessage>((resolve, reject) => {
get(url, options, res => {
const { statusCode, statusMessage, headers } = res;
if (statusCode! >= 400)
return void reject(`${statusCode}: ${statusMessage} - ${url}`);
if (statusCode! >= 300)
return simpleReq(headers.location!, options)
.then(resolve)
.catch(reject);
resolve(res);
});
});
}
export async function simpleGet(url: string, options: RequestOptions = {}) {
const res = await simpleReq(url, options);
return new Promise<Buffer>((resolve, reject) => {
const chunks = [] as Buffer[];
res.once("error", reject);
res.on("data", chunk => chunks.push(chunk));
res.once("end", () => resolve(Buffer.concat(chunks)));
});
}

View File

@@ -0,0 +1,54 @@
import { existsSync, mkdirSync } from "fs";
import { join } from "path";
import { USER_AGENT, VENCORD_FILES_DIR } from "../constants";
import { downloadFile, simpleGet } from "./http";
// TODO: Setting to switch repo
const API_BASE = "https://api.github.com/repos/Vendicated/VencordDev";
const FILES_TO_DOWNLOAD = [
"vencordDesktopMain.js",
"preload.js",
"vencordDesktopRenderer.js",
"renderer.css"
];
export async function githubGet(endpoint: string) {
return simpleGet(API_BASE + endpoint, {
headers: {
Accept: "application/vnd.github+json",
"User-Agent": USER_AGENT
}
});
}
export async function downloadVencordFiles() {
const release = await githubGet("/releases/latest");
const data = JSON.parse(release.toString("utf-8"));
const assets = data.assets as Array<{
name: string;
browser_download_url: string;
}>;
await Promise.all(
assets
.filter(({ name }) => FILES_TO_DOWNLOAD.some(f => name.startsWith(f)))
.map(({ name, browser_download_url }) =>
downloadFile(
browser_download_url,
join(
VENCORD_FILES_DIR,
name.replace(/vencordDesktop(\w)/, (_, c) => c.toLowerCase())
)
)
)
);
}
export async function ensureVencordFiles() {
if (existsSync(join(VENCORD_FILES_DIR, "main.js"))) return;
mkdirSync(VENCORD_FILES_DIR, { recursive: true });
await downloadVencordFiles();
}