-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
78 lines (65 loc) · 1.86 KB
/
format.js
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
'use strict';
const colors = require('colors/safe');
const semverCmp = require('semver/functions/compare');
const isMap = require('is-map');
const isSet = require('is-set');
const table = require('./table');
function specifierify({ name, version }) {
return `${name}@${version}`;
}
function publisherify({ name, email }) {
return `${name} <${email}>`;
}
function grayBold(x) {
return colors.bold(colors.gray(x));
}
function uniq(x) {
return Array.from(new Set(x));
}
function serializer(key, value) {
if (isSet(value)) {
return Array.from(value);
}
if (isMap(value)) {
return Object.fromEntries(value);
}
return value;
}
function bySize([, v1], [, v2]) {
return v2.size - v1.size;
}
module.exports = function formatOutput(cache, groupBy, json = false) {
if (json) {
const comparator = groupBy === 'package' ? undefined : bySize;
const sorted = Array.from(cache).sort(comparator);
return JSON.stringify(new Map(sorted), serializer, '\t');
}
if (groupBy === 'package') {
const rows = [
['package', 'version/publisher'].map(grayBold),
].concat(Array.from(cache)
.sort(([pkgA], [pkgB]) => pkgA.localeCompare(pkgB))
.map(([pkg, versions]) => [
colors.green(pkg),
Array.from(versions)
.sort(([vA], [vB]) => semverCmp(vB, vA))
.map(([v, p]) => `${colors.green(v)} by ${colors.blue(publisherify(p))}`)
.join('\n'),
]));
return table(rows);
}
if (groupBy !== 'publisher') {
throw new TypeError('invalid groupBy provided');
}
const sorted = Array.from(cache).sort(bySize);
const rows = [
['publisher', 'count', 'packages'].map(grayBold),
].concat(sorted.map(([k, v]) => [
colors.blue(k),
colors.bold(v.size),
colors.green(uniq(Array.from(v.values())
.sort(({ name: a, version: vA }, { name: b, version: vB }) => a.localeCompare(b) || semverCmp(vB, vA))
.map(specifierify)).join('\n')),
]));
return table(rows);
};