Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
Cleanup mods on update
Browse files Browse the repository at this point in the history
  • Loading branch information
Raicuparta committed Jan 8, 2022
1 parent 6b45061 commit b58c816
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/services/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ export function deleteFolder(folderPath: string) {
}
}

export function deleteFolderExcept(folderPath: string, pathsToKeep: string[]) {
if (!fs.existsSync(folderPath)) {
throw new Error(`${modsText.deleteNonExistingError}: "${folderPath}"`);
}

const files = fs.readdirSync(folderPath);

files.forEach((file) => {
const fileDir = path.join(folderPath, file);

if (!pathsToKeep.includes(file)) {
fs.removeSync(fileDir);
}
});
}

export async function unzipRemoteFile(
url: string,
destinationPath: string,
Expand Down
22 changes: 21 additions & 1 deletion app/services/mods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { shell, remote } from 'electron';

import { modsText, globalText } from '../helpers/static-text';
import { getConfig, saveConfig } from './mod-config';
import { unzipRemoteFile, deleteFolder, openDirectory } from './files';
import {
unzipRemoteFile,
deleteFolder,
openDirectory,
deleteFolderExcept,
} from './files';

export function isInstalled(mod: Mod): boolean {
if (!mod) {
Expand All @@ -23,11 +28,26 @@ export function isOutdated(mod: Mod): boolean {
return mod.remoteVersion !== mod.localVersion;
}

export function cleanup(mod: Mod) {
if (!mod.modPath) return;

deleteFolderExcept(
mod.modPath,
mod.uniqueName === 'Alek.OWML'
? ['Mods', 'OWML.Config.json', 'OWML.Manifest.json']
: ['config.json', 'save.json', 'manifest.json']
);
}

export async function install(mod: Mod, onProgress: ProgressHandler) {
if (!mod.downloadUrl) {
return;
}

if (mod.localVersion) {
cleanup(mod);
}

await unzipRemoteFile(mod.downloadUrl, mod.modPath, onProgress);
}

Expand Down

0 comments on commit b58c816

Please sign in to comment.