From 29d2b7b6499edfeaee4b3173265c4b970a39d92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1l=20P=C3=A9ter?= Date: Sun, 2 Jan 2022 16:32:04 +0100 Subject: [PATCH] feat: add legendary to before each command --- main/background.ts | 4 +-- main/helpers/LegendaryInstaller.ts | 27 +++++++++++++++++--- main/helpers/create-window.ts | 6 +---- renderer/lib/CommandHandler.ts | 5 ++-- renderer/lib/legendary/LegendaryConstants.ts | 24 ----------------- 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/main/background.ts b/main/background.ts index f511f47..2cb1a45 100644 --- a/main/background.ts +++ b/main/background.ts @@ -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, diff --git a/main/helpers/LegendaryInstaller.ts b/main/helpers/LegendaryInstaller.ts index 53f2b86..56d4fc0 100644 --- a/main/helpers/LegendaryInstaller.ts +++ b/main/helpers/LegendaryInstaller.ts @@ -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]: { @@ -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()}"`); @@ -89,7 +102,7 @@ export class LegendaryInstaller { }); } - public isInstalled() { + private isInstalled() { return fs.existsSync(path.join(app.getPath("userData"), this.link.dest.extracted)); } @@ -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; diff --git a/main/helpers/create-window.ts b/main/helpers/create-window.ts index 7638d28..4ec6793 100644 --- a/main/helpers/create-window.ts +++ b/main/helpers/create-window.ts @@ -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(" ")}...`); diff --git a/renderer/lib/CommandHandler.ts b/renderer/lib/CommandHandler.ts index 7793e10..530a4ab 100644 --- a/renderer/lib/CommandHandler.ts +++ b/renderer/lib/CommandHandler.ts @@ -1,5 +1,5 @@ import { ipcRenderer } from "electron"; -import LegendaryConstants from "./legendary/LegendaryConstants"; +import { platform } from "os"; interface ICallbackList { onError?: (data: any) => void; @@ -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); diff --git a/renderer/lib/legendary/LegendaryConstants.ts b/renderer/lib/legendary/LegendaryConstants.ts index 1a8e2d6..b765627 100644 --- a/renderer/lib/legendary/LegendaryConstants.ts +++ b/renderer/lib/legendary/LegendaryConstants.ts @@ -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; \ No newline at end of file