diff --git a/app/services/files.ts b/app/services/files.ts index 23650c5..e335592 100644 --- a/app/services/files.ts +++ b/app/services/files.ts @@ -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, diff --git a/app/services/mods.ts b/app/services/mods.ts index 6ccc20f..f4a5016 100644 --- a/app/services/mods.ts +++ b/app/services/mods.ts @@ -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) { @@ -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); }