You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

const config = require('./config')
const axios = require('axios')
const colors = require('colors/safe')
const { Mod } = require('./classes')
class Helpers {
api_url(endpoint) {
return `${config.api_base.endsWith('/') ? config.api_base : config.api_base+'/'}${endpoint.startsWith('/') ? endpoint.substring(1) : endpoint}`
}
async fetch_mod(name) {
try {
const mod = await axios.get(this.api_url(name))
return new Mod(name, mod.data)
} catch(e) {
this.error_out(`Unable to fetch mod ${name} - ${e}`)
}
}
error_out(message) {
console.log(`${colors.red('Fatal Error: ')}${message}`)
process.exit()
}
async install_mod({argv, spinner, install}) {
const mod = await this.fetch_mod(install)
const version = argv.minecraft ? mod.get_version_by_minecraft(argv.minecraft, argv.type) : mod.get_version_by_id(argv.mod_version, argv.type)
spinner.stop()
if ( !version ) this.error_out('Unable to find mod version matching those criteria. To list available versions, use --list.')
console.log('Installing mod version to repository...')
mod.print_header()
version.display()
spinner.color = 'yellow'
spinner.text = 'Download mod file (sorry about this)...'
spinner.start()
await version.wait(2000)
await version.stage_download()
spinner.stop()
spinner.color = 'green'
spinner.text = 'Verifying file staging...'
spinner.start()
if ( !(version.verify_stage()) ) {
spinner.stop()
this.error_out('We were unable to programmatically stage the downloaded JAR for this mod. Sorry.')
} else {
spinner.stop()
console.log(colors.green('File staged successfully!'))
}
spinner.color = 'cyan'
spinner.text = 'Preparing staged file for archive...'
spinner.start()
await version.archive_stage()
spinner.color = 'green'
spinner.text = 'Installing archive to repository...'
await version.install_stage()
spinner.stop()
console.log(colors.green('Archive added to repository.'))
version.print_solder()
}
list_installed_mods() {
const { readdirSync } = require('fs')
const getDirectories = source => readdirSync(source, { withFileTypes: true }).filter(dirent => dirent.isDirectory()).map(dirent => dirent.name)
return getDirectories(`${config.repo}/mods`)
}
list_mod_versions(pkg) {
const { readdirSync } = require('fs')
const getFiles = source => readdirSync(source, { withFileTypes: true }).filter(dirent => dirent.isFile()).map(dirent => dirent.name)
return getFiles(`${config.repo}/mods/${pkg}`)
}
}
module.exports = exports = new Helpers