UNPKG

datapack-manager

Version:
86 lines (80 loc) 3.27 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 minecraftDatapack = require('@throw-out-error/minecraft-datapack'); var pth = _interopDefault(require('path')); var util = require('./util.js'); const worldCache = new Map(); class World { static fromPath(path) { path = pth.resolve(util.getMinecraftPath(), "saves", path); const world = worldCache.get(path); return world || new World(path); } constructor(path) { this.path = path; worldCache.set(path, this); } getDatapacks() { var e_1, _a; return _tslib.__awaiter(this, void 0, void 0, function* () { const packs = []; const packsPath = pth.join(this.path, "datapacks"); try { const packsDir = yield fs.promises.opendir(packsPath); try { for (var packsDir_1 = _tslib.__asyncValues(packsDir), packsDir_1_1; packsDir_1_1 = yield packsDir_1.next(), !packsDir_1_1.done;) { let { name } = packsDir_1_1.value; packs.push(new minecraftDatapack.Datapack(name, packsPath, {})); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (packsDir_1_1 && !packsDir_1_1.done && (_a = packsDir_1.return)) yield _a.call(packsDir_1); } finally { if (e_1) throw e_1.error; } } } catch (e) { } return packs; }); } install(pack, { mode = "symlink" } = {}) { return _tslib.__awaiter(this, void 0, void 0, function* () { let packPath = pack; if (pack instanceof minecraftDatapack.Datapack) { packPath = pth.join(pack.path, pack.name); } if (typeof packPath !== "string") { throw TypeError("Invalid datapack"); } const newPath = pth.join(this.path, "datapacks", pth.basename(packPath)); switch (mode) { case "symlink": return fs.promises.symlink(packPath, newPath); case "move": return fs.promises.rename(packPath, newPath); default: throw Error(`install-mode "${mode}" not implemented`); } }); } uninstall(nameOrPath) { return _tslib.__awaiter(this, void 0, void 0, function* () { const path = pth.resolve(this.path, "datapacks", nameOrPath); const stats = yield fs.promises.stat(path); if (stats.isSymbolicLink()) { yield fs.promises.unlink(path); } else if (stats.isDirectory()) { yield fs.promises.rmdir(path, { recursive: true }); } }); } } exports.default = World;