UNPKG

1.45 kBJavaScriptView Raw
1#!/usr/bin/env node
2const { inspect } = require('util')
3const program = require('commander')
4const reporter = require('./src/reporter')
5
6const bytes = require('bytes')
7const readStream = require('./src/readStream')
8const { error } = require('prettycli')
9const build = require('./src/build')
10const debug = require('./src/debug')
11const compressedSize = require('./src/compressed-size')
12
13if (process.stdin.isTTY) {
14 error('bundlesize-pipe executable is meant for usage with piped data.')
15}
16
17program
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
27const config = {
28 name: program.name || require('read-pkg-up').sync().pkg.name,
29 maxSize: program.maxSize,
30 compression: program.compression || 'gzip'
31}
32
33debug('config', config)
34
35process.stdin.setEncoding('utf8')
36readStream(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
49process.on('unhandledRejection', reason => {
50 console.log('Unhandled Promise')
51 console.log(inspect(reason))
52 build.error()
53})