hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-12-09 21:55:02 +0100
committerhachem <im@hachem.wtf>2025-12-09 21:55:02 +0100
commit50e62e246fef7975866c244e08ead7c681d50349 (patch)
treeb863e4b818d5bfa4fbbdcb774351b094d323e3d1 /lib
parentee977e942c8deabde89f7979fc00850922062926 (diff)
[add]: prompt+banner+refactor
Diffstat (limited to 'lib')
-rw-r--r--lib/config.js62
-rw-r--r--lib/modrinth.js69
2 files changed, 131 insertions, 0 deletions
diff --git a/lib/config.js b/lib/config.js
new file mode 100644
index 0000000..9d2ed39
--- /dev/null
+++ b/lib/config.js
@@ -0,0 +1,62 @@
+const path = require("path")
+const fs = require("fs-extra")
+
+const configPath = path.join(__dirname, "../config/config.json")
+const apiPath = path.join(__dirname, "../config/api.json")
+const cachePath = path.join(__dirname, "../cache/mod_versions.json")
+
+const LoadConfig = () =>
+{
+ return require(configPath)
+}
+
+const LoadApi = () =>
+{
+ try
+ {
+ return require(apiPath)
+ }
+ catch
+ {
+ return {}
+ }
+}
+
+const LoadCache = async () =>
+{
+ try
+ {
+ if (await fs.pathExists(cachePath))
+ return JSON.parse(await fs.readFile(cachePath, "utf8"))
+ }
+ catch (err)
+ {
+ console.error("[Cache] Failed to load:", err)
+ }
+ return {}
+}
+
+const SaveCache = async cache =>
+{
+ try
+ {
+ await fs.ensureDir(path.dirname(cachePath))
+ await fs.writeFile(cachePath, JSON.stringify(cache, null, 2))
+ }
+ catch (err)
+ {
+ console.error("[Cache] Failed to save:", err)
+ }
+}
+
+module.exports =
+{
+ configPath,
+ apiPath,
+ cachePath,
+ LoadConfig,
+ LoadApi,
+ LoadCache,
+ SaveCache
+}
+
diff --git a/lib/modrinth.js b/lib/modrinth.js
new file mode 100644
index 0000000..cf0f697
--- /dev/null
+++ b/lib/modrinth.js
@@ -0,0 +1,69 @@
+const axios = require("axios")
+
+const MODRINTH_API = "https://api.modrinth.com/v2"
+
+const GetModVersions = async slug =>
+{
+ try
+ {
+ const response = await axios.get(`${MODRINTH_API}/project/${slug}/version`)
+ return response.data
+ }
+ catch (err)
+ {
+ console.error(`[Modrinth] Failed for ${slug}:`, err.message)
+ return null
+ }
+}
+
+const GetLatestVersion = versions =>
+{
+ if (!versions || !versions.length)
+ return null
+ return versions.sort((a, b) => new Date(b.date_published) - new Date(a.date_published))[0]
+}
+
+const GetCompatibleVersion = (versions, minecraftVersion, loader = null) =>
+{
+ if (!versions || !versions.length)
+ return null
+
+ const compatible = versions.filter(v =>
+ {
+ const hasVersion = v.game_versions.includes(minecraftVersion)
+ const hasLoader = loader ? v.loaders.includes(loader) : true
+ return hasVersion && hasLoader
+ })
+
+ if (!compatible.length)
+ return null
+
+ return compatible.sort((a, b) => new Date(b.date_published) - new Date(a.date_published))[0]
+}
+
+const DownloadFile = async (url, destPath) =>
+{
+ const fs = require("fs-extra")
+
+ try
+ {
+ const response = await axios.get(url, { responseType: "arraybuffer" })
+ await fs.writeFile(destPath, response.data)
+ return true
+ }
+ catch (err)
+ {
+ console.error(`[Download] Failed:`, err.message)
+ return false
+ }
+}
+
+module.exports =
+{
+ MODRINTH_API,
+ GetModVersions,
+ GetLatestVersion,
+ GetCompatibleVersion,
+ DownloadFile
+}
+