diff options
| author | hachem <im@hachem.wtf> | 2025-12-09 23:02:55 +0100 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-12-09 23:02:55 +0100 |
| commit | 5cc4c5a689e124e3951c80f0cfda0de4a797fec7 (patch) | |
| tree | 7cb412c67f54636a4926161f4c948b68d87e74e8 /cli/commands/clean.js | |
| parent | 50e62e246fef7975866c244e08ead7c681d50349 (diff) | |
[add]: merging and refactor
Diffstat (limited to 'cli/commands/clean.js')
| -rw-r--r-- | cli/commands/clean.js | 80 |
1 files changed, 80 insertions, 0 deletions
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 } + |
