Skip to content

Commit 086bbd8

Browse files
authored
Prettier reporter to beta port (#325)
* add matched path info to files * get colors from ava * rewrite reporter * add compression to output * update snapsohts * rename tiny things * clean up main function * update snapshots * tiny change, big cleanup * tiny refactor * move things around
1 parent da81cb0 commit 086bbd8

File tree

11 files changed

+107
-58
lines changed

11 files changed

+107
-58
lines changed

index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
#!/usr/bin/env node
22

3-
const { inspect } = require('util')
4-
const config = require('./src/files')
5-
const analyse = require('./src/analyse')
3+
const files = require('./src/pipeline/files')
4+
const analyse = require('./src/pipeline/analyse')
5+
66
const cli = require('./src/reporters/cli')
7-
const reporter = require('./src/reporter')
8-
const build = require('./src/build')
7+
const github = require('./src/reporters/github')
8+
const build = require('./src/reporters/build')
99

10-
const report = analyse(config)
11-
cli.report(report)
12-
// broke this
10+
// old reporter
11+
const reporter = require('./src/reporter')
1312
// reporter(report.files)
1413

15-
process.on('unhandledRejection', function(reason) {
16-
console.log('Unhandled Promise')
17-
console.log(inspect(reason))
18-
build.error()
19-
})
14+
try {
15+
const results = analyse(files)
16+
cli.report(results)
17+
build.report(results)
18+
} catch (err) {
19+
build.error(err)
20+
}
2021

2122
/*
2223
This is the ideal structure to get to:

src/analyse.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/pipeline/analyse.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
loop through files and add
3+
pass: true|false
4+
*/
5+
6+
const bytes = require('bytes')
7+
8+
function analyse(config) {
9+
const counter = { pass: 0, fail: 0 }
10+
11+
const files = config.files.map(function(row) {
12+
row.filesMatched.map(function(file) {
13+
const parsedFileSize = bytes.parse(file.size)
14+
const parsedMaxSize = bytes.parse(row.maxSize)
15+
16+
if (parsedFileSize > parsedMaxSize) {
17+
file.pass = false
18+
file.operator = '>'
19+
} else if (parsedFileSize < parsedMaxSize) {
20+
file.pass = true
21+
file.operator = '<'
22+
} else {
23+
file.pass = true
24+
file.operator = '='
25+
}
26+
27+
if (file.pass) counter.pass++
28+
else counter.fail++
29+
})
30+
return row
31+
})
32+
33+
const status = counter.fail ? 'fail' : 'pass'
34+
35+
return { files, counter, status }
36+
}
37+
38+
module.exports = analyse

src/config.js renamed to src/pipeline/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const cosmiconfig = require('cosmiconfig')
2+
const { error } = require('prettycli')
3+
const program = require('commander')
24
const fs = require('fs')
35

4-
const program = require('commander')
5-
const { error } = require('prettycli')
6-
const debug = require('./debug')
6+
const debug = require('../utils/debug')
77

88
// default places we check
99
const configPaths = ['package.json', 'bundlesize.config.json']

src/files.js renamed to src/pipeline/files.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const fs = require('fs')
2-
const bytes = require('bytes')
32
const glob = require('glob')
3+
const bytes = require('bytes')
44
const { error } = require('prettycli')
5+
56
const config = require('./config')
6-
const debug = require('./debug')
7-
const compressedSize = require('./compressed-size')
7+
const debug = require('../utils/debug')
8+
const compressedSize = require('../utils/compressed-size')
89

910
config.files.map(row => {
1011
row.filesMatched = []

src/reporters/build.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const colors = require('../utils/colors')
2+
3+
function pass() {
4+
// do nothing
5+
}
6+
7+
function fail() {
8+
process.exit(1)
9+
}
10+
11+
function error(err) {
12+
console.log(colors.fail('The build has an error: \n'))
13+
console.log(err)
14+
fail()
15+
}
16+
17+
function report(results) {
18+
if (results.status === 'fail') fail()
19+
else pass()
20+
}
21+
22+
module.exports = { report, error }

src/reporters/cli.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,27 @@ const plur = require('plur')
66
const colors = require('../utils/colors')
77

88
function report(results) {
9-
const maxFileLength = getMaxFileLenght(results)
9+
const files = results.files
1010

11-
const counter = { pass: 0, fail: 0 }
11+
const maxFileLength = getMaxFileLenght(files)
1212

13-
results.forEach(function(row) {
13+
files.forEach(function(row) {
1414
printBlockHeader(row)
1515

1616
row.filesMatched.forEach(function(file) {
1717
printRow(file, row, maxFileLength)
18-
19-
if (file.pass) counter.pass++
20-
else counter.fail++
2118
})
2219
})
2320

24-
printSummary(counter)
25-
26-
// exit with error code 1 if there are any failed checks
27-
if (counter.fail) process.exit(1)
21+
printSummary(results.counter)
2822
}
2923

3024
module.exports = { report }
3125

32-
function getMaxFileLenght(results) {
26+
function getMaxFileLenght(files) {
3327
let maxFileLength = 0
3428

35-
results.forEach(function(row) {
29+
files.forEach(function(row) {
3630
row.filesMatched.forEach(function(file) {
3731
if (file.path.length > maxFileLength) maxFileLength = file.path.length
3832
})
@@ -48,7 +42,7 @@ function printBlockHeader(row) {
4842

4943
function printRow(file, row, maxFileLength) {
5044
const symbol = getSymbol(file)
51-
const operator = getOperator(file, row)
45+
const operator = getOperator(file)
5246

5347
console.log(
5448
' ',
@@ -75,11 +69,12 @@ function getSymbol(file) {
7569
return file.pass ? colors.pass(figures.tick) : colors.fail(figures.cross)
7670
}
7771

78-
function getOperator(file, row) {
79-
const fileSize = bytes.parse(file.size)
80-
const maxSize = bytes.parse(row.maxSize)
72+
function getOperator(file) {
73+
const map = {
74+
'>': colors.fail('>'),
75+
'<': colors.pass('<'),
76+
'=': colors.pass('=')
77+
}
8178

82-
if (fileSize > maxSize) return colors.fail('>')
83-
if (fileSize === maxSize) return colors.pass('=')
84-
return colors.pass('<')
79+
return map[file.operator]
8580
}

src/reporters/github.js

Whitespace-only changes.

src/brotli.js renamed to src/utils/brotli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const { error } = require('prettycli')
22
const zlib = require('zlib')
33

4-
const config = require('./config')
4+
const config = require('../pipeline/config')
55

66
function getBrotliSync() {
77
// Does this project want brotli compression?
8-
const needsBrotli = config.find(row => row.compression === 'brotli')
8+
const needsBrotli = config.files.find(row => row.compression === 'brotli')
99

1010
// If it doesn't, save us the trouble.
1111
if (!needsBrotli) return null
File renamed without changes.

0 commit comments

Comments
 (0)