Skip to content

Commit 16d39be

Browse files
committed
refactor benchmark-bench to use async await
1 parent 8f3ef22 commit 16d39be

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

benchmark-bench.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
#!/usr/bin/env node
21
'use strict'
32

43
const inquirer = require('inquirer')
54
const bench = require('./lib/bench')
65
const { choices, list } = require('./lib/packages')
76
const argv = process.argv.slice(2)
87

9-
function select (callback) {
10-
inquirer.prompt([
11-
{
12-
type: 'checkbox',
13-
message: 'Select packages',
14-
name: 'list',
15-
choices: [
16-
new inquirer.Separator(' = The usual ='),
17-
...list(),
18-
new inquirer.Separator(' = The extras = '),
19-
...list(true)
20-
],
21-
validate: function (answer) {
22-
if (answer.length < 1) {
23-
return 'You must choose at least one package.'
24-
}
25-
return true
26-
}
27-
}
28-
])
29-
.then(function (answers) {
30-
callback(answers.list)
31-
})
8+
run().catch(err => {
9+
console.error(err)
10+
process.exit(1)
11+
})
12+
13+
async function run () {
14+
const options = await getBenchmarkOptions()
15+
const modules = options.all ? choices : await select(list)
16+
return bench(options, modules)
3217
}
3318

34-
(argv.length === 0
35-
? inquirer.prompt([
19+
async function getBenchmarkOptions () {
20+
if (argv.length) return parseArgv()
21+
return inquirer.prompt([
3622
{
3723
type: 'confirm',
3824
name: 'all',
@@ -70,20 +56,37 @@ function select (callback) {
7056
filter: Number
7157
}
7258
])
73-
: new Promise((resolve, reject) => {
74-
const [all, connections, pipelining, duration] = argv
75-
resolve({
76-
all: all === 'y',
77-
connections: +connections,
78-
pipelining: +pipelining,
79-
duration: +duration
80-
})
81-
})
82-
)
83-
.then((opts) => {
84-
if (!opts.all) {
85-
select(list => bench(opts, list))
86-
} else {
87-
bench(opts, choices)
59+
}
60+
61+
function parseArgv () {
62+
const [all, connections, pipelining, duration] = argv
63+
return {
64+
all: all === 'y',
65+
connections: +connections,
66+
pipelining: +pipelining,
67+
duration: +duration
68+
}
69+
}
70+
71+
async function select () {
72+
const result = await inquirer.prompt([
73+
{
74+
type: 'checkbox',
75+
message: 'Select packages',
76+
name: 'list',
77+
choices: [
78+
new inquirer.Separator(' = The usual ='),
79+
...list(),
80+
new inquirer.Separator(' = The extras = '),
81+
...list(true)
82+
],
83+
validate: function (answer) {
84+
if (answer.length < 1) {
85+
return 'You must choose at least one package.'
86+
}
87+
return true
88+
}
8889
}
89-
})
90+
])
91+
return result.list
92+
}

0 commit comments

Comments
 (0)