datapack-manager
Version:
Manage your Minecraft datapacks
121 lines (115 loc) • 3.71 kB
JavaScript
;
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 index = require('../index.js');
var Table = _interopDefault(require('cli-table3'));
function formatResult(result, opts = {}) {
let path = result.path;
if (result.symlink && opts.dereference) {
path = result.symlink;
}
return [
result.name,
opts.desc && result.datapack.description,
opts.path && path
].filter(Boolean);
}
const command = ["list", "l", "ls"];
const desc = "List local datapacks";
function builder(yargs) {
return yargs.options({
description: {
desc: "Print descriptions",
alias: "d",
default: true,
type: "boolean"
},
border: {
type: "boolean",
alias: "b",
desc: "Prints borders around the table",
default: true
},
location: {
type: "boolean",
desc: "Print locations",
default: true
},
dereference: {
type: "boolean",
alias: "L",
desc: "Dereference symlinks",
default: false
}
});
}
const borderlessConfig = {
chars: {
top: "",
"top-mid": "",
"top-left": "",
"top-right": "",
bottom: "",
"bottom-mid": "",
"bottom-left": "",
"bottom-right": "",
left: "",
"left-mid": "",
mid: "",
"mid-mid": "",
right: "",
"right-mid": "",
middle: " "
},
style: {
"padding-left": 0,
"padding-right": 0,
compact: true
}
};
function handler({ description, border, location, dereference }) {
return _tslib.__awaiter(this, void 0, void 0, function* () {
const manager = new index.DatapackManager();
const results = yield manager.search({ installed: true, global: true });
const global = results.filter(r => r.global);
const installed = results.filter(r => r.world);
let head = ["World", "Name"];
if (description)
head.push("Description");
if (location)
head.push("Location");
const tableConfig = {
head,
wordWrap: true
};
if (!border) {
Object.assign(tableConfig, borderlessConfig);
}
const table = new Table(tableConfig);
if (global.length) {
const rows = global.map(r => formatResult(r, { desc: description, path: location, dereference }));
rows[0].unshift({ content: "global", rowSpan: global.length });
table.push(...rows);
}
if (installed.length) {
const worlds = new Map();
installed.forEach(result => {
const results = worlds.get(result.world) || [];
results.push(result);
worlds.set(result.world, results);
});
for (let [world, results] of worlds) {
const rows = results.map(r => formatResult(r, { desc: description, path: location, dereference }));
rows[0].unshift({ content: world, rowSpan: results.length });
table.push(...rows);
}
}
console.log(table.toString());
});
}
exports.builder = builder;
exports.command = command;
exports.desc = desc;
exports.handler = handler;