Skip to content

Commit

Permalink
Added uninstall option
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Jan 30, 2022
1 parent eee779b commit d2d527d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
32 changes: 26 additions & 6 deletions src/electron/src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand All @@ -25,7 +26,7 @@ const App = () => {
name: "The Backrooms",
id: "the-backrooms",
Page: TheBackrooms,
isInstalled: true,
isInstalled: false,
},
]);

Expand All @@ -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;
})
);
Expand Down Expand Up @@ -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}
/>
))}
Expand Down
11 changes: 10 additions & 1 deletion src/electron/src/components/Page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Page = ({
onRandomizeBtnClick,
onGenerateBtnClick,
isInstalled,
onUninstallBtnClick,
children,
}) => {
return (
Expand All @@ -25,7 +26,15 @@ const Page = ({
>
<h1>
<span>{title}</span>
{isInstalled && <Badge>installed</Badge>}
{isInstalled && (
<>
<span style={{ flex: 1 }}></span>
<Badge>installed</Badge>
<button className="uninstallBtn" onClick={onUninstallBtnClick}>
Uninstall
</button>
</>
)}
</h1>

<div>
Expand Down
5 changes: 5 additions & 0 deletions src/electron/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,8 @@ button.generate:hover {
border-radius: 5px;
color: white;
}

.uninstallBtn {
margin-left: 10px;
cursor: pointer;
}
22 changes: 14 additions & 8 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -738,4 +743,5 @@ module.exports = {
rotateVec3,
circleOfVectors,
cleanupCache,
uninstall,
};

0 comments on commit d2d527d

Please sign in to comment.