import { remove , emptyDir } from "fs-extra" ; import { join as joinPath } from "path" ; import Config from "../utils/Config" ; import { existsSync , readFileSync , writeFileSync } from "fs" ; import Install from "../types/Install" ; export default class InstallManager { param folderName public static installExists ( folderName ) { return existsSync ( joinPath ( Config . readConfigValue ( "installFolder" ) , "installs" , folderName ) ) ; } public static updateInstallDataValue ( folderName , key , value ) { return new Promise ( ( ff , rj ) => { const installDataPath = joinPath ( Config . readConfigValue ( "installFolder" ) , "installs" , folderName , "install.json" ) ; if ( existsSync ( installDataPath ) ) { const data = JSON . parse ( readFileSync ( installDataPath , "utf8" ) ) ; data [ key ] = value ; writeFileSync ( installDataPath , JSON . stringify ( data ) ) ; ff ( ) ; } else { rj ( ) ; } } ) ; } param folderName param newName public static renameInstall ( folderName , newName ) { return InstallManager . updateInstallDataValue ( folderName , "name" , newName ) ; } param folderName public static deleteInstall ( folderName ) { return new Promise ( ( ff , rj ) => { const dirPath = joinPath ( Config . readConfigValue ( "installFolder" ) , "installs" , folderName ) ; if ( existsSync ( dirPath ) ) { remove ( dirPath ) . then ( ( ) => ff ( ) ) . catch ( err => rj ( err ) ) ; } else { rj ( new Error ( "Install does not exist." ) ) ; } } ) ; } public static archiveInstall ( folderName ) { return new Promise ( ( ff , rj ) => { const dirPath = joinPath ( Config . readConfigValue ( "installFolder" ) , "installs" , folderName , "install" ) ; if ( existsSync ( dirPath ) ) { remove ( dirPath ) . then ( ( ) => { InstallManager . updateInstallDataValue ( folderName , "archived" , true ) ; ff ( ) ; } ) . catch ( err => rj ( err ) ) ; } else { rj ( new Error ( "Install does not exist." ) ) ; } } ) ; } param folderName public static deleteSaveData ( folderName ) { return new Promise ( ( ff , rj ) => { const dirPath = joinPath ( Config . readConfigValue ( "installFolder" ) , "installs" , folderName ) ; if ( existsSync ( dirPath ) ) { if ( process . platform === "win32" ) { emptyDir ( joinPath ( dirPath , "appdata" ) ) . then ( ( ) => ff ( ) ) . catch ( err => rj ( err ) ) ; } else if ( process . platform === "darwin" ) { emptyDir ( joinPath ( dirPath , "appdata" , "Library" , "RenPy" ) ) . then ( ( ) => ff ( ) ) . catch ( err => rj ( err ) ) ; } else { emptyDir ( joinPath ( dirPath , "appdata" , ".renpy" ) ) . then ( ( ) => ff ( ) ) . catch ( err => rj ( err ) ) ; } } else { rj ( new Error ( "Install does not exist." ) ) ; } } ) ; } param folderName param category public static setCategory ( folderName , category ) { return InstallManager . updateInstallDataValue ( folderName , "category" , category ) ; } param folderName param exported public static setMonikaExported ( folderName , exported ) { return InstallManager . updateInstallDataValue ( folderName , "monikaExported" , exported ) ; } }