forked from Rowno/react-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·97 lines (80 loc) · 2.06 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
#!/usr/bin/env node
'use strict'
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 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.
--version Prints the version.
--help Prints this message.
--updates, -u Number of component updates to perform (default: 0)
Examples
$ react-benchmark benchmark.js
`.trim(),
flags: {
debug: {
type: 'boolean',
default: false,
alias: 'd'
},
devtools: {
type: 'boolean',
default: false,
alias: 't'
},
updates: {
type: 'number',
default: 0,
alias: 'u'
}
}
})
let spinner
async function main() {
if (cli.input.length !== 1) {
cli.showHelp()
return
}
const [filepath] = cli.input
const {debug, devtools, updates} = cli.flags
spinner = ora().start()
const reactBenchmark = new ReactBenchmark()
reactBenchmark.on('webpack', () => {
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, updates})
spinner.stop()
console.log(formatBenchmark(result))
}
main().catch(error => {
if (spinner) {
spinner.fail()
}
console.error(error)
process.exitCode = 1
})