UNPKG

datapack-manager

Version:
153 lines (147 loc) 6.36 kB
#!/usr/bin/env node 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _tslib = require('./_virtual/_tslib.js'); var fs = require('fs'); var fs__default = _interopDefault(fs); var pth = _interopDefault(require('path')); var util = require('./util.js'); var world = require('./world.js'); var config = require('./config.js'); class DatapackManager { install(nameOrPathOrPack, world$1, opts) { return _tslib.__awaiter(this, void 0, void 0, function* () { if (typeof world$1 === "string") { world$1 = world.default.fromPath(world$1); } let pack = nameOrPathOrPack; if (typeof nameOrPathOrPack === "string") { pack = pth.resolve(config.default.global, nameOrPathOrPack); } return world$1.install(pack, opts); }); } uninstall(nameOrPath, world$1) { return _tslib.__awaiter(this, void 0, void 0, function* () { if (typeof world$1 === "string") { world$1 = new world.default(world$1); } return world$1.uninstall(nameOrPath); }); } search({ name, world, here = !world, global = !world, installed = !world } = {}) { var e_1, _a; return _tslib.__awaiter(this, void 0, void 0, function* () { const dirs = []; if (here) { dirs.push({ dir: "./" }); } if (global) { dirs.push({ global: global, dir: pth.join(util.getMinecraftPath(), "datapacks") }); } if (world) { dirs.push({ world: pth.basename(world), dir: pth.resolve(util.getMinecraftPath(), "saves", world) }); } if (installed) { const saves = pth.join(util.getMinecraftPath(), "saves"); let worlds = null; try { worlds = yield fs.promises.opendir(saves); } catch (e) { } if (worlds) { try { for (var worlds_1 = _tslib.__asyncValues(worlds), worlds_1_1; worlds_1_1 = yield worlds_1.next(), !worlds_1_1.done;) { let { name: world } = worlds_1_1.value; dirs.push({ world, dir: pth.join(saves, world, "datapacks") }); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (worlds_1_1 && !worlds_1_1.done && (_a = worlds_1.return)) yield _a.call(worlds_1); } finally { if (e_1) throw e_1.error; } } } } if (typeof name === "string") { const results = yield Promise.all(dirs.map((r) => _tslib.__awaiter(this, void 0, void 0, function* () { const path = pth.join(r.dir, name); let datapack; let symlink; try { datapack = yield util.datapackFromPath(path); const stats = yield fs.promises.lstat(path); symlink = stats.isSymbolicLink(); if (symlink) { symlink = yield fs.promises.readlink(path); } } catch (e) { return false; } return Object.assign(Object.assign({}, r), { name, path, datapack, symlink }); }))); return results.filter(Boolean); } const promises = yield Promise.all(dirs.map((r) => _tslib.__awaiter(this, void 0, void 0, function* () { var e_2, _b; let dir = null; try { dir = yield fs.promises.opendir(r.dir); } catch (e) { } if (!dir) return false; const results = []; try { for (var dir_1 = _tslib.__asyncValues(dir), dir_1_1; dir_1_1 = yield dir_1.next(), !dir_1_1.done;) { let { name: pack } = dir_1_1.value; if (name instanceof RegExp && !name.test(pack)) continue; const path = pth.join(r.dir, pack); results.push(util.datapackFromPath(path) .then((datapack) => _tslib.__awaiter(this, void 0, void 0, function* () { const stats = yield fs.promises.lstat(path); let symlink = stats.isSymbolicLink(); if (symlink) { symlink = yield fs.promises.readlink(path); } return Object.assign(Object.assign({}, r), { name: pack, path, datapack, symlink }); })) .catch(() => false)); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (dir_1_1 && !dir_1_1.done && (_b = dir_1.return)) yield _b.call(dir_1); } finally { if (e_2) throw e_2.error; } } return results; }))); const results = yield Promise.all(promises.flat()); return results.filter(Boolean); }); } } exports.DatapackManager = DatapackManager;