hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/cli/app.js
blob: 12523583069a895ddc5a6a60ba0910b7a9604d48 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node

const readline = require("readline")

const { LoadConfig } = require("../lib/config")

const { Check } = require("./commands/check")
const { Generate } = require("./commands/generate")
const { Merge } = require("./commands/merge")
const { CleanGenerated, CleanLatest, CleanAll } = require("./commands/clean")
const { ListMods } = require("./commands/list")

const pkg = require("../package.json")
const config = LoadConfig()
const { minecraft_version } = config

const ShowHelp = () =>
{
    console.log(`
  Commands:

    list-mods          List configured mods
    check              Check if all mods are compatible before generating
    generate           Download mods + server, run to generate files
    merge              Backup server, move generated files to server location
    clean generated    Remove server directory
    clean latest       Remove latest backup zip
    clean all          Remove both generated and latest backup
    help               Show this help
    exit               Exit
`)
}

const ProcessCommand = async (input, rl) =>
{
    const parts = input.trim().toLowerCase().split(/\s+/)
    const cmd = parts[0]
    const arg = parts[1]

    switch (cmd)
    {
        case "list-mods":
            ListMods()
            break

        case "check":
            await Check()
            break

        case "generate":
            await Generate(rl)
            break

        case "merge":
            await Merge()
            break

        case "clean":
            if (arg === "generated")
                await CleanGenerated()
            else if (arg === "latest")
                await CleanLatest()
            else if (arg === "all")
                await CleanAll()
            else
                console.log("  Usage: clean generated | clean latest | clean all")
            break

        case "help":
        case "?":
            ShowHelp()
            break

        case "exit":
        case "quit":
        case "q":
            console.log("  Goodbye!\n")
            process.exit(0)

        case "":
            break

        default:
            console.log(`  Unknown command: ${cmd}. Type 'help' for commands.`)
    }
}

const DrawBox = (lines) =>
{
    const maxLen = Math.max(...lines.map(l => l.length))
    const pad = (str) => str + " ".repeat(maxLen - str.length)
    
    const top = "╔" + "═".repeat(maxLen + 2) + "╗"
    const bot = "╚" + "═".repeat(maxLen + 2) + "╝"
    const mid = lines.map(l => "║ " + pad(l) + " ║").join("\n  ")

    return `  ${top}\n  ${mid}\n  ${bot}`
}

const Main = () =>
{
    const title = `BENSON CLI v${pkg.version}`
    const subtitle = `Fabric Server for MC ${minecraft_version}`

    const box = DrawBox([title, subtitle])

    console.log(`
${box}

  Type 'help' for commands.
`)

    const rl = readline.createInterface
    ({
        input: process.stdin,
        output: process.stdout
    })

    const Prompt = () =>
    {
        rl.question("benson> ", async (input) =>
        {
            await ProcessCommand(input, rl)
            Prompt()
        })
    }

    rl.on("close", () =>
    {
        console.log("\n  Goodbye!\n")
        process.exit(0)
    })

    Prompt()
}

Main()