const fs = require("fs-extra") const path = require("path") const { LoadConfig } = require("../../lib/config") const { serverDir } = require("./generate") const config = LoadConfig() const { backup_location } = config const CleanGenerated = async () => { try { if (await fs.pathExists(serverDir)) { await fs.remove(serverDir) console.log(" Removed server directory.") } else { console.log(" Server directory doesn't exist.") } } catch (err) { console.error(" Failed to remove server directory:", err.message) } } const CleanLatest = async () => { if (!backup_location) { console.log(" Error: backup_location must be set in config.") return } if (!await fs.pathExists(backup_location)) { console.log(" Backup directory doesn't exist.") return } try { const files = await fs.readdir(backup_location) const zipFiles = files .filter(f => f.startsWith("backup_") && f.endsWith(".zip")) .sort() .reverse() if (zipFiles.length === 0) { console.log(" No backup files found.") return } const latestZip = zipFiles[0] const latestPath = path.join(backup_location, latestZip) await fs.remove(latestPath) console.log(` Removed latest backup: ${latestZip}`) } catch (err) { console.error(" Failed to remove latest backup:", err.message) } } const CleanAll = async () => { console.log(" Cleaning generated files...") await CleanGenerated() console.log("") console.log(" Cleaning latest backup...") await CleanLatest() } module.exports = { CleanGenerated, CleanLatest, CleanAll }