datapack-manager
Version:
Manage your Minecraft datapacks
146 lines (143 loc) • 6.03 kB
JavaScript
import { __awaiter, __asyncValues } from './_virtual/_tslib.js';
import { promises } from 'fs';
import pth$1 from 'path';
import { getMinecraftPath, datapackFromPath } from './util.js';
import World from './world.js';
import config$1 from './config.js';
class DatapackManager {
install(nameOrPathOrPack, world, opts) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof world === "string") {
world = World.fromPath(world);
}
let pack = nameOrPathOrPack;
if (typeof nameOrPathOrPack === "string") {
pack = pth$1.resolve(config$1.global, nameOrPathOrPack);
}
return world.install(pack, opts);
});
}
uninstall(nameOrPath, world) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof world === "string") {
world = new World(world);
}
return world.uninstall(nameOrPath);
});
}
search({ name, world, here = !world, global = !world, installed = !world } = {}) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
const dirs = [];
if (here) {
dirs.push({
dir: "./"
});
}
if (global) {
dirs.push({
global: global,
dir: pth$1.join(getMinecraftPath(), "datapacks")
});
}
if (world) {
dirs.push({
world: pth$1.basename(world),
dir: pth$1.resolve(getMinecraftPath(), "saves", world)
});
}
if (installed) {
const saves = pth$1.join(getMinecraftPath(), "saves");
let worlds = null;
try {
worlds = yield promises.opendir(saves);
}
catch (e) { }
if (worlds) {
try {
for (var worlds_1 = __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$1.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) => __awaiter(this, void 0, void 0, function* () {
const path = pth$1.join(r.dir, name);
let datapack;
let symlink;
try {
datapack = yield datapackFromPath(path);
const stats = yield promises.lstat(path);
symlink = stats.isSymbolicLink();
if (symlink) {
symlink = yield promises.readlink(path);
}
}
catch (e) {
return false;
}
return Object.assign(Object.assign({}, r), { name,
path,
datapack,
symlink });
})));
return results.filter(Boolean);
}
const promises$1 = yield Promise.all(dirs.map((r) => __awaiter(this, void 0, void 0, function* () {
var e_2, _b;
let dir = null;
try {
dir = yield promises.opendir(r.dir);
}
catch (e) { }
if (!dir)
return false;
const results = [];
try {
for (var dir_1 = __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$1.join(r.dir, pack);
results.push(datapackFromPath(path)
.then((datapack) => __awaiter(this, void 0, void 0, function* () {
const stats = yield promises.lstat(path);
let symlink = stats.isSymbolicLink();
if (symlink) {
symlink = yield 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$1.flat());
return results.filter(Boolean);
});
}
}
export { DatapackManager };