Skip to content

Commit

Permalink
ProgramData version detection + Semver Version sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
QbDesu committed Sep 3, 2021
1 parent 89455c6 commit 024f1d7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
76 changes: 56 additions & 20 deletions src/renderer/actions/paths.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
const fs = require("fs");
const path = require("path");
import {remote} from "electron";
const semverGreaterThan = require("semver/functions/gt");
const semverValid = require("semver/functions/valid");

export const platforms = {stable: "Discord", ptb: "Discord PTB", canary: "Discord Canary"};
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, ""));
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];
if (!version) return "";
resourcePath = path.join(basedir, version, "resources");
const releaseChannelFolder = releaseChannel.replace(" ", "");

const basedirs = [
path.join(process.env.PROGRAMDATA, process.env.USERNAME, releaseChannelFolder),
path.join(process.env.LOCALAPPDATA, releaseChannelFolder)
].filter(dir => fs.existsSync(dir));

let detectedVersion = "0.0.0";
for (const basedir of basedirs) {
for (const versiondir of fs.readdirSync(basedir)) {
if (versiondir.startsWith("app-")) {
const version = versiondir.replace("app-","");
if (semverValid(version) && semverGreaterThan(version, detectedVersion)) {
detectedVersion = version;
resourcePath = path.join(basedir, versiondir, "resources");
}
}
}
}
}
else if (process.platform === "darwin") {
resourcePath = path.join("/Applications", `${releaseChannel}.app`, "Contents", "Resources");
}
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];
if (!version) return "";
resourcePath = path.join(basedir, version, "modules", "discord_desktop_core");
const releaseChannelFolder = releaseChannel.replace(" ", "").toLowerCase();
const basedir = path.join(remote.app.getPath("appData"), releaseChannelFolder);

let detectedVersion = "0.0.0";
for (const version of fs.readdirSync(basedir)) {
if (semverValid(version) && semverGreaterThan(version, detectedVersion)) {
detectedVersion = version;
resourcePath = path.join(basedir, version, "modules", "discord_desktop_core");
}
}
}

if (fs.existsSync(resourcePath)) return resourcePath;
Expand Down Expand Up @@ -53,14 +75,23 @@ const validateWindows = function(channel, proposedPath) {

let resourcePath = "";
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];
if (!version) return "";
resourcePath = path.join(proposedPath, version, "resources");
if (selected === channelName) {
const basedir = proposedPath;
let detectedVersion;
for (const versiondir of fs.readdirSync(proposedPath)) {
if (versiondir.startsWith("app-")) {
const version = versiondir.replace("app-","");
if (semverValid(version) && semverGreaterThan(version, detectedVersion)) {
detectedVersion = version;
resourcePath = path.join(basedir, versiondir, "resources");
}
}
}
}

if (selected.startsWith("app-") && selected.split(".").length > 2) resourcePath = path.join(proposedPath, "resources");
if (selected.startsWith("app-") && semverValid(selected.replace("app-", ""))) {
resourcePath = path.join(proposedPath, "resources");
}
if (selected === "resources") resourcePath = proposedPath;

const executablePath = path.join(resourcePath, "..", `${channelName}.exe`);
Expand Down Expand Up @@ -90,11 +121,16 @@ 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];
if (!version) return "";
resourcePath = path.join(proposedPath, version, "modules", "discord_desktop_core");
const basedir = proposedPath;
let detectedVersion = "0.0.0";
for (const version of fs.readdirSync(basedir)) {
if (semverValid(version) && semverGreaterThan(version, detectedVersion)) {
detectedVersion = version;
resourcePath = path.join(basedir, version, "modules", "discord_desktop_core");
}
}
}
if (selected.split(".").length > 2) resourcePath = path.join(proposedPath, "modules", "discord_desktop_core");
if (semverValid(selected)) resourcePath = path.join(proposedPath, "modules", "discord_desktop_core");
if (selected === "modules") resourcePath = path.join(proposedPath, "discord_desktop_core");
if (selected === "discord_desktop_core") resourcePath = proposedPath;

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6221,7 +6221,7 @@ semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.2.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.1.3, semver@^7.2.1, semver@^7.3.2:
semver@^7.1.3, semver@^7.2.1, semver@^7.3.2, semver@^7.3.5:
version "7.3.5"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==
Expand Down

0 comments on commit 024f1d7

Please sign in to comment.