UNPKG

savory

Version:

A command-line interface for operating your Codefresh account

57 lines (52 loc) 2.21 kB
const _ = require('lodash'), fp = require('lodash/fp'), util = require('util'), chalk = require('chalk'), Table = require('cli-table'); const errorFormatter = fp.pipe(fp.partialRight(util.inspect, [{ depth: 10 }]), chalk.red), jsonFormatter = fp.partialRight(JSON.stringify, [null, ' ']), tableFormatter = (function(){ const CONFIG_PARSERS = [ { test: _.isArray, parser: (config)=> ({ title: config.map(_.capitalize), column: config }) }, { test: _.isObject, parser: (config)=> _.defaults(config, config["column"] && { title: config["column"].map(_.capitalize) }, config["title"] && { column: config["title"].map(_.toLower) })}, { test: _.isNil, parser: (config, data)=> ((column)=> ({ title: column.map(_.capitalize), column }))(_(data).map(_.keys).flatten().uniq().value()) } ]; return _.curry((config, data)=> { let configuration = CONFIG_PARSERS.find(({ test })=> test(config)).parser(config, data), table = new Table({ head: configuration["title"].map(fp.unary(chalk.bold)), chars: { "top": "", "top-mid": "", "top-left": "", "top-right": "", "bottom": "", "bottom-mid": "", "bottom-left": "", "bottom-right": "", "left": "", "left-mid": "", "mid": "", "mid-mid": "", "right": "", "right-mid": "", "middle": "" } }); data.forEach((dataItem = [])=> { table.push(!_.isArray(dataItem) ? _.at(dataItem, configuration["column"]).map((data) => data || "") : dataItem.map((data) => data || "") ) }); return table.toString(); }, 2); })(); module.exports = { tableFormatter, errorFormatter, jsonFormatter, };