Skip to content

Commit

Permalink
Screw GitHub's merge tool
Browse files Browse the repository at this point in the history
  • Loading branch information
zerebos committed Dec 13, 2022
1 parent fa58466 commit 5d92a88
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 148 deletions.
53 changes: 5 additions & 48 deletions src/renderer/actions/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {progress, status} from "../stores/installation";
import {remote, shell} from "electron";
import {promises as fs} from "fs";
import originalFs from "original-fs";
import path from "path";
import phin from "phin";

Expand All @@ -11,14 +10,12 @@ import fail from "./utils/fail";
import exists from "./utils/exists";
import reset from "./utils/reset";
import kill from "./utils/kill";
import restart from "./utils/restart";
import {showRestartNotice, showKillNotice} from "./utils/notices";
import {showRestartNotice} from "./utils/notices";
import doSanityCheck from "./utils/sanity";

const MAKE_DIR_PROGRESS = 30;
const DOWNLOAD_PACKAGE_PROGRESS = 60;
const INJECT_SHIM_PROGRESS = 80;
const RENAME_ASAR_PROGRESS = 90;
const INJECT_SHIM_PROGRESS = 90;
const RESTART_DISCORD_PROGRESS = 100;

const RELEASE_API = "https://api.github.com/repos/BetterDiscord/BetterDiscord/releases";
Expand Down Expand Up @@ -54,6 +51,7 @@ const getJSON = phin.defaults({method: "GET", parse: "json", followRedirects: tr
const downloadFile = phin.defaults({method: "GET", followRedirects: true, headers: {"User-Agent": "BetterDiscord/Installer", "Accept": "application/octet-stream"}});
const asarPath = path.join(bdDataFolder, "betterdiscord.asar");
async function downloadAsar() {
try {
const response = await getJSON(RELEASE_API);
const releases = response.body;
const asset = releases && releases.length && releases[0].assets && releases[0].assets.find(a => a.name.toLowerCase() === "betterdiscord.asar");
Expand Down Expand Up @@ -100,30 +98,6 @@ async function injectShims(paths) {
}
}

async function renameAsar(paths) {
const progressPerLoop = (RENAME_ASAR_PROGRESS - progress.value) / paths.length;
for (const discordPath of paths) {
const appAsar = path.join(discordPath, "app.asar");
const discordAsar = path.join(discordPath, "discord.asar");
log("Renaming " + appAsar);
try {
const appAsarExists = originalFs.existsSync(appAsar);
const discordAsarExists = originalFs.existsSync(discordAsar);
if (!appAsarExists && !discordAsarExists) throw new Error("Discord installation corrupt, please reinstall.");
if (appAsarExists && discordAsarExists) originalFs.rmSync(discordAsar);
if (appAsarExists) originalFs.renameSync(appAsar, discordAsar);
log("✅ Rename successful");
progress.set(progress.value + progressPerLoop);
}
catch (error) {
log(`❌ Could not rename asar ${appAsar}`);
log(`❌ ${error.message}`);
return error;
}

}
}


export default async function(config) {
await reset();
Expand All @@ -134,15 +108,6 @@ export default async function(config) {
const channels = Object.keys(config);
const paths = Object.values(config);

lognewline("Stopping Discord...");
const killErr = await kill(channels, (KILL_DISCORD_PROGRESS - progress.value) / channels.length);
if (killErr) {
showKillNotice();
return fail();
}
log("✅ Discord stopped");
progress.set(KILL_DISCORD_PROGRESS);


lognewline("Creating required directories...");
const makeDirErr = await makeDirectories(bdFolder, bdDataFolder, bdThemesFolder, bdPluginsFolder);
Expand All @@ -164,18 +129,10 @@ export default async function(config) {
log("✅ Shims injected");
progress.set(INJECT_SHIM_PROGRESS);

if (process.platform === "win32" || process.platform === "darwin") {
lognewline("Renaming asars...");
const renameAsarErr = await renameAsar(paths);
if (renameAsarErr) return fail();
log("✅ Asars renamed");
progress.set(RENAME_ASAR_PROGRESS);
}


lognewline("Restarting Discord...");
const restartErr = await restart(channels, (RESTART_DISCORD_PROGRESS - progress.value) / channels.length);
if (restartErr) showRestartNotice(); // No need to bail out and show failed
const killErr = await kill(channels, (RESTART_DISCORD_PROGRESS - progress.value) / channels.length);
if (killErr) showRestartNotice(); // No need to bail out and show failed
else log("✅ Discord restarted");
progress.set(RESTART_DISCORD_PROGRESS);

Expand Down
7 changes: 3 additions & 4 deletions src/renderer/actions/repair.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {progress} from "../stores/installation";
import {remote} from "electron";
import {promises as fs} from "fs";
Expand Down Expand Up @@ -96,13 +95,13 @@ export default async function(config) {
const paths = Object.values(config);


lognewline("Stopping Discord...");
const killErr = await kill(channels, (KILL_DISCORD_PROGRESS - progress.value) / channels.length); // await killProcesses(channels);
lognewline("Killing Discord...");
const killErr = await kill(channels, (KILL_DISCORD_PROGRESS - progress.value) / channels.length, false); // await killProcesses(channels);
if (killErr) {
showKillNotice();
return fail();
}
log("✅ Discord stopped");
log("✅ Discord Killed");
progress.set(KILL_DISCORD_PROGRESS);


Expand Down
55 changes: 5 additions & 50 deletions src/renderer/actions/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import fail from "./utils/fail";
import exists from "./utils/exists";
import reset from "./utils/reset";
import kill from "./utils/kill";
import restart from "./utils/restart";
import {showRestartNotice, showKillNotice} from "./utils/notices";
import {showRestartNotice} from "./utils/notices";
import doSanityCheck from "./utils/sanity";


const KILL_DISCORD_PROGRESS = 25;
const RENAME_ASAR_PROGRESS = 50;
const DELETE_SHIM_PROGRESS = 75;
const DELETE_SHIM_PROGRESS = 85;
const RESTART_DISCORD_PROGRESS = 100;


Expand All @@ -40,30 +37,6 @@ async function deleteShims(paths) {
}
}

async function renameAsar(paths) {
const progressPerLoop = (RENAME_ASAR_PROGRESS - progress.value) / paths.length;
for (const discordPath of paths) {
const appAsar = path.join(discordPath, "app.asar");
const discordAsar = path.join(discordPath, "discord.asar");
log("Renaming " + discordAsar);
try {
const appAsarExists = originalFs.existsSync(appAsar);
const discordAsarExists = originalFs.existsSync(discordAsar);
if (!appAsarExists && !discordAsarExists) throw new Error("Discord installation corrupt, please reinstall.");
if (appAsarExists && discordAsarExists) originalFs.rmSync(appAsar);
if (discordAsarExists) originalFs.renameSync(discordAsar, appAsar);
log("✅ Rename successful");
progress.set(progress.value + progressPerLoop);
}
catch (error) {
log(`❌ Could not rename asar ${discordAsar}`);
log(`❌ ${error.message}`);
return error;
}

}
}


export default async function(config) {
await reset();
Expand All @@ -74,24 +47,6 @@ export default async function(config) {
const channels = Object.keys(config);
const paths = Object.values(config);

lognewline("Stopping Discord...");
const killErr = await kill(channels, (RESTART_DISCORD_PROGRESS - progress.value) / channels.length);
if (killErr) {
showKillNotice();
return fail();
}
log("✅ Discord stopped");
progress.set(KILL_DISCORD_PROGRESS);


if (process.platform === "win32" || process.platform === "darwin") {
lognewline("Renaming asars...");
const renameAsarErr = await renameAsar(paths);
if (renameAsarErr) return fail();
log("✅ Asars renamed");
progress.set(RENAME_ASAR_PROGRESS);
}


lognewline("Deleting shims...");
const deleteErr = await deleteShims(paths);
Expand All @@ -100,9 +55,9 @@ export default async function(config) {
progress.set(DELETE_SHIM_PROGRESS);


lognewline("Restarting Discord...");
const restartErr = await restart(channels, (RESTART_DISCORD_PROGRESS - progress.value) / channels.length);
if (restartErr) showRestartNotice(); // No need to bail out and show failed
lognewline("Killing Discord...");
const killErr = await kill(channels, (RESTART_DISCORD_PROGRESS - progress.value) / channels.length);
if (killErr) showRestartNotice(); // No need to bail out
else log("✅ Discord restarted");
progress.set(RESTART_DISCORD_PROGRESS);

Expand Down
23 changes: 8 additions & 15 deletions src/renderer/actions/utils/kill.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@

import path from "path";
import findProcess from "find-process";
import kill from "tree-kill";
import {shell} from "electron";
import {progress, bins} from "../../stores/installation";
import {progress} from "../../stores/installation";
import {log} from "./log";

const platforms = {stable: "Discord", ptb: "Discord PTB", canary: "Discord Canary"};
export default async function killProcesses(channels, progressPerLoop) {
bins.set({});
export default async function killProcesses(channels, progressPerLoop, shouldRestart = true) {
for (const channel of channels) {
let processName = platforms[channel];
if (process.platform === "darwin") processName = platforms[channel]; // Discord Canary and Discord PTB on Mac
// else if (process.platform === "win32") processName = `${platforms[channel].replace(" ", "")}.exe`; // DiscordCanary and DiscordPTB on Windows/Linux
else processName = platforms[channel].replace(" ", ""); // DiscordCanary and DiscordPTB on Windows/Linux

log(`Attempting to stop ${platforms[channel]}`);
log("Attempting to kill " + processName);
try {
const results = await findProcess("name", processName, true);
if (!results || !results.length) {
Expand All @@ -27,18 +24,14 @@ export default async function killProcesses(channels, progressPerLoop) {
const parentPids = results.map(p => p.ppid);
const discordPid = results.find(p => parentPids.includes(p.pid));
const bin = process.platform === "darwin" ? path.resolve(discordPid.bin, "..", "..", "..") : discordPid.bin;
const killErr = await new Promise(r => kill(discordPid.pid, "SIGKILL", r));
if (killErr) throw killErr;
bins.update(o => {
o[channel] = bin;
return o;
});
// if (shouldRestart) setTimeout(() => shell.openPath(bin), 1000);
await new Promise(r => kill(discordPid.pid, r));
if (shouldRestart) setTimeout(() => shell.openPath(bin), 1000);
progress.set(progress.value + progressPerLoop);
}
catch (err) {
log(`❌ Could not stop ${platforms[channel]}`);
log(`❌ ${err.message}`);
const symbol = shouldRestart ? "⚠️" : "❌";
log(`${symbol} Could not kill ${platforms[channel]}`);
log(`${symbol} ${err.message}`);
return err;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/actions/utils/reset.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logs from "../../stores/logs";
import {bins, progress, status} from "../../stores/installation";
import {progress, status} from "../../stores/installation";

export default async function reset() {
logs.set([]);
progress.set(0);
status.set("");
bins.set({});
await new Promise(r => setTimeout(r, 500));
}
28 changes: 0 additions & 28 deletions src/renderer/actions/utils/restart.js

This file was deleted.

1 change: 0 additions & 1 deletion src/renderer/stores/installation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ export const paths = writable({stable: locations.stable, canary: locations.canar

export const progress = readwritable(0);
export const action = readwritable("install");
export const bins = readwritable({});

0 comments on commit 5d92a88

Please sign in to comment.