-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (106 loc) · 4.21 KB
/
index.js
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
139
140
141
142
#!/usr/bin/env node
import chalk from "chalk"
import chalkAnimation from "chalk-animation"
import gradient from "gradient-string"
import inquirer from "inquirer"
import { createSpinner } from "nanospinner"
import figlet from "figlet"
import path from "path"
import fs from "fs"
const mcModsPath = path.join(process.env.APPDATA, "\\.minecraft\\mods")
const dataPath = path.join(mcModsPath, "mcmodman.json")
const getData = () => JSON.parse(fs.readFileSync(dataPath))
const setData = (data) => fs.writeFileSync(dataPath, JSON.stringify(data))
const applyRandomGradient = (string, gradients) => chalk.bold(gradients[Math.floor(Math.random() * gradients.length)](string))
const sleep = (ms = 2000) => new Promise(resolve => setTimeout(resolve, ms))
const intro = async () => console.log(gradient.summer.multiline(figlet.textSync("Minecraft Mod Manager")))
function createNecessaryFolders() {
Array.of("Forge", "Fabric").forEach(modLoader => {
if(!fs.existsSync(path.join(mcModsPath, modLoader))) {
fs.mkdirSync(path.join(mcModsPath, modLoader))
}
})
if(!fs.existsSync(dataPath)) {
setData({})
}
}
async function outro() {
const spinner = createSpinner("Saving changes...").start()
await sleep()
spinner.success({ text: "Changes saved successfully." })
}
async function askModLoader() {
const modLoaderPrompt = await inquirer.prompt({
name: "modLoader",
type: "list",
message: "Which mod-loader do you want to use?",
choices: ["Forge", "Fabric"]
})
return modLoaderPrompt.modLoader
}
async function askVersion() {
const versionPrompt = await inquirer.prompt({
name: "version",
type: "input",
message: "Which game version do you want to play?"
})
return versionPrompt.version
}
async function askMods(modLoader, version) {
const modsPath = path.join(mcModsPath, modLoader, version)
const data = getData()
const redGradients = [gradient.morning, gradient.passion, gradient.instagram]
if(data.modLoader == modLoader && data.version == version) {
console.log(`You are already using modloader ${applyRandomGradient(modLoader, redGradients)} and version ${applyRandomGradient(version, redGradients)}!`)
return null
}
else if(fs.existsSync(modsPath)) {
const mods = fs.readdirSync(modsPath)
?.filter(file => path.extname(file) == ".jar")
?.map(file => path.basename(file))
if(mods && mods.length > 0) {
const modsPrompt = await inquirer.prompt({
name: "mods",
type: "checkbox",
message: "Which mods do you want to use?",
choices: mods
})
return modsPrompt.mods
}
}
console.log(`No mods found with modloader ${applyRandomGradient(modLoader, redGradients)} and version ${applyRandomGradient(version, redGradients)}.`)
return null
}
async function askConfirm(modLoader, version, mods) {
const confirmPrompt = await inquirer.prompt({
name: "confirmation",
type: "confirm",
message: `${gradient.cristal("Confirm")}\n mod-loader: ${modLoader}\n version: ${version}\n mods: ${mods.join(", ")}`
})
return confirmPrompt.confirmation
}
function moveOldMods() {
const { modLoader, version } = getData()
fs.readdirSync(mcModsPath)
.filter(mod => path.extname(mod) == ".jar")
.forEach(mod => fs.renameSync(path.join(mcModsPath, mod), path.join(mcModsPath, modLoader, version, mod)))
}
function moveNewMods(mods, modLoader, version) {
mods.forEach(mod => fs.renameSync(path.join(mcModsPath, modLoader, version, mod), path.join(mcModsPath, mod)))
const data = getData()
data.modLoader = modLoader
data.version = version
setData(data)
}
createNecessaryFolders()
await intro()
const modLoader = await askModLoader()
const version = await askVersion()
const mods = await askMods(modLoader, version)
if(mods) {
if(await askConfirm(modLoader, version, mods)) {
moveOldMods()
moveNewMods(mods, modLoader, version)
outro()
}
}