|
1 |
| -#!/usr/bin/env node |
2 | 1 | 'use strict'
|
3 | 2 |
|
4 | 3 | const inquirer = require('inquirer')
|
5 | 4 | const bench = require('./lib/bench')
|
6 | 5 | const { choices, list } = require('./lib/packages')
|
7 | 6 | const argv = process.argv.slice(2)
|
8 | 7 |
|
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) |
32 | 17 | }
|
33 | 18 |
|
34 |
| -(argv.length === 0 |
35 |
| - ? inquirer.prompt([ |
| 19 | +async function getBenchmarkOptions () { |
| 20 | + if (argv.length) return parseArgv() |
| 21 | + return inquirer.prompt([ |
36 | 22 | {
|
37 | 23 | type: 'confirm',
|
38 | 24 | name: 'all',
|
@@ -70,20 +56,37 @@ function select (callback) {
|
70 | 56 | filter: Number
|
71 | 57 | }
|
72 | 58 | ])
|
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 | + } |
88 | 89 | }
|
89 |
| - }) |
| 90 | + ]) |
| 91 | + return result.list |
| 92 | +} |
0 commit comments