This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
formatter.js
89 lines (73 loc) · 2.03 KB
/
formatter.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
79
80
81
82
83
84
85
86
87
88
89
const path = require('path');
const { countBreaks } = require('grapheme-breaker');
const stripAnsi = require('strip-ansi');
const chalk = require('chalk');
const filesize = require('filesize');
const columns = [{ align: 'left' }, { align: 'right' }, { align: 'right' }];
// Several functions in here were taken from parcel's official Logging class
// https://github.com/parcel-bundler/parcel/blob/2aa72153509a72c0fd247b3e94495071ae61f717/src/Logger.js#L154
function pad(text, length, align = 'left') {
let pad = ' '.repeat(length - stringWidth(text));
if (align === 'right') {
return pad + text;
}
return text + pad;
}
function stringWidth(string) {
return countBreaks(stripAnsi('' + string));
}
function table(table) {
// Measure column widths
let colWidths = [];
table.forEach((row) => {
row.forEach((item, idx) => {
colWidths[idx] = Math.max(colWidths[idx] || 0, stringWidth(item));
})
});
// Render rows
table.forEach((row) => {
let items = row.map((item, i) => {
// Add padding between columns unless the alignment is the opposite to the
// next column and pad to the column width.
let padding =
!columns[i + 1] || columns[i + 1].align === columns[i].align ? 4 : 0;
return pad(item, colWidths[i] + padding, columns[i].align);
});
console.log(items.join(''));
});
}
function sortResults(a, b) {
const aSize = a.size;
const bSize = b.size;
const aFile = a.file;
const bFile = b.file;
if (aSize > bSize) {
return -1;
}
if (aSize < bSize) {
return 1;
}
if (aSize === bSize) {
if (aFile < bFile) {
return -1;
}
if (aFile > bFile) {
return 1;
}
return 0;
}
}
function formatResults(result) {
return [
path.relative(process.cwd(), result.file).replace(/(.+)\/(.+)$/, (_, p1, p2) => chalk.gray(`${p1}/`) + chalk.bold.cyan(p2)),
chalk.bold.magenta(filesize(result.size)),
chalk.bold.green(result.time / 1000 + 's'),
]
}
module.exports = {
table,
pad,
stringWidth,
sortResults,
formatResults,
};