Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
- Add `setup`, `install`, `remove`, and `update`.
  • Loading branch information
Nixinova committed Apr 3, 2021
0 parents commit 689ac9f
Show file tree
Hide file tree
Showing 9 changed files with 2,104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# General
* text=auto

# Project
package-lock.json -diff
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
*.jar
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 1.0.0
*2021-04-04*
- Added functions and CLI commands `setup`, `install`, `remove`, and `update`.
99 changes: 99 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const fs = require('fs');
const download = require('download');
const curseforge = require('mc-curseforge-api');

const VERSION = '1.0.0';
const MODS_FILE = 'mods.json';

function getConfig() { return JSON.parse(fs.readFileSync(MODS_FILE, { encoding: 'utf8' }, data => data)); }
function getUrlFileName(url) { return url.match(/[^/]+$/)?.[0]; }

async function getMod(source, modID) {
switch (source) {
case 'curseforge': return await curseforge.getMod(+modID);
}
}

async function getModFiles(source, modID) {
switch (source) {
case 'curseforge': return await curseforge.getModFiles(+modID);
}
}

function getValidVersions(versions, mcver) {
const validVers = versions.filter(obj => {
const getMajor = ver => ver.replace(/^(\d+\.\d+).*$/, '$1');
return obj.minecraft_versions.map(ver => getMajor(ver)).includes(getMajor(mcver));
});
return validVers;
}

async function setup(mcver) {
const data = { versions: { minecraft: mcver }, mods: {} };
fs.writeFile(MODS_FILE, JSON.stringify(data, {}, 4), { encoding: 'utf8' }, data => data);
}

async function install(srcArg, modID) {
if (!modID) throw new Error(`Mod host argument is missing. Try 'modmanager install curse ${srcArg}'.`);
const source = /^-*c/.test(srcArg) ? 'curseforge' : srcArg;
const modsJson = getConfig();
const mcver = modsJson.version.minecraft;
const modinfo = await getMod(source, modID);
const files = getValidVersions(await getModFiles(source, modID), mcver);
if (!files.length) throw new Error(`No valid ${mcver} versions for mod ${modID} ${modinfo.name}`);
modsJson.mods[modID] = {
name: modinfo.name,
host: source,
url: modinfo.url,
version: getUrlFileName(files[0].download_url),
}
download(files[0].download_url, '.');
fs.writeFile(MODS_FILE, JSON.stringify(modsJson, {}, 4), { encoding: 'utf8' }, data => data);
}

async function remove(modID) {
const modsJson = getConfig();
if (!modsJson.mods[modID]) return;
fs.unlink('./' + modsJson.mods[modID].version, err => err && console.log(err))
delete modsJson.mods[modID];
fs.writeFile(MODS_FILE, JSON.stringify(modsJson, {}, 4), { encoding: 'utf8' }, data => data);
}

async function update() {
const modsJson = getConfig();
const mcver = modsJson.version.minecraft;
for (let [modID, obj] of Object.entries(modsJson.mods)) {
const files = getValidVersions(await curseforge.getModFiles(modID), mcver);
const dlUrl = files[0]?.download_url;
if (!dlUrl || getUrlFileName(dlUrl) !== obj.version) {
install(modID).catch(e => { console.error(e) });
}
}
}

module.exports = { install, remove, update, setup };

const args = process.argv.slice(2);
if (args[0]) {
if (args[0].includes('h')) console.log(`
ModManager commands:
modmanager help
Display this help message.
modmanager setup <mcVersion>
Initialise a 'mods.json' listing file with a given Minecraft version.
modmanager install curse <modID>
Install a mod from CurgeForge, saving its metadata to 'mods.json'.
modmanager remove <modID>
Uninstall a mod and remove its metadata from 'mods.json'.
modmanager update
Update all mods specified in the 'mods.json' file.
modmanager version
Display the current version of ModManager.
`);
else if (args[0].includes('s')) init(args[1]).catch(e => console.error(e));
else if (args[0].includes('i')) install(args[1], args[2]).catch(e => console.error(e));
else if (args[0].includes('r')) remove(args[1]).catch(e => console.error(e));
else if (args[0].includes('u')) update().catch(e => console.error(e));
else if (args[0].includes('v')) console.log(`The current version of ModManager is ${VERSION}`)
}
15 changes: 15 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ISC License

Copyright &copy; 2021 Nixinova

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

The software is provided "as is" and the author disclaims all warranties
with regard to this software including all implied warranties of
merchantability and fitness. In no event shall the author be liable for
any special, direct, indirect, or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether in an
action of contract, negligence or other tortious action, arising out of
or in connection with the use or performance of this software.
13 changes: 13 additions & 0 deletions mods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": {
"minecraft": "1.16"
},
"mods": {
"238222": {
"name": "Just Enough Items (JEI)",
"host": "curseforge",
"url": "https://www.curseforge.com/minecraft/mc-mods/jei",
"version": "jei-1.16.4-7.6.0.56.jar"
}
}
}
Loading

0 comments on commit 689ac9f

Please sign in to comment.