Skip to content

Commit

Permalink
Merge pull request #223 from BetterDiscord/development
Browse files Browse the repository at this point in the history
Merge development
  • Loading branch information
zerebos committed Oct 23, 2021
2 parents 5f6a6be + fa31f1a commit 3a18de5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 157 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"productName": "BetterDiscord Installer",
"description": "A simple standalone program which automates the installation, removal and maintenance of BetterDiscord.",
"author": "BetterDiscord",
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"scripts": {
"dev": "electron-webpack dev",
Expand All @@ -15,11 +15,9 @@
"dist:dir": "yarn dist --dir -c.compression=store -c.mac.identity=null"
},
"dependencies": {
"semver": "^7.3.5",
"source-map-support": "^0.5.16"
},
"devDependencies": {
"del": "^6.0.0",
"electron": "^9.4.0",
"electron-builder": "^22.4.1",
"electron-webpack": "^2.8.2",
Expand All @@ -28,6 +26,8 @@
"find-process": "^1.4.4",
"focus-visible": "^5.2.0",
"phin": "^3.6.0",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"svelte": "^3.38.2",
"svelte-loader": "^3.0.0",
"svelte-spa-router": "^3.1.0",
Expand Down
52 changes: 52 additions & 0 deletions src/renderer/actions/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,44 @@ import {showRestartNotice} from "./utils/notices";
import doSanityCheck from "./utils/sanity";

const MAKE_DIR_PROGRESS = 30;
const CHECK_OLD_INSTALL = 40;
const TRANSFER_OLD_ADDONS = 50;
const DOWNLOAD_PACKAGE_PROGRESS = 60;
const INJECT_SHIM_PROGRESS = 90;
const RESTART_DISCORD_PROGRESS = 100;

const oldBDFolder = path.join(remote.app.getPath("home"), "Library", "Preferences", "betterdiscord"); // Old MacOS
const bdFolder = path.join(remote.app.getPath("appData"), "BetterDiscord");
const bdDataFolder = path.join(bdFolder, "data");
const bdPluginsFolder = path.join(bdFolder, "plugins");
const bdThemesFolder = path.join(bdFolder, "themes");

async function checkOldMacOS(folder) {
if (await exists(folder)) {
log(`⚠️ Found old BD installation: ${folder}`);
return true;
}
return false;
}

async function transferOldAddons(oldFolder, newFolder) {
if (await exists(oldFolder)) {
const addons = await fs.readdir(oldFolder);
for (let a = 0; a < addons.length; a++) {
const oldName = path.join(oldFolder, addons[a]);
const newName = path.join(newFolder, addons[a]);
const stats = await fs.stat(oldName);
if (!stats.isFile()) continue;
try {
await fs.rename(oldName, newName);
}
catch (err) {
log(`❌ Failed to transfer: ${addons[a]}`);
}
}
}
}

async function makeDirectories(...folders) {
const progressPerLoop = (MAKE_DIR_PROGRESS - progress.value) / folders.length;
for (const folder of folders) {
Expand Down Expand Up @@ -110,6 +139,29 @@ export default async function(config) {
if (makeDirErr) return fail();
log("✅ Directories created");
progress.set(MAKE_DIR_PROGRESS);


if (process.platform === "darwin") {
lognewline("Checking for old MacOS installation...");
const found = await checkOldMacOS(oldBDFolder);
progress.set(CHECK_OLD_INSTALL);
if (found) {
const confirmation = await remote.dialog.showMessageBox(remote.BrowserWindow.getFocusedWindow(), {
type: "question",
title: "Old Install Found",
message: "Found an old BD installation, do you want to transfer your plugins and themes?",
noLink: true,
cancelId: 1,
buttons: ["Yes", "No"]
});

if (confirmation.response === 0) {
await transferOldAddons(path.join(oldBDFolder, "plugins"), path.join(bdFolder, "plugins"));
await transferOldAddons(path.join(oldBDFolder, "themes"), path.join(bdFolder, "themes"));
progress.set(TRANSFER_OLD_ADDONS);
}
}
}


lognewline("Downloading asar file");
Expand Down
11 changes: 6 additions & 5 deletions src/renderer/actions/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export const locations = {stable: "", ptb: "", canary: ""};
const getDiscordPath = function(releaseChannel) {
let resourcePath = "";
if (process.platform === "win32") {
const basedir = path.join(process.env.LOCALAPPDATA, releaseChannel.replace(/ /g, ""));
let basedir = path.join(process.env.LOCALAPPDATA, releaseChannel.replace(/ /g, "")); // Normal install path in AppData\Local
if (!fs.existsSync(basedir)) basedir = path.join(process.env.PROGRAMDATA, process.env.USERNAME, releaseChannel.replace(/ /g, "")); // Atypical location in ProgramData\%username%
if (!fs.existsSync(basedir)) return "";
const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort()[0];
const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0];
if (!version) return "";
resourcePath = path.join(basedir, version, "resources");
}
Expand All @@ -20,7 +21,7 @@ const getDiscordPath = function(releaseChannel) {
else {
const basedir = path.join(remote.app.getPath("userData"), "..", releaseChannel.toLowerCase().replace(" ", ""));
if (!fs.existsSync(basedir)) return "";
const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort()[0];
const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0];
if (!version) return "";
resourcePath = path.join(basedir, version, "modules", "discord_desktop_core");
}
Expand Down Expand Up @@ -55,7 +56,7 @@ const validateWindows = function(channel, proposedPath) {
const selected = path.basename(proposedPath);
const isBaseDir = selected === channelName;
if (isBaseDir) {
const version = fs.readdirSync(proposedPath).filter(f => fs.lstatSync(path.join(proposedPath, f)).isDirectory() && f.split(".").length > 1).sort()[0];
const version = fs.readdirSync(proposedPath).filter(f => fs.lstatSync(path.join(proposedPath, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0];
if (!version) return "";
resourcePath = path.join(proposedPath, version, "resources");
}
Expand Down Expand Up @@ -90,7 +91,7 @@ const validateLinux = function(channel, proposedPath) {
let resourcePath = "";
const selected = path.basename(proposedPath);
if (selected === channelName) {
const version = fs.readdirSync(proposedPath).filter(f => fs.lstatSync(path.join(proposedPath, f)).isDirectory() && f.split(".").length > 1).sort()[0];
const version = fs.readdirSync(proposedPath).filter(f => fs.lstatSync(path.join(proposedPath, f)).isDirectory() && f.split(".").length > 1).sort().reverse()[0];
if (!version) return "";
resourcePath = path.join(proposedPath, version, "modules", "discord_desktop_core");
}
Expand Down
34 changes: 22 additions & 12 deletions src/renderer/actions/repair.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import {progress, status} from "../stores/installation";
import {remote} from "electron";
import {promises as fs} from "fs";
import del from "del";
import originalFs from "original-fs";
import rimraf from "rimraf";
import path from "path";
import install from "./install.js";
import {log, lognewline} from "./utils/log";
Expand All @@ -23,16 +24,16 @@ async function deleteAppDirs(paths) {
for (const discordPath of paths) {
log("Removing " + discordPath);
const appPath = path.join(discordPath, "app");
try {
if (await exists(appPath)) await del(appPath, {force: true});
log("✅ Deletion successful");
progress.set(progress.value + progressPerLoop);
}
catch (err) {
log(` Could not delete folder ${appPath}`);
log(`❌ ${err.message}`);
return err;
if (await exists(appPath)) {
const error = await new Promise(resolve => rimraf(appPath, originalFs, resolve));
if (error) {
log(` Could not delete folder ${appPath}`);
log(`❌ ${error.message}`);
return error;
}
}
log("✅ Deletion successful");
progress.set(progress.value + progressPerLoop);
}
}

Expand All @@ -44,7 +45,16 @@ async function deleteModuleDirs(config) {
const roaming = path.join(remote.app.getPath("userData"), "..", platforms[channel].replace(" ", "").toLowerCase());
try {
const versionDir = (await fs.readdir(roaming)).find(d => d.split(".").length > 2);
if (await exists(path.join(versionDir, "modules"))) await del(versionDir, {force: true});
const modulesPath = path.join(roaming, versionDir, "modules");
log("Removing " + modulesPath);
if (await exists(modulesPath)) {
const error = await new Promise(resolve => rimraf(path.join(modulesPath), originalFs, resolve));
if (error) {
log(`❌ Could not delete modules in ${roaming}`);
log(`❌ ${error.message}`);
return error;
}
}
log("✅ Deletion successful");
progress.set(progress.value + progressPerLoop);
}
Expand Down Expand Up @@ -110,7 +120,7 @@ export default async function(config) {
lognewline("Deleting discord modules...");
const deleteModulesErr = await deleteModuleDirs(config);
if (deleteModulesErr) return fail();
log("✅ Shims deleted");
log("✅ Modules deleted");
progress.set(DELETE_MODULE_DIRS_PROGRESS);


Expand Down
8 changes: 6 additions & 2 deletions src/renderer/actions/uninstall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {promises as fs} from "fs";
import del from "del";
import originalFs from "original-fs";
import rimraf from "rimraf";
import path from "path";

import {progress} from "../stores/installation";
Expand All @@ -26,7 +27,10 @@ async function deleteShims(paths) {
const indexFile = path.join(discordPath, "index.js");
try {
if (process.platform === "win32" || process.platform === "darwin") {
if (await exists(appPath)) await del(appPath, {force: true});
if (await exists(appPath)) {
const error = await new Promise(r => rimraf(appPath, originalFs, r));
if (error) throw error; // Throw instead because there are multiple throw points
}
}
else {
if (await exists(indexFile)) await fs.writeFile(indexFile, `module.exports = require("./core.asar");`);
Expand Down
Loading

0 comments on commit 3a18de5

Please sign in to comment.