UNPKG

1.52 kBJavaScriptView Raw
1const chalk = require('chalk')
2const cliui = require('cliui')
3
4const sum = (arr, init = 0) => arr.reduce((prev, cur) => prev + cur, init)
5const uniq = arr => Array.from(new Set(arr))
6const filesize = size => {
7 if (size < 2 ** 10) {
8 return `${size} B`
9 } else {
10 return `${(size / 2 ** 10).toFixed(2)} KB`
11 }
12}
13
14const print = stats => {
15 const {
16 startTime,
17 endTime,
18 compilation: { assets }
19 } = stats
20
21 const ui = cliui({ width: 50 })
22
23 ui.div({
24 text: `${chalk.green('统计:')} 耗时${chalk.cyan(
25 endTime - startTime
26 )}ms`,
27 padding: [1, 0, 1, 0]
28 })
29
30 const printTable = arr => ui.div(...arr.map(v => ({ text: v })))
31
32 const thead = ['类型', '数量', '文件大小'].map(v => chalk.cyan(v))
33 printTable(thead)
34
35 const tbody = Object.keys(assets).reduce((prev, cur) => {
36 prev.push({
37 name: cur,
38 type: cur.split('.').slice(-1)[0],
39 size: assets[cur].size()
40 })
41 return prev
42 }, [])
43 uniq(tbody.map(v => v.type))
44 .map(type => {
45 const files = tbody.filter(v2 => v2.type === type)
46 const { length: len } = files
47 const size = sum(files.map(v => v.size))
48 return { type, len, size }
49 })
50 .sort((a, b) => a.len - b.len)
51 .forEach(({ type, len, size }) => {
52 printTable([type, len, filesize(size)])
53 })
54
55 const tfoot = [
56 '总计',
57 tbody.length,
58 filesize(sum(tbody.map(v => v.size)))
59 ].map(v => chalk.green(v))
60
61 printTable(tfoot)
62
63 console.log(ui.toString())
64}
65
66module.exports = { print }