From d2d527dd31d98284a1d913afc8bb471f6d37fcf5 Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Sun, 30 Jan 2022 19:59:47 +0100 Subject: [PATCH] Added uninstall option --- src/electron/src/components/App.jsx | 32 ++++++++++++++++++++++------ src/electron/src/components/Page.jsx | 11 +++++++++- src/electron/static/style.css | 5 +++++ src/helpers.js | 22 ++++++++++++------- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/electron/src/components/App.jsx b/src/electron/src/components/App.jsx index a6dbd2b1..71f50109 100644 --- a/src/electron/src/components/App.jsx +++ b/src/electron/src/components/App.jsx @@ -1,3 +1,4 @@ +import fs from "fs"; import React, { useState, useEffect } from "react"; import AliasNightmare from "../projects/AliasNightmare.jsx"; import TheBackrooms from "../projects/TheBackrooms.jsx"; @@ -6,7 +7,7 @@ import MenuItem from "./MenuItem.jsx"; import path from "path"; import seedrandom from "seedrandom"; import { ipcRenderer } from "electron"; -import { cleanupCache } from "../../../helpers.js"; +import { cleanupCache, uninstall } from "../../../helpers.js"; import aliasNightmare from "../../../projects/alias-nightmare/index.js"; import theBackrooms from "../../../projects/backrooms/index.js"; import { compileFTS, compileLLF, compileDLF } from "../../../compile.js"; @@ -25,7 +26,7 @@ const App = () => { name: "The Backrooms", id: "the-backrooms", Page: TheBackrooms, - isInstalled: true, + isInstalled: false, }, ]); @@ -46,16 +47,21 @@ const App = () => { checkForInstalledMaps(outputDir); }, []); - const checkForInstalledMaps = (folder) => { + const checkForInstalledMaps = async (folder) => { let mapName = null; + try { - mapName = require(`${folder}/manifest.json`).meta.mapName; + const raw = await fs.promises.readFile( + `${folder}/manifest.json`, + "utf-8" + ); + const manifest = JSON.parse(raw); + mapName = manifest.meta.mapName.toLowerCase(); } catch (e) {} setProjects( projects.map((project) => { - project.isInstalled = - project.name.toLowerCase() === mapName.toLowerCase(); + project.isInstalled = project.name.toLowerCase() === mapName; return project; }) ); @@ -169,6 +175,20 @@ const App = () => { onSeedChange={(e) => setSeed(e.target.value)} onRandomizeBtnClick={onRandomizeBtnClick} onGenerateBtnClick={onGenerateBtnClick} + onUninstallBtnClick={async () => { + setIsLoading(true); + setLoadingText("Uninstalling level"); + setLoadingProgressbarPercent(0); + setIsLoadingDoneBtnVisible(false); + setTimeout(async () => { + await uninstall(outputDir); + + setLoadingText("Done!"); + setLoadingProgressbarPercent(100); + setIsLoadingDoneBtnVisible(true); + checkForInstalledMaps(outputDir); + }, 100); + }} isInstalled={isInstalled} /> ))} diff --git a/src/electron/src/components/Page.jsx b/src/electron/src/components/Page.jsx index 85d26608..ba4c14e9 100644 --- a/src/electron/src/components/Page.jsx +++ b/src/electron/src/components/Page.jsx @@ -14,6 +14,7 @@ const Page = ({ onRandomizeBtnClick, onGenerateBtnClick, isInstalled, + onUninstallBtnClick, children, }) => { return ( @@ -25,7 +26,15 @@ const Page = ({ >

{title} - {isInstalled && installed} + {isInstalled && ( + <> + + installed + + + )}

diff --git a/src/electron/static/style.css b/src/electron/static/style.css index a26a54a0..79353da9 100644 --- a/src/electron/static/style.css +++ b/src/electron/static/style.css @@ -231,3 +231,8 @@ button.generate:hover { border-radius: 5px; color: white; } + +.uninstallBtn { + margin-left: 10px; + cursor: pointer; +} diff --git a/src/helpers.js b/src/helpers.js index 4a61bf8d..281ed0cd 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -307,6 +307,18 @@ const generateBlankMapData = (config) => { return compose(addOriginPolygon)(mapData); }; +const uninstall = async (dir) => { + try { + const manifest = require(`${dir}/manifest.json`); + for (let file of manifest.files) { + try { + await fs.promises.rm(file); + } catch (f) {} + } + await fs.promises.rm(`${dir}/manifest.json`); + } catch (e) {} +}; + const saveToDisk = async (mapData) => { const { levelIdx } = mapData.config; @@ -320,14 +332,7 @@ const saveToDisk = async (mapData) => { await fs.promises.rm("dist", { recursive: true }); } catch (e) {} } else { - try { - const manifest = require(`${outputDir}/manifest.json`); - for (let filename of manifest.files) { - try { - await fs.promises.rm(`${outputDir}/${filename}`); - } catch (f) {} - } - } catch (e) {} + await uninstall(outputDir); } let scripts = exportScripts(outputDir); @@ -738,4 +743,5 @@ module.exports = { rotateVec3, circleOfVectors, cleanupCache, + uninstall, };