-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
pipe.js
53 lines (46 loc) · 1.42 KB
/
pipe.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
#!/usr/bin/env node
const { inspect } = require('util')
const program = require('commander')
const reporter = require('./src/reporter')
const bytes = require('bytes')
const readStream = require('./src/readStream')
const { error } = require('prettycli')
const build = require('./src/build')
const debug = require('./src/debug')
const compressedSize = require('./src/compressed-size')
if (process.stdin.isTTY) {
error('bundlesize-pipe executable is meant for usage with piped data.')
}
program
.option('-n, --name [name]', 'custom name for a file (lib.min.js)')
.option('-s, --max-size [maxSize]', 'maximum size threshold (3Kb)')
.option(
'-c, --compression [gzip|brotli|none]',
'specify which compression algorithm to use'
)
.option('--debug', 'run in debug mode')
.parse(process.argv)
const config = {
name: program.name || require('read-pkg-up').sync().pkg.name,
maxSize: program.maxSize,
compression: program.compression || 'gzip'
}
debug('config', config)
process.stdin.setEncoding('utf8')
readStream(process.stdin).then(data => {
const size = compressedSize(data, config.compression)
const maxSize = bytes(config.maxSize) || Infinity
const file = {
path: config.name,
maxSize,
size,
compression: config.compression
}
debug('file', file)
reporter([file])
})
process.on('unhandledRejection', reason => {
console.log('Unhandled Promise')
console.log(inspect(reason))
build.error()
})