UNPKG

minecraft-modpack

Version:

Minecraft modpack creation made easy

87 lines (75 loc) 2.94 kB
const axios = require("axios").default; const config = require("../config"); const Helpers = require("./helpers"); const chalk = require("chalk"); const fs = require("fs"); const path = require("path"); const inquirer = require("inquirer"); const storage = new ( require("../classes/VariableStorage") )(); const ModFactory = require("../classes/ModFactory"); let ModFactoryI = new ModFactory(); storage.gameConfigPath = path.resolve(process.cwd(), "pack.cfg"); storage.gameConfig = {}; storage.installedMods = []; storage.instance = axios.create({ baseURL: config.base, timeout: 10000, }); module.exports.named = installMod; module.exports.file = installModFile; async function installMod(mod) { if( fs.existsSync(storage.gameConfigPath) ){ storage.gameConfig = JSON.parse(fs.readFileSync(storage.gameConfigPath)); let modURL = config.path.search + `?categoryId=0&gameId=432&gameVersion=${storage.gameConfig.gameVersion}&searchFilter=${mod}&sectionId=6&sort=0`; let modsArr = ( await storage.instance.get(modURL) ).data; let modNames = []; modsArr.map( (modObj, i) => { modNames.push(i + ". " + modObj.name); }); if (modsArr.length == 0) { console.log( chalk.yellow.bold`Nothing found, maybe this mod doesn't exist?` ); return 0; } let modChoicesNames = await inquirer.prompt([ { type: "checkbox", name: "choices", message: chalk.yellow`Mods`, choices: modNames } ]); if (modChoicesNames.choices.length == 0) { console.log(chalk.red`You haven't chosen any mod!`); return 0; } let modChoices = []; ModFactory.passStorage(storage); await Helpers.asyncForEach(modChoicesNames.choices, async modName => { modChoices.push( await ModFactoryI.instance( modsArr[ parseInt(modName.split(".")[0]) ], 1 ) ); }); await Helpers.asyncForEach(modChoices, async modT => { if( !storage.installedMods.includes(modT.id) ){ await modT.download(); } }); } else { console.log(chalk`{red [-]} {yellow Error} No modpack config found (run {cyan modpack init}).`); } return 0; } async function installModFile(mod) { let modID = mod.split("-")[0]; let modStringArr = mod.split("-"); modStringArr.shift(); let fileName = modStringArr.join("-"); storage.gameConfig = JSON.parse(fs.readFileSync(storage.gameConfigPath)); let modObj = (await storage.instance.get( config.path.info(modID) )).data; ModFactory.passStorage(storage); let modInstance = await ModFactoryI.instance(modObj); if( !storage.installedMods.includes(modInstance.id) ){ await modInstance.download(fileName); } return 0; }