1 | #!/usr/bin/env node
|
2 | const { inspect } = require('util')
|
3 | const program = require('commander')
|
4 | const reporter = require('./src/reporter')
|
5 |
|
6 | const bytes = require('bytes')
|
7 | const readStream = require('./src/readStream')
|
8 | const { error } = require('prettycli')
|
9 | const build = require('./src/build')
|
10 | const debug = require('./src/debug')
|
11 | const compressedSize = require('./src/compressed-size')
|
12 |
|
13 | if (process.stdin.isTTY) {
|
14 | error('bundlesize-pipe executable is meant for usage with piped data.')
|
15 | }
|
16 |
|
17 | program
|
18 | .option('-n, --name [name]', 'custom name for a file (lib.min.js)')
|
19 | .option('-s, --max-size [maxSize]', 'maximum size threshold (3Kb)')
|
20 | .option(
|
21 | '-c, --compression [gzip|brotli|none]',
|
22 | 'specify which compression algorithm to use'
|
23 | )
|
24 | .option('--debug', 'run in debug mode')
|
25 | .parse(process.argv)
|
26 |
|
27 | const config = {
|
28 | name: program.name || require('read-pkg-up').sync().pkg.name,
|
29 | maxSize: program.maxSize,
|
30 | compression: program.compression || 'gzip'
|
31 | }
|
32 |
|
33 | debug('config', config)
|
34 |
|
35 | process.stdin.setEncoding('utf8')
|
36 | readStream(process.stdin).then(data => {
|
37 | const size = compressedSize(data, config.compression)
|
38 | const maxSize = bytes(config.maxSize) || Infinity
|
39 | const file = {
|
40 | path: config.name,
|
41 | maxSize,
|
42 | size,
|
43 | compression: config.compression
|
44 | }
|
45 | debug('file', file)
|
46 | reporter([file])
|
47 | })
|
48 |
|
49 | process.on('unhandledRejection', reason => {
|
50 | console.log('Unhandled Promise')
|
51 | console.log(inspect(reason))
|
52 | build.error()
|
53 | })
|