UNPKG

@sidekick-coder/db

Version:

Cli Tool to manipulate data from diferent sources

92 lines (89 loc) 2.44 kB
import { stringify as stringify$1 } from 'yaml'; import Table from 'cli-table3'; // src/utils/yaml.ts var stringify = stringify$1; 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({ 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({ 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({ 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()); } } export { print };