UNPKG

@marteye/studio-cli

Version:

CLI for MartEye Studio API

153 lines (149 loc) 4.34 kB
'use strict'; var chalk = require('chalk'); var Table = require('cli-table3'); var yaml = require('js-yaml'); var sync = require('../node_modules/csv-stringify/lib/sync.js'); var jmespath = require('jmespath'); function output(data, options = {}) { const format = options.format || 'json'; // Apply JMESPath query if provided let result = data; if (options.query) { try { result = jmespath.search(data, options.query); } catch (error) { console.error(chalk.red('Invalid JMESPath query:'), error.message); process.exit(2); } } // Format output based on specified format switch (format) { case 'json': outputJson(result, options.noColor); break; case 'yaml': outputYaml(result); break; case 'table': outputTable(result); break; case 'csv': outputCsv(result); break; default: console.error(chalk.red(`Unknown output format: ${format}`)); process.exit(2); } } function outputJson(data, noColor) { const json = JSON.stringify(data, null, 2); if (noColor) { console.log(json); } else { // Simple syntax highlighting for JSON const highlighted = json .replace(/(".*?")/g, chalk.green('$1')) .replace(/:\s*(\d+)/g, ': ' + chalk.yellow('$1')) .replace(/:\s*(true|false)/g, ': ' + chalk.cyan('$1')) .replace(/:\s*(null)/g, ': ' + chalk.gray('$1')); console.log(highlighted); } } function outputYaml(data) { console.log(yaml.dump(data)); } function outputTable(data) { if (!data) { console.log('No data to display'); return; } // Handle single object if (!Array.isArray(data)) { data = [data]; } if (data.length === 0) { console.log('No data to display'); return; } // Get all unique keys from all objects const keys = Array.from(new Set(data.flatMap((obj) => Object.keys(obj)))); // Create table const table = new Table({ head: keys.map(k => chalk.cyan(k)), style: { head: [], border: [] } }); // Add rows data.forEach((item) => { const row = keys.map((key) => { const value = item[key]; if (value === null || value === undefined) { return chalk.gray('null'); } if (typeof value === 'object') { return JSON.stringify(value); } return String(value); }); table.push(row); }); console.log(table.toString()); } function outputCsv(data) { if (!data) { return; } // Handle single object if (!Array.isArray(data)) { data = [data]; } if (data.length === 0) { return; } // Flatten nested objects for CSV const flattenedData = data.map((item) => flatten(item)); const csv = sync.stringify(flattenedData, { header: true, columns: Object.keys(flattenedData[0] || {}) }); console.log(csv); } function flatten(obj, prefix = '') { const result = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { const newKey = prefix ? `${prefix}.${key}` : key; if (obj[key] === null || obj[key] === undefined) { result[newKey] = ''; } else if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) { Object.assign(result, flatten(obj[key], newKey)); } else if (Array.isArray(obj[key])) { result[newKey] = JSON.stringify(obj[key]); } else { result[newKey] = obj[key]; } } } return result; } function success(message) { console.log(chalk.green('✓'), message); } function error(message, details) { console.error(chalk.red('✗'), message); if (details) { console.error(chalk.gray(JSON.stringify(details, null, 2))); } } function warning(message) { console.warn(chalk.yellow('⚠'), message); } exports.error = error; exports.output = output; exports.success = success; exports.warning = warning; //# sourceMappingURL=output.js.map