From f72e488ed48387e50e64dad04fcb483c9027cb97 Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Wed, 3 Jul 2019 22:47:27 +0530 Subject: [PATCH 1/9] add matched path info to files --- src/files.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/files.js b/src/files.js index 586231fc..a2d13fc4 100644 --- a/src/files.js +++ b/src/files.js @@ -18,7 +18,8 @@ config.map(file => { const maxSize = bytes(file.maxSize) || Infinity const compression = file.compression || 'gzip' const size = compressedSize(fs.readFileSync(path, 'utf8'), compression) - files.push({ maxSize, path, size, compression }) + const pathMatched = file.path + files.push({ size, maxSize, compression, path, pathMatched }) }) } }) From 27d0073837b8693419bbeeeff5fa20e89820c9db Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Wed, 3 Jul 2019 22:47:38 +0530 Subject: [PATCH 2/9] get colors from ava --- src/colors.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/colors.js diff --git a/src/colors.js b/src/colors.js new file mode 100644 index 00000000..fc793a46 --- /dev/null +++ b/src/colors.js @@ -0,0 +1,15 @@ +const chalk = require('chalk') + +module.exports = { + pass: chalk.green, + error: chalk.red, + skip: chalk.yellow, + log: chalk.gray, + title: chalk.bold, + log: chalk.gray, + duration: chalk.gray.dim, + errorSource: chalk.gray, + errorStack: chalk.gray, + stack: chalk.red, + information: chalk.magenta +} From 490183908a2cff3e3a9cf96876966984fa03d00c Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 20:53:10 +0530 Subject: [PATCH 3/9] rewrite reporter --- index.js | 24 +++++++++++- package.json | 4 +- src/analyse.js | 18 +++++++++ src/colors.js | 15 ------- src/config.js | 6 +-- src/files.js | 30 +++++++------- src/reporter.js | 16 +++++--- src/reporters/cli.js | 71 ++++++++++++++++++++++++++++++++++ src/utils/colors.js | 9 +++++ tests/snapshots/index.js.md | 64 +++++++++++++++++++++++------- tests/snapshots/index.js.snap | Bin 500 -> 546 bytes 11 files changed, 201 insertions(+), 56 deletions(-) create mode 100644 src/analyse.js delete mode 100644 src/colors.js create mode 100644 src/reporters/cli.js create mode 100644 src/utils/colors.js diff --git a/index.js b/index.js index f021d4d4..7539273d 100755 --- a/index.js +++ b/index.js @@ -1,14 +1,34 @@ #!/usr/bin/env node const { inspect } = require('util') -const files = require('./src/files') +const config = require('./src/files') +const analyse = require('./src/analyse') +const cli = require('./src/reporters/cli') const reporter = require('./src/reporter') const build = require('./src/build') -reporter(files) +const report = analyse(config) +cli.report(report) +// broke this +// reporter(report.files) process.on('unhandledRejection', function(reason) { console.log('Unhandled Promise') console.log(inspect(reason)) build.error() }) + +/* + This is the ideal structure to get to: + + - utilities function + - pipe results down + + start() + .then(getConfig) + .then(attachFiles) + .then(attachSize) + .then(attachComparison) + .then(reportOnCli) + .then(reportOnBuild) +*/ diff --git a/package.json b/package.json index 08ad538a..37c3c28c 100644 --- a/package.json +++ b/package.json @@ -42,10 +42,12 @@ "ci-env": "^1.4.0", "commander": "^2.20.0", "cosmiconfig": "^5.2.1", + "figures": "^3.0.0", "github-build": "^1.2.0", "glob": "^7.1.4", "gzip-size": "^4.0.0", - "prettycli": "^1.4.3" + "prettycli": "^1.4.3", + "right-pad": "^1.0.1" }, "lint-staged": { "*.js": [ diff --git a/src/analyse.js b/src/analyse.js new file mode 100644 index 00000000..387b04e3 --- /dev/null +++ b/src/analyse.js @@ -0,0 +1,18 @@ +/* + loop through files and add + pass: true|false +*/ + +const bytes = require('bytes') + +function analyse(config) { + return config.files.map(function(row) { + row.filesMatched.map(function(file) { + if (bytes.parse(file.size) > bytes.parse(row.maxSize)) file.pass = false + else file.pass = true + }) + return row + }) +} + +module.exports = analyse diff --git a/src/colors.js b/src/colors.js deleted file mode 100644 index fc793a46..00000000 --- a/src/colors.js +++ /dev/null @@ -1,15 +0,0 @@ -const chalk = require('chalk') - -module.exports = { - pass: chalk.green, - error: chalk.red, - skip: chalk.yellow, - log: chalk.gray, - title: chalk.bold, - log: chalk.gray, - duration: chalk.gray.dim, - errorSource: chalk.gray, - errorStack: chalk.gray, - stack: chalk.red, - information: chalk.magenta -} diff --git a/src/config.js b/src/config.js index c1405a1b..2af1e13f 100644 --- a/src/config.js +++ b/src/config.js @@ -76,10 +76,10 @@ if (!configFromFile && !configFromCli) { ) } -const config = configFromCli || configFromFile +const files = configFromCli || configFromFile debug('cli config', configFromCli) debug('file config', configFromFile) -debug('selected config', config) +debug('selected config', files) -module.exports = config +module.exports = { files } diff --git a/src/files.js b/src/files.js index a2d13fc4..76f42464 100644 --- a/src/files.js +++ b/src/files.js @@ -5,25 +5,25 @@ const { error } = require('prettycli') const config = require('./config') const debug = require('./debug') const compressedSize = require('./compressed-size') -const files = [] -config.map(file => { - const paths = glob.sync(file.path) - if (!paths.length) { - error(`There is no matching file for ${file.path} in ${process.cwd()}`, { +config.files.map(row => { + row.filesMatched = [] + + const files = glob.sync(row.path) + + files.map(path => { + const compression = row.compression || 'gzip' + const size = compressedSize(fs.readFileSync(path, 'utf8'), compression) + row.filesMatched.push({ path, size }) + }) + + if (!row.filesMatched.length) { + error(`There is no matching file for ${row.path} in ${process.cwd()}`, { silent: true }) - } else { - paths.map(path => { - const maxSize = bytes(file.maxSize) || Infinity - const compression = file.compression || 'gzip' - const size = compressedSize(fs.readFileSync(path, 'utf8'), compression) - const pathMatched = file.path - files.push({ size, maxSize, compression, path, pathMatched }) - }) } }) -debug('files', files) +debug('files', config) -module.exports = files +module.exports = config diff --git a/src/reporter.js b/src/reporter.js index 816c1a3a..90f95012 100644 --- a/src/reporter.js +++ b/src/reporter.js @@ -53,9 +53,13 @@ const getGlobalMessage = ({ const prettyChange = change === 0 ? 'no change' - : change > 0 ? `+${bytes(change)}` : `-${bytes(Math.abs(change))}` + : change > 0 + ? `+${bytes(change)}` + : `-${bytes(Math.abs(change))}` - globalMessage = `${failures} out of ${results.length} bundles are too big! (${prettyChange})` + globalMessage = `${failures} out of ${ + results.length + } bundles are too big! (${prettyChange})` } else { // multiple files, no failures const prettySize = bytes(totalSize) @@ -64,7 +68,9 @@ const getGlobalMessage = ({ const prettyChange = change === 0 ? 'no change' - : change > 0 ? `+${bytes(change)}` : `-${bytes(Math.abs(change))}` + : change > 0 + ? `+${bytes(change)}` + : `-${bytes(Math.abs(change))}` globalMessage = `Total bundle size is ${prettySize}/${prettyMaxSize} (${prettyChange})` } @@ -127,7 +133,7 @@ const analyse = ({ files, masterValues }) => { }) } -const report = ({ files, globalMessage, fail }) => { +const globalReport = ({ files, globalMessage, fail }) => { /* prepare the build page */ const params = encodeURIComponent( JSON.stringify({ files, repo, branch, commit_message, sha }) @@ -160,7 +166,7 @@ const compare = (files, masterValues = {}) => { }) let fail = results.filter(result => result.fail).length > 0 - report({ files, globalMessage, fail }) + globalReport({ files, globalMessage, fail }) } const reporter = files => { diff --git a/src/reporters/cli.js b/src/reporters/cli.js new file mode 100644 index 00000000..bdaaae22 --- /dev/null +++ b/src/reporters/cli.js @@ -0,0 +1,71 @@ +const rightpad = require('right-pad') +const figures = require('figures') +const bytes = require('bytes') + +const colors = require('../utils/colors') + +function report(results) { + const maxFileLength = getMaxFileLenght(results) + + const counter = { pass: 0, fail: 0 } + + results.forEach(function(row) { + console.log() + console.log(colors.path(`${figures.line} ${row.path}`)) + row.filesMatched.forEach(function(file) { + const symbol = getSymbol(file) + const operator = getOperator(file, row) + + if (file.pass) counter.pass++ + else counter.fail++ + + console.log( + ' ', + symbol, + rightpad(file.path, maxFileLength), + ' ', + bytes(file.size), + operator, + row.maxSize + ) + + // > maxSize ${prettySize} ${compressionText} + }) + }) + console.log() + + if (counter.pass) console.log(' ', colors.pass(counter.pass, 'checks passed')) + if (counter.fail) console.log(' ', colors.fail(counter.fail, 'checks failed')) + + console.log() + + // exit with error code 1 if there are any errors + if (counter.fail) process.exit(1) +} + +module.exports = { report } + +function getMaxFileLenght(results) { + let maxFileLength = 0 + + results.forEach(function(row) { + row.filesMatched.forEach(function(file) { + if (file.path.length > maxFileLength) maxFileLength = file.path.length + }) + }) + + return maxFileLength +} + +function getSymbol(file) { + return file.pass ? colors.pass(figures.tick) : colors.fail(figures.cross) +} + +function getOperator(file, row) { + const fileSize = bytes.parse(file.size) + const maxSize = bytes.parse(row.maxSize) + + if (fileSize > maxSize) return colors.fail('>') + if (fileSize === maxSize) return colors.pass('=') + return colors.pass('<') +} diff --git a/src/utils/colors.js b/src/utils/colors.js new file mode 100644 index 00000000..582906e7 --- /dev/null +++ b/src/utils/colors.js @@ -0,0 +1,9 @@ +const chalk = require('chalk') + +module.exports = { + path: chalk.gray, + pass: chalk.green, + fail: chalk.red, + title: chalk.bold, + information: chalk.magenta +} diff --git a/tests/snapshots/index.js.md b/tests/snapshots/index.js.md index a1cd5a60..57a40b44 100644 --- a/tests/snapshots/index.js.md +++ b/tests/snapshots/index.js.md @@ -8,68 +8,102 @@ Generated by [AVA](https://ava.li). > Snapshot 1 - 'PASS file-1.js: 270B < maxSize 300B (gzip)' + `─ file-1.js␊ + ✔ file-1.js 270B < 300B␊ + ␊ + 1 checks passed` ## 10. pass: match by fuzzy name > Snapshot 1 - `PASS build/vendor-ha5h.js: 270B < maxSize 350B (gzip) ␊ + `─ build/vendor-*.js␊ + ✔ build/vendor-ha5h.js 270B < 350B␊ + ␊ + ─ build/**/chunk-*.js␊ + ✔ build/chunks/chunk-ch0nk.js 270B < 300B␊ ␊ - PASS build/chunks/chunk-ch0nk.js: 270B < maxSize 300B (gzip)` + 2 checks passed` ## 2. fail: single file larger than limit > Snapshot 1 - 'FAIL file-2.js: 270B > maxSize 250B (gzip)' + `─ file-2.js␊ + ✖ file-2.js 270B > 250B␊ + ␊ + 1 checks failed` ## 3. pass: use brotli > Snapshot 1 - 'PASS file-3.js: 245B < maxSize 250B (brotli)' + `─ file-3.js␊ + ✔ file-3.js 245B < 250B␊ + ␊ + 1 checks passed` ## 4. fail: dont use compression > Snapshot 1 - 'FAIL file-4.js: 437B > maxSize 300B (no compression)' + `─ file-4.js␊ + ✖ file-4.js 437B > 300B␊ + ␊ + 1 checks failed` ## 5. pass: custom config file > Snapshot 1 - 'PASS file-5.js: 270B < maxSize 300B (gzip)' + `─ file-5.js␊ + ✔ file-5.js 270B < 300B␊ + ␊ + 1 checks passed` ## 6. pass: multiple files, both smaller than limit > Snapshot 1 - `PASS file-61.js: 270B < maxSize 300B (gzip) ␊ + `─ file-61.js␊ + ✔ file-61.js 270B < 300B␊ + ␊ + ─ file-62.js␊ + ✔ file-62.js 270B < 300B␊ ␊ - PASS file-62.js: 270B < maxSize 300B (gzip)` + 2 checks passed` ## 7. fail: multiple files, both bigger than limit > Snapshot 1 - `FAIL file-61.js: 270B > maxSize 200B (gzip) ␊ + `─ file-61.js␊ + ✖ file-61.js 270B > 200B␊ ␊ - FAIL file-62.js: 270B > maxSize 200B (gzip)` + ─ file-62.js␊ + ✖ file-62.js 270B > 200B␊ + ␊ + 2 checks failed` ## 8. fail: multiple files, 1 smaller + 1 bigger than limit > Snapshot 1 - `PASS file-61.js: 270B < maxSize 300B (gzip) ␊ + `─ file-61.js␊ + ✔ file-61.js 270B < 300B␊ + ␊ + ─ file-62.js␊ + ✖ file-62.js 270B > 200B␊ ␊ - FAIL file-62.js: 270B > maxSize 200B (gzip)` + 1 checks passed␊ + 1 checks failed` ## 9. pass: catch all js files > Snapshot 1 - `PASS build/chunks/chunk-ch0nk.js: 270B < maxSize 300B (gzip) ␊ + `─ build/**/*.js␊ + ✔ build/chunks/chunk-ch0nk.js 270B < 300B␊ + ✔ build/vendor-ha5h.js 270B < 300B␊ ␊ - PASS build/vendor-ha5h.js: 270B < maxSize 300B (gzip)` + 2 checks passed` diff --git a/tests/snapshots/index.js.snap b/tests/snapshots/index.js.snap index ed124eb2bcfd89d6a1fb406052a9da737cae9eed..dfaaac46dc7c3f768ed1f325fa8895fed2ff7715 100644 GIT binary patch delta 522 zcmV+l0`>j$1EK^XK~_N^Q*L2!b7*gLAa*he0sw?RVdPX(WV5^8f337gYwwXFB!A!w z#8XrbDSr>Uc=f1-;?zl>n_U^fqO*W_%kS%+LW}p9U1EJVnvs=3kkS6plm>;g%$!tRL%pnGE(L{0bEY8p3P50FZs4R~qhM@c;D5vgls8mJ z&PYwpE>!C{yY%&=()KHM-n1tU{5!_pFgs&E-*jOH?9m|-TSAeUhnhSy~# zXf88>8fIc_4sscWH}Sg66wPI(q=#7{+%Plr7%;;c1qk&<=<1EITW>_7_0zCf4~#Ti z)=$G`Jxo24^_W4))XYEy?_U$JA1(CpM=nzv*hNXDnK>!?T3Y&A2uDH0k~2#4vWp>< zZgPe}UN#n2B5Np1%}dEI(#=RT&48+e0^(zdX)%(cpypvXjG#THu;7Dx4&n*&yol*y M07-8~9byFl04k>Txc~qF delta 476 zcmV<20VDpR1oQ(UK~_N^Q*L2!b7*gLAa*he0ss^kjlOMsgNvJ;kM8d8+U1cVB!6HD z#8XrbDSr>Uc=f1-;?zl>n_U^fqD4Tw<@a?@p~ZX5F0sC_$f$2$$Osl)3&cBn%vSs~ zJ72hJf!2z{p6jPFf<-?7@m3MZ#Ueb}tw|kucBV1RJd9vbO`vt&ucw$a8(W5lO_bn% z=wAWU0}`DK#D_}y)F#>)FZAf~)qiZ;{rv?aSoALt?~PwwYQNBGe$6u{R(>Z}E=I6u zIuirK)q`Q|Eq}V+tedZQ!25RXI!3VQ8zAoU{%}$8X+{=!8ID0V%MmPWTP=eqlqc9jS!7VMfoK;nW$P#k?b@9Yc(-8N468< z*t~p&#lJB~8qElWlmZu*0|zL|o19^gmrZnVLJTNN%}dEI(#=RT%^)RU%SkcA S6sPSZJ0AcQ%^53u1ONa^hu>ZR From f3394d0357447826b5bce2c3e04cf19d6f678fb0 Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 20:56:06 +0530 Subject: [PATCH 4/9] add compression to output --- src/reporters/cli.js | 3 ++- src/utils/colors.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/reporters/cli.js b/src/reporters/cli.js index bdaaae22..40b4d1a1 100644 --- a/src/reporters/cli.js +++ b/src/reporters/cli.js @@ -26,7 +26,8 @@ function report(results) { ' ', bytes(file.size), operator, - row.maxSize + row.maxSize, + colors.compression(row.compression || 'gzip') ) // > maxSize ${prettySize} ${compressionText} diff --git a/src/utils/colors.js b/src/utils/colors.js index 582906e7..3930110e 100644 --- a/src/utils/colors.js +++ b/src/utils/colors.js @@ -4,6 +4,7 @@ module.exports = { path: chalk.gray, pass: chalk.green, fail: chalk.red, + compression: chalk.gray, title: chalk.bold, information: chalk.magenta } From c1271d1d56271d517c98296ec951dd548c11bec7 Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 21:01:18 +0530 Subject: [PATCH 5/9] update snapsohts --- src/reporters/cli.js | 2 -- tests/snapshots/index.js.md | 30 +++++++++++++++--------------- tests/snapshots/index.js.snap | Bin 546 -> 567 bytes 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/reporters/cli.js b/src/reporters/cli.js index 40b4d1a1..9d421043 100644 --- a/src/reporters/cli.js +++ b/src/reporters/cli.js @@ -29,8 +29,6 @@ function report(results) { row.maxSize, colors.compression(row.compression || 'gzip') ) - - // > maxSize ${prettySize} ${compressionText} }) }) console.log() diff --git a/tests/snapshots/index.js.md b/tests/snapshots/index.js.md index 57a40b44..2262cd6f 100644 --- a/tests/snapshots/index.js.md +++ b/tests/snapshots/index.js.md @@ -9,7 +9,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-1.js␊ - ✔ file-1.js 270B < 300B␊ + ✔ file-1.js 270B < 300B gzip␊ ␊ 1 checks passed` @@ -18,10 +18,10 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ build/vendor-*.js␊ - ✔ build/vendor-ha5h.js 270B < 350B␊ + ✔ build/vendor-ha5h.js 270B < 350B gzip␊ ␊ ─ build/**/chunk-*.js␊ - ✔ build/chunks/chunk-ch0nk.js 270B < 300B␊ + ✔ build/chunks/chunk-ch0nk.js 270B < 300B gzip␊ ␊ 2 checks passed` @@ -30,7 +30,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-2.js␊ - ✖ file-2.js 270B > 250B␊ + ✖ file-2.js 270B > 250B gzip␊ ␊ 1 checks failed` @@ -39,7 +39,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-3.js␊ - ✔ file-3.js 245B < 250B␊ + ✔ file-3.js 245B < 250B brotli␊ ␊ 1 checks passed` @@ -48,7 +48,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-4.js␊ - ✖ file-4.js 437B > 300B␊ + ✖ file-4.js 437B > 300B none␊ ␊ 1 checks failed` @@ -57,7 +57,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-5.js␊ - ✔ file-5.js 270B < 300B␊ + ✔ file-5.js 270B < 300B gzip␊ ␊ 1 checks passed` @@ -66,10 +66,10 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-61.js␊ - ✔ file-61.js 270B < 300B␊ + ✔ file-61.js 270B < 300B gzip␊ ␊ ─ file-62.js␊ - ✔ file-62.js 270B < 300B␊ + ✔ file-62.js 270B < 300B gzip␊ ␊ 2 checks passed` @@ -78,10 +78,10 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-61.js␊ - ✖ file-61.js 270B > 200B␊ + ✖ file-61.js 270B > 200B gzip␊ ␊ ─ file-62.js␊ - ✖ file-62.js 270B > 200B␊ + ✖ file-62.js 270B > 200B gzip␊ ␊ 2 checks failed` @@ -90,10 +90,10 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ file-61.js␊ - ✔ file-61.js 270B < 300B␊ + ✔ file-61.js 270B < 300B gzip␊ ␊ ─ file-62.js␊ - ✖ file-62.js 270B > 200B␊ + ✖ file-62.js 270B > 200B gzip␊ ␊ 1 checks passed␊ 1 checks failed` @@ -103,7 +103,7 @@ Generated by [AVA](https://ava.li). > Snapshot 1 `─ build/**/*.js␊ - ✔ build/chunks/chunk-ch0nk.js 270B < 300B␊ - ✔ build/vendor-ha5h.js 270B < 300B␊ + ✔ build/chunks/chunk-ch0nk.js 270B < 300B gzip␊ + ✔ build/vendor-ha5h.js 270B < 300B gzip␊ ␊ 2 checks passed` diff --git a/tests/snapshots/index.js.snap b/tests/snapshots/index.js.snap index dfaaac46dc7c3f768ed1f325fa8895fed2ff7715..1b46541be8851abd3c96f09cb11ff0f36d889108 100644 GIT binary patch delta 525 zcmV+o0`mQ$1h)htK~_N^Q*L2!b7*gLAa*he0svgPyUc=f1-;?zl>n_U^fqDzrp7JmbXclMaA_-S^&aMc2>6^A|7Ph|v)dI1gH zDk8a9geSW-sUy$MG=`an5iB|fh`nD=F=;lo3=f+q!Tr#`0;mThs>;N`aHynDZK9p= zLXRF_&9>d&Uoe72*8%a~_|>KM3$5nYJab~@cXH)o1dDnxGca5|7{=c6r|ZqS`G0x` zyl>a8V+4!71mZsL4@ZTMIc<78Q<<&qK07lbSac~10|PSyJJ{EZtPFyTE{~=(D5PcP zr0N>#WfgNNC_I`o1;JMU0wZ$+Cj}b?V*?Hd&?CnTckCcE8KG-3!s8Gl5*;!Pn?rz+ ziq|32usH-p6OuzP!Uc=f1-;?zl>n_U^fqO*}+7JnRwclMaA_-S^&aMc2>6^A|7Ph|v)ng9*m zDk8a9geSW-sUy$MG=`an5iHsW#NMx`m^2$(hKEg*;C|>|0n`H$Wnf}pI8@T7Hqp*_ zp+}FeX4~%XFBrk1^+3Eges!t+LaX^T&zxBKom{yX!J>l93=CHfhOxK&>3XwnzJJ~U z@7uNO7{Q{;fVj{5!%^X5PMaRjRA#HY&(6#U7L8zGU|?oo2m6|ll|hiv{?U{Mg|y6^ zR9!>8tYR(&g-3IyAovPEU}SFKq+p|9Y+&HT1(Y{bNX|%2&MsCcNGvW+O~GN95zMe@ z2tM2}I|UK zWu~NuSs~moGxQiR!x{w$^+xFGjj&s9M56W6uvrg`G+fqC!)859J(Bg9LCMt2Kn3p; zupce-@kcIG8`wojrI|S?`dV80S_nr$#F8^g^RkN}lx}i{L0&c%S0ZaDOEJw$$uH8) uNHooWs)PdKV~J@olB1yJVK|JSJ*KeWgL@9*3G%#%>0$s$Z$=$r1polVH|p{L From 0c5809152d004f5d6a36c4f4bf78fae70addfa90 Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 21:06:06 +0530 Subject: [PATCH 6/9] rename tiny things --- src/reporters/cli.js | 6 ++++-- src/utils/colors.js | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/reporters/cli.js b/src/reporters/cli.js index 9d421043..4546ec5a 100644 --- a/src/reporters/cli.js +++ b/src/reporters/cli.js @@ -11,7 +11,7 @@ function report(results) { results.forEach(function(row) { console.log() - console.log(colors.path(`${figures.line} ${row.path}`)) + console.log(colors.subtle(`${figures.line} ${row.path}`)) row.filesMatched.forEach(function(file) { const symbol = getSymbol(file) const operator = getOperator(file, row) @@ -27,8 +27,10 @@ function report(results) { bytes(file.size), operator, row.maxSize, - colors.compression(row.compression || 'gzip') + colors.subtle(row.compression || 'gzip') ) + + // > maxSize ${prettySize} ${compressionText} }) }) console.log() diff --git a/src/utils/colors.js b/src/utils/colors.js index 3930110e..026c6b18 100644 --- a/src/utils/colors.js +++ b/src/utils/colors.js @@ -1,10 +1,9 @@ const chalk = require('chalk') module.exports = { - path: chalk.gray, + subtle: chalk.gray, pass: chalk.green, fail: chalk.red, - compression: chalk.gray, title: chalk.bold, - information: chalk.magenta + info: chalk.magenta } From 7141789e566e5c1c25500b490644ce620086fab5 Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 21:17:43 +0530 Subject: [PATCH 7/9] clean up main function --- package.json | 1 + src/reporters/cli.js | 67 +++++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 37c3c28c..15518edf 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "github-build": "^1.2.0", "glob": "^7.1.4", "gzip-size": "^4.0.0", + "plur": "^3.1.1", "prettycli": "^1.4.3", "right-pad": "^1.0.1" }, diff --git a/src/reporters/cli.js b/src/reporters/cli.js index 4546ec5a..239efa3d 100644 --- a/src/reporters/cli.js +++ b/src/reporters/cli.js @@ -1,6 +1,7 @@ const rightpad = require('right-pad') const figures = require('figures') const bytes = require('bytes') +const plur = require('plur') const colors = require('../utils/colors') @@ -10,37 +11,19 @@ function report(results) { const counter = { pass: 0, fail: 0 } results.forEach(function(row) { - console.log() - console.log(colors.subtle(`${figures.line} ${row.path}`)) + printBlockHeader(row) + row.filesMatched.forEach(function(file) { - const symbol = getSymbol(file) - const operator = getOperator(file, row) + printRow(file, row, maxFileLength) if (file.pass) counter.pass++ else counter.fail++ - - console.log( - ' ', - symbol, - rightpad(file.path, maxFileLength), - ' ', - bytes(file.size), - operator, - row.maxSize, - colors.subtle(row.compression || 'gzip') - ) - - // > maxSize ${prettySize} ${compressionText} }) }) - console.log() - - if (counter.pass) console.log(' ', colors.pass(counter.pass, 'checks passed')) - if (counter.fail) console.log(' ', colors.fail(counter.fail, 'checks failed')) - console.log() + printSummary(counter) - // exit with error code 1 if there are any errors + // exit with error code 1 if there are any failed checks if (counter.fail) process.exit(1) } @@ -58,6 +41,44 @@ function getMaxFileLenght(results) { return maxFileLength } +function printBlockHeader(row) { + console.log() + console.log(colors.subtle(`${figures.line} ${row.path}`)) +} + +function printRow(file, row, maxFileLength) { + const symbol = getSymbol(file) + const operator = getOperator(file, row) + + console.log( + ' ', + symbol, + rightpad(file.path, maxFileLength), + ' ', + bytes(file.size), + operator, + row.maxSize, + colors.subtle(row.compression || 'gzip') + ) +} + +function printSummary(counter) { + console.log() + + if (counter.pass) { + console.log( + colors.pass(' ', counter.pass, plur('check', counter.pass), 'passed') + ) + } + if (counter.fail) { + console.log( + colors.fail(' ', counter.fail, plur('check', counter.fail), 'failed') + ) + } + + console.log() +} + function getSymbol(file) { return file.pass ? colors.pass(figures.tick) : colors.fail(figures.cross) } From 1f964ae839b98bbf01c7e9b4c9cc9603869840de Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 21:18:17 +0530 Subject: [PATCH 8/9] update snapshots --- tests/snapshots/index.js.md | 14 +++++++------- tests/snapshots/index.js.snap | Bin 567 -> 569 bytes 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/snapshots/index.js.md b/tests/snapshots/index.js.md index 2262cd6f..2c999bbc 100644 --- a/tests/snapshots/index.js.md +++ b/tests/snapshots/index.js.md @@ -11,7 +11,7 @@ Generated by [AVA](https://ava.li). `─ file-1.js␊ ✔ file-1.js 270B < 300B gzip␊ ␊ - 1 checks passed` + 1 check passed` ## 10. pass: match by fuzzy name @@ -32,7 +32,7 @@ Generated by [AVA](https://ava.li). `─ file-2.js␊ ✖ file-2.js 270B > 250B gzip␊ ␊ - 1 checks failed` + 1 check failed` ## 3. pass: use brotli @@ -41,7 +41,7 @@ Generated by [AVA](https://ava.li). `─ file-3.js␊ ✔ file-3.js 245B < 250B brotli␊ ␊ - 1 checks passed` + 1 check passed` ## 4. fail: dont use compression @@ -50,7 +50,7 @@ Generated by [AVA](https://ava.li). `─ file-4.js␊ ✖ file-4.js 437B > 300B none␊ ␊ - 1 checks failed` + 1 check failed` ## 5. pass: custom config file @@ -59,7 +59,7 @@ Generated by [AVA](https://ava.li). `─ file-5.js␊ ✔ file-5.js 270B < 300B gzip␊ ␊ - 1 checks passed` + 1 check passed` ## 6. pass: multiple files, both smaller than limit @@ -95,8 +95,8 @@ Generated by [AVA](https://ava.li). ─ file-62.js␊ ✖ file-62.js 270B > 200B gzip␊ ␊ - 1 checks passed␊ - 1 checks failed` + 1 check passed␊ + 1 check failed` ## 9. pass: catch all js files diff --git a/tests/snapshots/index.js.snap b/tests/snapshots/index.js.snap index 1b46541be8851abd3c96f09cb11ff0f36d889108..8b194e0dd29a0424863a4cc4114044d7ea60e564 100644 GIT binary patch delta 465 zcmV;?0WSWx1i1tvK~_N^Q*L2!b7*gLAa*he0svHc4KnZ=l?Bd&!h)qU_Lq?&B^L<9 zQ&bKqe-FEO^{9p7)JdP4T^Yfmi;-RyTos6S_L!~sX?DJF)dH;*hdtL%Wdw`50S(hM9*EEIJ*Cy&?3PdI!93*REp(i#`J4KJO1lg^xLH zdOTB^t?oWMGb31Z9t#5lGXp!=*Nm(Tf{f0SA^|Xetx%9yT%4MM!$2dLfzuFtxPf*G zMy6N{OiKi+L^aSAZlE!m3yon0nwWxI2sSXOD8D2plV~@Zpt;cmYNUy=ImnG*Pv_<5 zr4n!>!a!3rH=2?ciq&uf&Cuh;40psJG#R05GQ#5yBZxbSiFL>{Yz_fNC|-w5!{!ha zO-K%C!3<)iZU$-v2{A4~jT*d>&olw-vZT_?oD_X6EqyJ7;~-+m8KrsI#Slt2Il~|? z8<%U5b(N*&rQ{dsW+a+sK$SxQi808u7Rh-~Lou94&@xk$0EPP%;wK7xjOlg&!Re2# Hs09E3C+Ej` delta 463 zcmV;=0Wki#1h)htK~_N^Q*L2!b7*gLAa*he0svgPyhM9*EEIJ2>y&?3PdI!93*REp(i@pTnKJO1lg^xLH zdOTB^t?oWMGb31ZDGLJwGXp!=*Nm(Tf{ZScA^|XeU93=$SX`W%g2O~3n2FO6e7K2r z3Pz^bOiW7zDn&KX4Q`?_nj4K_CYqRn+z2)?sVKiBCzE(rnxMJT1Zt*g#|(GuAT$}FYcj&)5F-*DG7XzUfRT#VA=9uq1Vs~) zLomZ;n5mb6T0ugLOVHv5fBZ8|0J|-zG&3hfUrS403*kJ7SaL>bUUo5r(oN1V$jip% zUSwTmsd*{+MYP(WfFGOa~&9@J0_Cla*G6eUREeuemn0v}_#9RM+k;n1%I F001YN#~}a! From d39c40eae34afaf7b6d3de9ccfe467a1a845ebdc Mon Sep 17 00:00:00 2001 From: siddharthkp Date: Thu, 4 Jul 2019 21:20:54 +0530 Subject: [PATCH 9/9] tiny change, big cleanup --- src/reporters/cli.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/reporters/cli.js b/src/reporters/cli.js index 239efa3d..59fbf786 100644 --- a/src/reporters/cli.js +++ b/src/reporters/cli.js @@ -62,19 +62,11 @@ function printRow(file, row, maxFileLength) { ) } -function printSummary(counter) { +function printSummary({ pass, fail }) { console.log() - if (counter.pass) { - console.log( - colors.pass(' ', counter.pass, plur('check', counter.pass), 'passed') - ) - } - if (counter.fail) { - console.log( - colors.fail(' ', counter.fail, plur('check', counter.fail), 'failed') - ) - } + if (pass) console.log(colors.pass(' ', pass, plur('check', pass), 'passed')) + if (fail) console.log(colors.fail(' ', fail, plur('check', fail), 'failed')) console.log() }