Skip to content

Commit

Permalink
feat: add legendary to before each command
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyta committed Jan 2, 2022
1 parent 504e82e commit 29d2b7b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
4 changes: 1 addition & 3 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ if (isProd) {

(async () => {
await app.whenReady();
if(!installer.isInstalled()) {
await installer.install();
}
await installer.init();

const mainWindow = createWindow('main', {
width: 1000,
Expand Down
27 changes: 24 additions & 3 deletions main/helpers/LegendaryInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { app } from "electron";
import path from "path";
import { platform } from "os";
import unzip from "unzipper";
import { execSync } from "child_process";
import { exec, execSync } from "child_process";

const links: {
[key: string]: {
Expand Down Expand Up @@ -49,6 +49,19 @@ export class LegendaryInstaller {
console.log(`[Legendary Installer] ${str}`);
}

public addToPath() {
LegendaryInstaller.log("Setting path variable...");
if(platform() === "win32") {
execSync(`$env:Path += ";${app.getPath("userData")}"`, {
shell: "powershell.exe"
});
} else {
execSync(`PATH=$PATH:${app.getPath("userData")}"`, {
shell: "/bin/bash"
});
}
}

private fixPermission() {
if(platform() === "win32") return;
LegendaryInstaller.log(`Fixing permission for "${this.getBinaryPath()}"`);
Expand Down Expand Up @@ -89,7 +102,7 @@ export class LegendaryInstaller {
});
}

public isInstalled() {
private isInstalled() {
return fs.existsSync(path.join(app.getPath("userData"), this.link.dest.extracted));
}

Expand Down Expand Up @@ -127,7 +140,15 @@ export class LegendaryInstaller {
});
}

public async install() {
public async init() {
if(!this.isInstalled()) {
await this.install();
}

this.addToPath();
}

private async install() {
await this.download();
if (this.link.unzip) await this.unzip();
return;
Expand Down
6 changes: 1 addition & 5 deletions main/helpers/create-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,8 @@ export default (windowName: string, options: BrowserWindowConstructorOptions): B

// todo: use prepacked legendary client
ipcMain.on("command-handler", (event, id: string, args: string[]) => {
installer.addToPath();
const handlerChannel = `command-handler-response-${id}`;
if(args[0] === "{LegendaryBinaryLocation}") {
args[0] = installer.getBinaryPath();
}

//args[0] = `"${args[0]}"`;

console.log(`exec ${args.join(" ")}...`);

Expand Down
5 changes: 2 additions & 3 deletions renderer/lib/CommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ipcRenderer } from "electron";
import LegendaryConstants from "./legendary/LegendaryConstants";
import { platform } from "os";

interface ICallbackList {
onError?: (data: any) => void;
Expand All @@ -12,8 +12,7 @@ type ResponseType = "error" | "data" | "close" | "pid";
const CommandHandler = {
generateId: () => `${Math.random()}`,
send: async (args: string, callbacks: ICallbackList) => {
console.log(LegendaryConstants.bin);
return await CommandHandler.dispatch(["{LegendaryBinaryLocation}", ...args.split(" ")], callbacks);
return await CommandHandler.dispatch([platform() === "win32" ? "legendary.exe" : "legendary", ...args.split(" ")], callbacks);
},
execSync: (cmd: string) => {
ipcRenderer.send("command-handler-exec-sync", cmd);
Expand Down
24 changes: 0 additions & 24 deletions renderer/lib/legendary/LegendaryConstants.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
import { homedir, platform } from "os";
import path from "path";
import { isPackaged } from 'electron-is-packaged';

const isProd = process.env.NODE_ENV === "production";
const isWindows = platform() === "win32";
const getLegendaryBin = () => {
const fixAsar = (str: string) => {
if(!str.includes("app.asar.unpacked")) {
return str.replace("app.asar", "app.asar.unpacked");
}

return str;
};

const binariesPath =
isProd && isPackaged // the path to a bundled electron app.
? path.join(process.resourcesPath, '/app.asar.unpacked', '/app', '/bin', platform())
: path.join(process.cwd(), '/renderer', '/public', '/bin', platform());

return fixAsar(path.resolve(
path.join(binariesPath, isWindows ? "/legendary.exe" : "/legendary")
));
}

const LegendaryConstants = {
configFilePath: `${homedir()}/.config/legendary`,
bin: getLegendaryBin(),
};

export default LegendaryConstants;

0 comments on commit 29d2b7b

Please sign in to comment.