-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcli.js
executable file
·159 lines (131 loc) · 4.01 KB
/
cli.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
'use strict'
const cac = require('cac')
const fs = require('fs-extra')
const update = require('update-notifier')
const chalk = require('chalk')
const createTable = require('text-table')
const getWidth = require('string-width')
const bytes = require('bytes')
const logUpdate = require('log-update')
const ora = require('ora')
const cache = require('./lib/cache')
const pkg = require('./package.json')
const cli = cac()
cli.command('clear-cache', 'Clear the package size cache.').action(() => {
cache.clear()
console.log('Done!')
})
cli
.command('[...packages]', pkg.description)
.action((input, flags) => {
if (input.length === 0) return cli.outputHelp()
if (flags.debug) {
process.env.DEBUG = 'package-size'
}
if (flags.analyze) {
return require('./lib')(input.join(','), flags)
}
const spinner = ora({ spinner: 'simpleDotsScrolling' })
const stats = input.map(name => ({
name,
size: -1,
minified: -1,
gzipped: -1
}))
let finalTable
const render = () => {
let result = [].concat(stats)
if (flags.sort) {
result = result.sort((a, b) => {
return a.gzipped > b.gzipped
})
}
const frame = spinner.frame()
result = result.map(item => {
const prettify = v => (v > 0 ? bytes(v, { unitSeparator: ' ' }) : frame)
return [
` ${chalk.yellow(item.versionedName || item.name)}`,
prettify(item.size),
prettify(item.minified),
prettify(item.gzipped)
]
})
result.unshift(
[' package', 'size', 'minified', 'gzipped'].map(v => chalk.bold(v))
)
const table = `\n${createTable(result, { stringLength: getWidth })}\n`
if (!flags.debug) {
logUpdate(table)
}
finalTable = table
}
const build = require('./lib')
this.timer = setInterval(render, 100)
Promise.all(
input.map((name, index) => {
return build(name, flags).then(stat => {
stats[index] = stat
render()
})
})
)
.then(() => clearInterval(this.timer))
.then(() => {
if (flags.debug) {
console.log(finalTable)
}
if (flags.output) {
const outputFile = typeof flags.output === 'string'
? flags.output
: 'package-size-output.json'
return fs
.writeFile(outputFile, JSON.stringify(stats, null, 2), 'utf8')
.then(() => {
console.log(
`> Results have been saved to ${outputFile} in JSON format.`
)
})
}
})
.catch(err => {
clearInterval(this.timer)
handlerError(err)
})
})
.option('--debug', 'Show debug output')
.option('--cwd', 'Bundle package in current working directory')
.option('--externals <externals>', 'Exclude packages from bundled file')
.option('--sort', 'Sort packages from small to big bundle size')
.option('--no-cache', 'Disable module size caching')
.option('--output', 'Save results to file system in JSON format')
.option('--analyze', 'Analyze bundled files')
.option('--port <port>', 'Port to start analyze on. Defaults to 8888')
.option('--registry <registry>', 'npm registry URL to install from')
.option(
'--resolve <directory>',
'Extra folders to resolve local node_modules from'
)
.example(` package-size react,react-dom`)
.example(` package-size styled-jsx/style --externals react`)
.example(` package-size ./dist/my-bundle.js`)
.example(` package-size local-package --cwd`)
.example(` package-size vue@1 angular@1 react@0.14`)
cli.version(pkg.version)
cli.help()
cli.parse()
function handlerError(err) {
logUpdate('')
if (err.name === 'WebpackOptionsValidationError') {
stderr(err.message)
} else {
stderr(err.stack)
}
process.exit(1)
}
function stderr(msg) {
console.log(`${chalk.bgRed.black(' ERROR ')} Compiled with errors!`)
console.log('\n' + msg)
console.log()
}
update({ pkg }).notify()