UNPKG

@sidekick-coder/db

Version:

Cli Tool to manipulate data from diferent sources

98 lines (92 loc) 2.63 kB
'use strict'; var yaml = require('yaml'); var Table = require('cli-table3'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var Table__default = /*#__PURE__*/_interopDefault(Table); // src/utils/yaml.ts var stringify = yaml.stringify; function parse(value) { if (typeof value === "string" || typeof value === "number") { return String(value); } if (value === void 0) { return ""; } if (Array.isArray(value)) { return JSON.stringify(value); } if (typeof value === "object") { return JSON.stringify(value); } return value; } function printTableVertical(data) { const screenWidth = process.stdout.columns || 80; for (const item of data) { 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(item)) { table.push([key, parse(item[key])]); } console.log(table.toString()); } } function printList(data) { const header = []; const rows = []; data.forEach((item) => { Object.keys(item).forEach((key) => { if (!header.includes(key)) { header.push(key); } }); const row = []; header.forEach((key) => { row.push(parse(item[key])); }); rows.push(row); }); const screenWidth = process.stdout.columns ? process.stdout.columns - 5 : 80; const table = new Table__default.default({ head: header, wordWrap: true, wrapOnWordBoundary: false, colWidths: new Array(header.length).fill(Math.floor(screenWidth / header.length)), rowHeights: [2] }); rows.forEach((row) => table.push(row)); console.log(table.toString()); } function print(data, options) { const output = (options == null ? void 0 : options.format) || "table"; if (output === "json") { console.log(JSON.stringify(data)); return; } if (output === "yaml") { console.log(stringify(data)); return; } if (Array.isArray(data) && (options == null ? void 0 : options.vertical)) { return printTableVertical(data); } if (Array.isArray(data)) { return printList(data); } if (typeof data === "object") { 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(data || {})) { table.push([key, parse(data[key])]); } console.log(table.toString()); } } exports.print = print;