mcrp-util
Version:
A one-stop shop for all things Resource Pack, the Texture Pack replacement since 1.6-pre
62 lines (54 loc) • 1.79 kB
JavaScript
const Zip = require('node-stream-zip')
const { basename, join } = require('path')
const { readdir } = require('fs')
const { printPack, notice } = require('./printers')
const _INTERNAL = require('../_internal.json')
module.exports.readPack = async function(path) {
return new Promise((resolve, reject) => {
let filename = basename(path)
if (filename.substring(filename.length - 3) !== 'zip') {
reject('Non-zip Resource Packs are not currently supported.')
}
{
let zip = new Zip({
file: path,
storeEntries: true
})
zip.on('ready', () => {
let mcmeta = JSON.parse(zip.entryDataSync('pack.mcmeta').toString('utf8'))
let output = {
// Take the archive name, drop the extension
name: basename(path).substring(0, basename(path).length - 4),
// Remove color formatting
description: mcmeta.pack.description.replace(/§\d/g, ''),
// Get the version range the pack was designed for
version: _INTERNAL.definitions.packversions[mcmeta.pack.pack_format.toString()]
}
resolve(output)
})
}
})
}
module.exports.listPacks = async function(mcpath) {
let rpPath = join(mcpath, 'resourcepacks', '/')
readdir(rpPath, {
withFileTypes: true
}, async (err, archives) => {
if (err) throw err;
let packCount = 0
for (let i = 0; i < archives.length; i++) {
let archive = archives[i]
let pack = archive.name
if (pack.endsWith('.zip')) {
packCount ++
let packpath = join(rpPath, pack)
let pushval = await module.exports.readPack(packpath)
printPack(pushval)
console.log('')
}
}
if (packCount === 0) {
notice('No Resource Packs installed.')
}
})
}