@sidekick-coder/db
Version:
Cli Tool to manipulate data from diferent sources
117 lines (111 loc) • 3.44 kB
JavaScript
;
var module$1 = require('module');
var Table = require('cli-table3');
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var Table__default = /*#__PURE__*/_interopDefault(Table);
// src/core/render/defineRender.ts
function defineRender(render) {
return render;
}
var require2 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('console.cjs', document.baseURI).href)));
function formatValue(value) {
const chalk = require2("chalk");
if (typeof value === "boolean") {
return value ? chalk.green("true") : chalk.red("false");
}
if (typeof value === "object") {
return JSON.stringify(value);
}
return value;
}
function general(output = {}) {
const screenWidth = process.stdout.columns || 80;
const table = new Table__default.default({
wordWrap: true,
wrapOnWordBoundary: false,
colWidths: [Math.floor(screenWidth * 0.2), Math.floor(screenWidth * 0.7)]
});
for (const key of Object.keys(output)) {
table.push([key, formatValue(output[key])]);
}
console.log(table.toString());
}
function list(output, columns) {
const chalk = require2("chalk");
const screenWidth = (process.stdout.columns || 80) - 6;
const rows = [];
const head = [];
const data = output.data;
if (columns) {
head.push(...columns);
}
if (!columns) {
data.map((item) => Object.keys(item)).flat().filter((value, index, self) => self.indexOf(value) === index).forEach((key) => {
head.push({
label: key,
value: key
});
});
}
output.data.forEach((item) => {
const row = [];
head.forEach((h) => {
row.push(formatValue(item[h.value]));
});
rows.push(row);
});
head.forEach((h) => {
if (h.width) {
h.width = Number(h.width);
}
if (!h.label) {
h.label = h.value;
}
});
const usedWidth = head.filter((h) => !!h.width).reduce((acc, h) => acc + h.width, 0);
const remainingWidth = 100 - usedWidth;
const withNoWidth = head.filter((h) => !h.width).length;
head.forEach((h) => {
if (!h.width) {
h.width = Math.floor(remainingWidth / withNoWidth);
}
});
head.forEach((h) => {
if (h.width) {
h.realWidth = Math.floor(screenWidth * (h.width / 100));
}
});
const table = new Table__default.default({
head: head.map((h) => chalk.bold(h.label)),
style: {
head: [],
//disable colors in header cells
border: []
//disable colors for the border
},
wordWrap: true,
wrapOnWordBoundary: false,
colWidths: head.map((h) => h.realWidth)
});
table.push(...rows);
general(output.meta);
console.log(table.toString());
}
var consoleRender = defineRender({
name: "console",
render: async ({ method, output, options }) => {
if ((options == null ? void 0 : options.format) === "json") {
console.log(JSON.stringify(output));
return;
}
if (method === "list") {
return list(output, options == null ? void 0 : options.columns);
}
if (Array.isArray(output)) {
return output.forEach((o) => general(o));
}
return general(output);
}
});
exports.consoleRender = consoleRender;