From 5cc4c5a689e124e3951c80f0cfda0de4a797fec7 Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 9 Dec 2025 23:02:55 +0100 Subject: [add]: merging and refactor --- cli/commands/clean.js | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 cli/commands/clean.js (limited to 'cli/commands/clean.js') diff --git a/cli/commands/clean.js b/cli/commands/clean.js new file mode 100644 index 0000000..99a96f6 --- /dev/null +++ b/cli/commands/clean.js @@ -0,0 +1,80 @@ +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 } + -- cgit v1.3