UNPKG

datapack-manager

Version:
79 lines (76 loc) 2.97 kB
#!/usr/bin/env node import { __awaiter, __asyncValues } from './_virtual/_tslib.js'; import { promises } from 'fs'; import { Datapack } from '@throw-out-error/minecraft-datapack'; import pth$1 from 'path'; import { getMinecraftPath } from './util.js'; const worldCache = new Map(); class World { static fromPath(path) { path = pth$1.resolve(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 __awaiter(this, void 0, void 0, function* () { const packs = []; const packsPath = pth$1.join(this.path, "datapacks"); try { const packsDir = yield promises.opendir(packsPath); try { for (var packsDir_1 = __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 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 __awaiter(this, void 0, void 0, function* () { let packPath = pack; if (pack instanceof Datapack) { packPath = pth$1.join(pack.path, pack.name); } if (typeof packPath !== "string") { throw TypeError("Invalid datapack"); } const newPath = pth$1.join(this.path, "datapacks", pth$1.basename(packPath)); switch (mode) { case "symlink": return promises.symlink(packPath, newPath); case "move": return promises.rename(packPath, newPath); default: throw Error(`install-mode "${mode}" not implemented`); } }); } uninstall(nameOrPath) { return __awaiter(this, void 0, void 0, function* () { const path = pth$1.resolve(this.path, "datapacks", nameOrPath); const stats = yield promises.stat(path); if (stats.isSymbolicLink()) { yield promises.unlink(path); } else if (stats.isDirectory()) { yield promises.rmdir(path, { recursive: true }); } }); } } export default World;