@contentstack/cli-utilities
Version:
Utilities for contentstack projects
111 lines (110 loc) • 4.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const tty_table_1 = tslib_1.__importDefault(require("tty-table"));
const yaml = tslib_1.__importStar(require("js-yaml"));
const papaparse_1 = require("papaparse");
const core_1 = require("@oclif/core");
const _1 = require(".");
class CLITable {
/** Returns CLI table flags */
static getTableFlags(columns = ['columns', 'sort', 'filter', 'csv', 'no-truncate']) {
const flags = {
columns: core_1.Flags.string({
description: 'Specify columns to display, comma-separated.',
helpGroup: 'TABLE',
}),
sort: core_1.Flags.string({
description: 'Sort the table by a column. Use "-" for descending.',
helpGroup: 'TABLE',
}),
filter: core_1.Flags.string({
description: 'Filter rows by a column value (e.g., name=foo).',
helpGroup: 'TABLE',
}),
csv: core_1.Flags.boolean({
description: 'Output results in CSV format.',
helpGroup: 'TABLE',
}),
'no-truncate': core_1.Flags.boolean({
description: 'Prevent truncation of long text in columns.',
helpGroup: 'TABLE',
}),
'no-header': core_1.Flags.boolean({
description: 'Hide table headers in output.',
helpGroup: 'TABLE',
}),
output: core_1.Flags.string({
options: ['csv', 'json', 'yaml'],
description: 'Specify output format: csv, json, or yaml.',
helpGroup: 'TABLE',
}),
};
// Return only requested flags
return Object.entries(flags)
.filter(([key]) => columns.includes(key))
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
}
static render(headers, data, flags, options) {
let tableData = [...data];
if (flags) {
// **Filter Data**
if (flags.filter) {
const [key, value] = flags.filter.split('=');
tableData = tableData.filter((row) => { var _a; return (_a = row[key]) === null || _a === void 0 ? void 0 : _a.toString().toLowerCase().includes(value.toLowerCase()); });
}
// **Select Specific Columns**
if (flags.columns) {
const selectedColumns = flags.columns.split(',');
headers = headers.filter((header) => selectedColumns.includes(header.value));
tableData = tableData.map((row) => selectedColumns.reduce((acc, key) => {
if (row[key] !== undefined) {
acc[key] = row[key];
}
return acc;
}, {}));
}
// **Sort Data**
if (flags.sort) {
const sortKey = flags.sort.replace('-', '');
const descending = flags.sort.startsWith('-');
tableData.sort((a, b) => {
if (a[sortKey] < b[sortKey])
return descending ? 1 : -1;
if (a[sortKey] > b[sortKey])
return descending ? -1 : 1;
return 0;
});
}
// **Format Output**
if (flags.output) {
switch (flags.output) {
case 'json':
console.log(JSON.stringify(tableData, null, 2));
return;
case 'yaml':
console.log(yaml.dump(tableData));
return;
case 'csv':
console.log((0, papaparse_1.parse)(tableData));
return;
}
}
if (flags.csv) {
console.log((0, papaparse_1.parse)(tableData));
}
}
// **Render Table**
const config = {
truncate: !(flags === null || flags === void 0 ? void 0 : flags['no-header']),
borderStyle: 'solid',
paddingBottom: 0,
showHeader: !(flags === null || flags === void 0 ? void 0 : flags['no-header']),
};
_1.cliux.print((0, tty_table_1.default)(headers, tableData, config).render());
}
}
exports.default = CLITable;