Equicord/scripts/runInstaller.mjs

121 lines
3.6 KiB
JavaScript
Raw Normal View History

/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2023 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import "./checkNodeVersion.js";
2023-01-14 18:44:02 +01:00
import { execFileSync, execSync } from "child_process";
2026-09-18 09:48:34 -07:00
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync, cpSync, rmSync } from "fs";
import { dirname, join } from "path";
import { Readable } from "stream";
import { finished } from "stream/promises";
import { fileURLToPath } from "url";
2024-07-18 23:54:25 -04:00
const BASE_URL = "https://github.com/Equicord/Equilotl/releases/latest/download/";
2023-01-14 18:44:02 +01:00
const BASE_DIR = join(dirname(fileURLToPath(import.meta.url)), "..");
const FILE_DIR = join(BASE_DIR, "dist", "Installer");
const ETAG_FILE = join(FILE_DIR, "etag.txt");
2026-09-20 11:48:53 -04:00
function byArch(files) {
return files[process.arch] ?? files.default;
}
function getFilename() {
switch (process.platform) {
case "win32":
2026-09-20 11:48:53 -04:00
return byArch({
arm64: "EquilotlCli-arm64.exe",
default: "EquilotlCli.exe"
});
case "darwin":
2026-09-20 11:48:53 -04:00
return byArch({
x64: "EquilotlCli-x64",
arm64: "EquilotlCli-arm64",
default: "EquilotlCli-universal"
});
case "linux":
2026-09-20 11:48:53 -04:00
return byArch({
arm64: "EquilotlCli-linux-arm64",
default: "EquilotlCli-Linux"
});
default:
throw new Error("Unsupported platform: " + process.platform);
}
}
async function ensureBinary() {
const filename = getFilename();
console.log("Downloading " + filename);
mkdirSync(FILE_DIR, { recursive: true });
2026-09-19 10:44:03 -04:00
const outputFile = join(FILE_DIR, filename);
2023-01-14 18:44:02 +01:00
const etag = existsSync(outputFile) && existsSync(ETAG_FILE)
? readFileSync(ETAG_FILE, "utf-8")
: null;
const res = await fetch(BASE_URL + filename, {
headers: {
2024-07-13 00:06:01 -04:00
"User-Agent": "Equicord (https://github.com/Equicord/Equicord)",
"If-None-Match": etag
}
});
2023-01-14 18:44:02 +01:00
if (res.status === 304) {
console.log("Up to date, not redownloading!");
2023-01-14 18:44:02 +01:00
return outputFile;
}
2023-01-14 18:44:02 +01:00
if (!res.ok)
throw new Error(`Failed to download installer: ${res.status} ${res.statusText}`);
2023-01-14 18:44:02 +01:00
writeFileSync(ETAG_FILE, res.headers.get("etag"));
2026-09-19 10:44:03 -04:00
const body = Readable.fromWeb(res.body);
await finished(body.pipe(createWriteStream(outputFile, {
mode: 0o755,
autoClose: true
})));
console.log("Finished downloading!");
2023-01-14 18:44:02 +01:00
return outputFile;
}
const installerBin = await ensureBinary();
2023-01-14 18:44:02 +01:00
console.log("Now running Installer...");
2025-04-02 04:55:24 +02:00
const argStart = process.argv.indexOf("--");
const args = argStart === -1 ? [] : process.argv.slice(argStart + 1);
2024-02-06 17:59:44 +01:00
try {
2025-04-02 04:55:24 +02:00
execFileSync(installerBin, args, {
2024-02-06 17:59:44 +01:00
stdio: "inherit",
env: {
...process.env,
2024-07-13 00:06:01 -04:00
EQUICORD_USER_DATA_DIR: BASE_DIR,
2024-07-19 16:08:18 -04:00
EQUICORD_DIRECTORY: join(BASE_DIR, "dist/desktop"),
2024-07-13 00:06:01 -04:00
EQUICORD_DEV_INSTALL: "1"
2024-02-06 17:59:44 +01:00
}
});
} catch {
console.error("Something went wrong. Please check the logs above.");
}