From fb9065408f7904c05f093abcc5f8930430c17358 Mon Sep 17 00:00:00 2001 From: Fhilipe Coelho Date: Sun, 14 Apr 2024 13:30:53 -0300 Subject: [PATCH] refactor: changed if elses ti early return --- src/main/events/library/open-game.ts | 66 +++++++++----------- src/main/events/misc/generate-lutris-yaml.ts | 24 +++---- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/main/events/library/open-game.ts b/src/main/events/library/open-game.ts index 9ac38950..9b55348c 100644 --- a/src/main/events/library/open-game.ts +++ b/src/main/events/library/open-game.ts @@ -3,7 +3,7 @@ import { generateYML } from "../misc/generate-lutris-yaml"; import path from "node:path"; import fs from "node:fs"; import { writeFile } from "node:fs/promises"; -import { spawn, spawnSync } from "node:child_process"; +import { spawnSync, exec } from "node:child_process"; import { registerEvent } from "../register-event"; import { shell } from "electron"; @@ -15,48 +15,42 @@ const openGame = async ( ) => { const game = await gameRepository.findOne({ where: { id: gameId } }); - if (!game) return; + if (!game) return true; const gamePath = path.join( game.downloadPath ?? (await getDownloadsPath()), game.folderName ); - if (fs.existsSync(gamePath)) { - const setupPath = path.join(gamePath, "setup.exe"); - console.log(setupPath); - if (fs.existsSync(setupPath)) { - if (process.platform === "win32") { - shell.openExternal(setupPath); - } - if (process.platform === "linux") { - if (spawnSync('which', ['lutris']).status === 0) { - const ymlPath = path.join(gamePath, "setup.yml"); - console.log(ymlPath) - await writeFile(ymlPath, generateYML(game)); - const lutris = spawn('lutris', ['--install', `"${ymlPath}"`], { - shell: true, - detached: true, - stdio: 'ignore' - }); - lutris.unref(); - } else { - const wine = spawn('wine', [`"${setupPath}"`], { - shell: true, - detached: true, - stdio: 'ignore' - }); - wine.unref(); - } - } - } else { - shell.openPath(gamePath); - } - } else { - await gameRepository.delete({ - id: gameId, - }); + if (!fs.existsSync(gamePath)) { + await gameRepository.delete({ id: gameId, }); + return true; } + + const setupPath = path.join(gamePath, "setup.exe"); + if (!fs.existsSync(setupPath)) { + shell.openPath(gamePath); + return true; + } + + if (process.platform === "win32") { + shell.openExternal(setupPath); + return true; + } + + if (spawnSync("which", ["lutris"]).status === 0) { + const ymlPath = path.join(gamePath, "setup.yml"); + await writeFile(ymlPath, generateYML(game)); + exec(`lutris --install "${ymlPath}"`); + return true; + } + + if (spawnSync("which", ["wine"]).status === 0) { + exec(`wine "${setupPath}"`); + return true; + } + + return false; }; registerEvent(openGame, { diff --git a/src/main/events/misc/generate-lutris-yaml.ts b/src/main/events/misc/generate-lutris-yaml.ts index d294ea6c..2b6bfed6 100644 --- a/src/main/events/misc/generate-lutris-yaml.ts +++ b/src/main/events/misc/generate-lutris-yaml.ts @@ -1,21 +1,21 @@ -import { Document as YMLDocument } from 'yaml'; -import { Game } from '@main/entity'; +import { Document as YMLDocument } from "yaml"; +import { Game } from "@main/entity"; import path from "node:path"; -export function generateYML(game: Game) { - const slugfiedGameTitle = game.title.replace(/\s/g, '-').toLocaleLowerCase(); +export const generateYML = (game: Game) => { + const slugifiedGameTitle = game.title.replace(/\s/g, "-").toLocaleLowerCase(); const doc = new YMLDocument({ name: game.title, - game_slug: slugfiedGameTitle, - slug: `${slugfiedGameTitle}-installer`, - version: 'Installer', - runner: 'wine', + game_slug: slugifiedGameTitle, + slug: `${slugifiedGameTitle}-installer`, + version: "Installer", + runner: "wine", script: { game: { - prefix: '$GAMEDIR', - arch: 'win64', - working_dir: '$GAMEDIR' + prefix: "$GAMEDIR", + arch: "win64", + working_dir: "$GAMEDIR" }, installer: [{ task: { @@ -25,7 +25,7 @@ export function generateYML(game: Game) { } }, { task: { - executable: path.join(game.downloadPath, game.folderName, 'setup.exe'), + executable: path.join(game.downloadPath, game.folderName, "setup.exe"), name: "wineexec", prefix: "$GAMEDIR" }