hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/cli/commands/clean.js
blob: 99a96f6394ba3f49d977ccf04092819327b493cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 }