Skip to content

Commit

Permalink
refactor benchmark-bench to use async await
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesKyburz committed Oct 14, 2019
1 parent 8f3ef22 commit 16d39be
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions benchmark-bench.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
#!/usr/bin/env node
'use strict'

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

function select (callback) {
inquirer.prompt([
{
type: 'checkbox',
message: 'Select packages',
name: 'list',
choices: [
new inquirer.Separator(' = The usual ='),
...list(),
new inquirer.Separator(' = The extras = '),
...list(true)
],
validate: function (answer) {
if (answer.length < 1) {
return 'You must choose at least one package.'
}
return true
}
}
])
.then(function (answers) {
callback(answers.list)
})
run().catch(err => {
console.error(err)
process.exit(1)
})

async function run () {
const options = await getBenchmarkOptions()
const modules = options.all ? choices : await select(list)
return bench(options, modules)
}

(argv.length === 0
? inquirer.prompt([
async function getBenchmarkOptions () {
if (argv.length) return parseArgv()
return inquirer.prompt([
{
type: 'confirm',
name: 'all',
Expand Down Expand Up @@ -70,20 +56,37 @@ function select (callback) {
filter: Number
}
])
: new Promise((resolve, reject) => {
const [all, connections, pipelining, duration] = argv
resolve({
all: all === 'y',
connections: +connections,
pipelining: +pipelining,
duration: +duration
})
})
)
.then((opts) => {
if (!opts.all) {
select(list => bench(opts, list))
} else {
bench(opts, choices)
}

function parseArgv () {
const [all, connections, pipelining, duration] = argv
return {
all: all === 'y',
connections: +connections,
pipelining: +pipelining,
duration: +duration
}
}

async function select () {
const result = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select packages',
name: 'list',
choices: [
new inquirer.Separator(' = The usual ='),
...list(),
new inquirer.Separator(' = The extras = '),
...list(true)
],
validate: function (answer) {
if (answer.length < 1) {
return 'You must choose at least one package.'
}
return true
}
}
})
])
return result.list
}

0 comments on commit 16d39be

Please sign in to comment.