|
| 1 | +const rightpad = require('right-pad') |
| 2 | +const figures = require('figures') |
| 3 | +const bytes = require('bytes') |
| 4 | +const plur = require('plur') |
| 5 | + |
| 6 | +const colors = require('../utils/colors') |
| 7 | + |
| 8 | +function report(results) { |
| 9 | + const maxFileLength = getMaxFileLenght(results) |
| 10 | + |
| 11 | + const counter = { pass: 0, fail: 0 } |
| 12 | + |
| 13 | + results.forEach(function(row) { |
| 14 | + printBlockHeader(row) |
| 15 | + |
| 16 | + row.filesMatched.forEach(function(file) { |
| 17 | + printRow(file, row, maxFileLength) |
| 18 | + |
| 19 | + if (file.pass) counter.pass++ |
| 20 | + else counter.fail++ |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + printSummary(counter) |
| 25 | + |
| 26 | + // exit with error code 1 if there are any failed checks |
| 27 | + if (counter.fail) process.exit(1) |
| 28 | +} |
| 29 | + |
| 30 | +module.exports = { report } |
| 31 | + |
| 32 | +function getMaxFileLenght(results) { |
| 33 | + let maxFileLength = 0 |
| 34 | + |
| 35 | + results.forEach(function(row) { |
| 36 | + row.filesMatched.forEach(function(file) { |
| 37 | + if (file.path.length > maxFileLength) maxFileLength = file.path.length |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + return maxFileLength |
| 42 | +} |
| 43 | + |
| 44 | +function printBlockHeader(row) { |
| 45 | + console.log() |
| 46 | + console.log(colors.subtle(`${figures.line} ${row.path}`)) |
| 47 | +} |
| 48 | + |
| 49 | +function printRow(file, row, maxFileLength) { |
| 50 | + const symbol = getSymbol(file) |
| 51 | + const operator = getOperator(file, row) |
| 52 | + |
| 53 | + console.log( |
| 54 | + ' ', |
| 55 | + symbol, |
| 56 | + rightpad(file.path, maxFileLength), |
| 57 | + ' ', |
| 58 | + bytes(file.size), |
| 59 | + operator, |
| 60 | + row.maxSize, |
| 61 | + colors.subtle(row.compression || 'gzip') |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +function printSummary({ pass, fail }) { |
| 66 | + console.log() |
| 67 | + |
| 68 | + if (pass) console.log(colors.pass(' ', pass, plur('check', pass), 'passed')) |
| 69 | + if (fail) console.log(colors.fail(' ', fail, plur('check', fail), 'failed')) |
| 70 | + |
| 71 | + console.log() |
| 72 | +} |
| 73 | + |
| 74 | +function getSymbol(file) { |
| 75 | + return file.pass ? colors.pass(figures.tick) : colors.fail(figures.cross) |
| 76 | +} |
| 77 | + |
| 78 | +function getOperator(file, row) { |
| 79 | + const fileSize = bytes.parse(file.size) |
| 80 | + const maxSize = bytes.parse(row.maxSize) |
| 81 | + |
| 82 | + if (fileSize > maxSize) return colors.fail('>') |
| 83 | + if (fileSize === maxSize) return colors.pass('=') |
| 84 | + return colors.pass('<') |
| 85 | +} |
0 commit comments