-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.js
53 lines (46 loc) · 3.38 KB
/
analysis.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
const {statSync} = require('fs');
const {extname} = require('path');
let fileSummary = [];
function colorLog(s, fg='white', {bg=false, bright=true}={}){ // console.log with color
const colors = {black:30, red:31, green:32, yellow:33, blue:34, magenta:35, cyan:36, white:37};
console.log('\x1b[%s%sm%s\x1b[0m', bright ? '1;' : '', colors[fg] + (bg ? 10 : 0), s);
}
function outliers(arr, sd=2, key=null) {
const values = arr.map(e=>key?e[key]:e).sort((a, b) => a - b);
const mean = values.reduce((s,e)=>s+e) / values.length;
const stdev = Math.sqrt(values.map(e=>(e-mean)**2).reduce((s,e)=>s+e)/values.length);
return arr.filter(e=>(key?e[key]:e) >= mean+sd*stdev).sort((a, b) => (key?b[key]:b) - (key?a[key]:a));
}
function afterFile({src, dst}){
try { fileSummary.push({src, dst, ext: (extname(dst)||'no ext').toUpperCase(), size: statSync(dst).size}) } catch (err) { console.log(err) }
}
function beforeBuild({sdPairs, rmd}){
fileSummary = []
}
function afterBuild({logs}){
const round = (n)=>Math.round((n*10)/1024)/10;
const pluralize = (s,n)=>n == 1 ? s : s+'s';
const spaced = (s,ml,b="\t")=>`${s} ${' '.repeat(ml-String(s).length)}${b}`;
const totalSize = fileSummary.reduce((s,e)=>s+e.size, 0);
const extSize = fileSummary.reduce((s,e)=>{s[e.ext] = s[e.ext] ? s[e.ext]+e.size : e.size; return s}, {});
const extCount = fileSummary.reduce((s,e)=>{s[e.ext] = s[e.ext] ? s[e.ext]+1 : 1; return s}, {});
const extSum = Object.entries(extCount).map(([k,v])=>({ext:k, count:v, size:extSize[k]})).sort((a,b)=>b.size-a.size)
const extSumOut = outliers(extSum, 1, 'size');
colorLog('⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼ sizes ⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼', 'white');
const mext = Math.max(...extSum.map(e=>e.ext.length)), msize = Math.max(...extSum.map(e=>String(e.size).length));
extSum.forEach(e=>colorLog(`${spaced(e.ext,mext)} ${spaced(round(e.size),msize,' ')}kb\tin ${e.count} ${pluralize('file', e.count)}`, extSumOut.indexOf(e)!=-1 ? 'yellow' : 'green', {bright:false}))
colorLog(`${spaced('TOTAL',mext)} ${spaced(round(totalSize),msize,' ')}kb\tin ${fileSummary.length} ${pluralize('file', fileSummary.length)}`, 'white');
const errors = logs.filter(e=>e.error);
const timesinks = outliers(logs, 2, 'time');
const heavyweights = outliers(fileSummary, 2, 'size');
if(errors.length || timesinks.length || heavyweights.length){
colorLog('⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼ problems ⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼', 'white');
errors.map(e=>colorLog(`💀 ${e.src} 🔎 ${e.error}`, 'red'));
timesinks.map(e=>colorLog(`⏳ ${e.src} ${e.time} ms is slower than others`, 'yellow'));
heavyweights.map(e=>colorLog(`🐘 ${e.src} ${round(e.size)} kb is bigger than others`, 'yellow', {bright: false}));
}else{
colorLog('✅ no problems detected', 'green');
}
colorLog('⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼', 'white');
}
module.exports = { beforeBuild, afterBuild, afterFile }