refactor: changed if elses ti early return

This commit is contained in:
Fhilipe Coelho 2024-04-14 13:30:53 -03:00
parent a9f621214b
commit fb9065408f
2 changed files with 42 additions and 48 deletions

View File

@ -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, {

View File

@ -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"
}