const col = require('colors/safe') const axios = require('axios') const config = require('./config') class ModVersion { #id = '' #data = {} #mod = {} constructor(id, data, mod) { this.#id = id this.#data = data this.#mod = mod } display() { console.log(` ${col.cyan(this.#data.id)} ${col.blue('('+this.#data.type+')')}\t${col.yellow(new Date(this.#data.uploaded_at).toLocaleString())}\t\t${this.#data.name}`) } wait(ms) { return new Promise(res => { setTimeout(res, ms) }) } id_vers() { return `mc.${this.#data.version}.id.${this.#id}` } async stage_download() { const url = `https://www.curseforge.com/minecraft/mc-mods/${this.#mod.name()}/download/${this.#id}` const responses = [] const br = await (require('puppeteer')).launch({headless: false}) const page = await br.newPage() await page.goto(url); const cookieLink = await page.$(`div[data-tracking-opt-in-accept="true"]`); await this.wait(2000) if ( cookieLink ) await cookieLink.click({clickCount: 1, delay: 500}) const reportLink = await page.$(`a[href="/minecraft/mc-mods/${this.#mod.name()}/download/${this.#id}/file"]`); await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: config.download_staging}); await reportLink.click({ clickCount: 1, delay: 100 }); await this.wait(5000) await br.close() } verify_stage() { return (require('fs')).existsSync(`${config.download_staging}/${this.#data.name.replace(/ /g, '+')}`) } async archive_stage() { const fs = require('fs').promises const proc = require('child_process') await fs.mkdir(`${config.download_staging}/mods`) await fs.rename(`${config.download_staging}/${this.#data.name.replace(/ /g, '+')}`, `${config.download_staging}/mods/${this.#data.name.replace(/ /g, '+')}`) await new Promise((res, rej) => { proc.exec(`cd ${config.download_staging} && zip -r ${this.#mod.name()}-${this.id_vers()}.zip ./mods && rm -rf mods`, (err, stout, sterr) => { if ( err ) rej(err) else res() }) }) } async install_stage() { const fs = require('fs') if ( !(await fs.existsSync(`${config.repo}`)) ) await fs.promises.mkdir(config.repo) if ( !(await fs.existsSync(`${config.repo}/mods`))) await fs.promises.mkdir(`${config.repo}/mods`) if ( !(await fs.existsSync(`${config.repo}/mods/${this.#mod.name()}`))) await fs.promises.mkdir(`${config.repo}/mods/${this.#mod.name()}`) await fs.promises.rename(`${config.download_staging}/${this.#mod.name()}-${this.id_vers()}.zip`, `${config.repo}/mods/${this.#mod.name()}/${this.#mod.name()}-${this.id_vers()}.zip`) } print_solder() { console.log(`Update Solder to have the mod: "${col.green(this.#mod.name())}" with version: "${col.blue(this.id_vers())}".`) } } class Mod { #data = {} #cli_name = '' constructor(name, data) { this.#data = data this.#cli_name = name // console.log(data) } data() { return this.#data } name() { return this.#cli_name } version_info(minecraft) { if ( !this.#data.versions ) return return this.#data.versions[minecraft] } get_version_by_minecraft(minecraft, type = false) { let vers = this.version_info(minecraft) if ( !vers || vers.length < 1 ) return; if ( type ) vers = vers.filter(x => x.type === type) if ( vers.length < 1 ) return; const sorted_vers = vers.sort((a, b) => a.id - b.id).reverse() return new ModVersion(sorted_vers[0].id, sorted_vers[0], this) } get_version_by_id(id, type = false) { console.log({id}) let vers = Object.values(this.#data.versions).flat(1) if ( !vers || vers.length < 1 ) return; if ( type ) vers = vers.filter(x => x.type === type) if ( vers.length < 1 ) return const id_vers = vers.filter(x => x.id === id) if ( id_vers.length < 1 ) return; return new ModVersion(id_vers[0].id, id_vers[0], this) } print_header() { console.log(` ${col.green('Mod:')} ${this.#data.title}`) console.log(` ${col.green('CLI Package Name:')} ${this.name()}`) console.log(` ${col.green('URL:')} ${this.#data.urls.curseforge}`) console.log(` ${col.green('Last Fetch:')} ${new Date(this.#data.last_fetch).toLocaleString()}`) } print_info() { console.log(` ${col.green('Mod:')} ${this.#data.title}`) console.log(` ${col.green('CLI Package Name:')} ${this.name()}`) console.log(` ${col.green('URL:')} ${this.#data.urls.curseforge}`) console.log(` ${col.green('Last Fetch:')} ${new Date(this.#data.last_fetch).toLocaleString()}`) console.log(` ${col.green('Supported Versions:')}`) const display_vers = {} const vers_order = [] for ( const version in this.#data.versions ) { display_vers[version] = ` - ${version}:\t ${col.yellow(this.#data.versions[version].reverse()[0].name)}` vers_order.push(version) } vers_order.map( a => a.split('.').map( n => +n+100000 ).join('.') ).sort() .map( a => a.split('.').map( n => +n-100000 ).join('.') ).reverse() .forEach(x => console.log(display_vers[x])) } } module.exports = exports = { Mod }