hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/cli/commands/clean.js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/commands/clean.js')
-rw-r--r--cli/commands/clean.js80
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 }
+