-
Notifications
You must be signed in to change notification settings - Fork 13
/
cli.js
executable file
·103 lines (85 loc) · 2.22 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
#!/usr/bin/env node
'use strict'
require('source-map-support').install()
const ora = require('ora')
const meow = require('meow')
const formatBenchmark = require('./format-benchmark')
const ReactBenchmark = require('.')
const cli = meow({
help: `
Usage
$ react-benchmark <path>
Options
<path> Path to a JavaScript or TypeScript file that exports the function to be benchmarked.
--debug, -d Run a development build instead of a production build to aid debugging.
--devtools, -t Run Chrome in windowed mode with the devtools open.
--cpuThrottle=X Run Chrome with CPU throttled X times.
--version Prints the version.
--help Prints this message.
Examples
$ react-benchmark benchmark.js
`.trim(),
flags: {
debug: {
type: 'boolean',
default: false,
alias: 'd',
},
devtools: {
type: 'boolean',
default: false,
alias: 't',
},
cpuThrottle: {
type: 'number',
default: 1,
},
},
})
let spinner
async function main() {
if (cli.input.length !== 1) {
cli.showHelp()
return
}
const [filepath] = cli.input
const { debug, devtools, cpuThrottle } = cli.flags
spinner = ora().start()
const reactBenchmark = new ReactBenchmark()
reactBenchmark.on('webpack', () => {
// Add trailing spaces so that if ts-loader console.log's something it's easier to read
spinner.text = 'Compiling bundle '
})
reactBenchmark.on('server', () => {
spinner.text = 'Starting server '
})
reactBenchmark.on('chrome', () => {
spinner.text = 'Starting Chrome '
})
reactBenchmark.on('start', () => {
spinner.text = 'Starting benchmark '
})
reactBenchmark.on('progress', (benchmark) => {
spinner.text = formatBenchmark(benchmark)
})
reactBenchmark.on('console', (log) => {
spinner.clear()
// Log to stderr so that stdout only contains the final output
console.error(`console.${log.type}: ${log.text}`)
spinner.render()
})
const result = await reactBenchmark.run(filepath, {
debug,
devtools,
cpuThrottle,
})
spinner.stop()
console.log(formatBenchmark(result))
}
main().catch((error) => {
if (spinner) {
spinner.fail()
}
console.error(error)
process.exitCode = 1
})