Repository navigation
Expand file tree
/
Copy pathtable.output.js
More file actions
107 lines (89 loc) · 3.3 KB
/
Copy pathtable.output.js
File metadata and controls
107 lines (89 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const CFError = require('cf-errors'); // eslint-disable-line
const Entity = require('../../logic/entities/Entity');
const _ = require('lodash');
const columnify = require('columnify');
const moment = require('moment');
const { Formatter } = require('../../output/Formatter');
const { FormatterRegistry } = require('../../output/formatters');
const Style = require('../../output/Style');
const CliConfigManager = require('../../logic/cli-config/Manager');
const NO_RESOURCES_MESSAGE = 'no available resources';
const STYLED_NO_RESOURCES_MESSAGE = Style.yellow.prependedBy(Style.red('▸'))(NO_RESOURCES_MESSAGE);
const NOOP_FORMATTER = new Formatter();
const prettyOpts = {
headingTransform: Style.bold,
columnSplitter: ' ',
preserveNewLines: true,
};
function _makeColumns(entities, pretty) {
const keys = Object.keys(entities[0]);
const res = [];
let obj = [];
_.forEach(entities, (row) => {
obj = [];
_.forEach(keys, (key) => {
obj[key.toUpperCase()] = row[key];
});
res.push(obj);
});
return columnify(res, pretty ? prettyOpts : {});
}
function output(data, argv) {
const config = CliConfigManager.config();
const {
pretty = config.output.pretty,
output,
selectColumns,
dateFormat = config.output.dateFormat,
} = argv;
if (!data || data.length === 0) {
return pretty ? STYLED_NO_RESOURCES_MESSAGE : NO_RESOURCES_MESSAGE;
}
let entities = _.isArray(data) ? data : [data];
_.forEach(entities, (entity) => {
if (!(entity instanceof Entity)) {
throw new CFError('Cannot extract data for "table" output -- data contains not Entity');
}
});
entities = entities.map((entity) => {
let info = output === 'wide' ? entity.toWide() : entity.toDefault();
if (!_.isEmpty(selectColumns)) {
info = _.pick(info, selectColumns);
}
if (dateFormat && dateFormat !== 'default') {
let dateFormatter;
switch (dateFormat) {
case 'date':
dateFormatter = Style.date;
break;
case 'datetime':
dateFormatter = Style.datetime;
break;
case 'date-diff':
dateFormatter = Style.dateDiff;
break;
default:
dateFormatter = input => input instanceof Date ? moment(input)
.format(dateFormat) : input;
}
info = _.mapValues(info, dateFormatter);
}
// if no pretty flag and no dateFormat - force apply specific entity date format
if (!pretty && (!dateFormat || dateFormat === 'default')) {
const formatter = FormatterRegistry.get(entities[0].constructor.name);
_.keys(info).forEach((key) => {
const prop = info[key];
if (prop instanceof Date) {
info[key] = formatter.applyStyles(key, prop);
}
});
}
if (pretty) {
const formatter = FormatterRegistry.get(entities[0].constructor.name);
info = formatter.apply(info);
}
return info;
});
return _makeColumns(entities, pretty);
}
module.exports = output;