Skip to content

Commit bc2c893

Browse files
authored
Replace commander with minimist (closes #305) (#307)
1 parent 164b89d commit bc2c893

File tree

6 files changed

+98
-63
lines changed

6 files changed

+98
-63
lines changed

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,21 @@ Should work in theory :)
259259
Usage: `airtap [options] <files>`. Supports multiple `files`. They can be paths relative to the working directory or glob patterns (e.g. `airtap test/*.js`). Options:
260260

261261
```
262-
-v, --version print version
263-
-l, --list-browsers list (effective or --all) browsers
264-
-a, --all test or list all available browsers
265-
-c, --concurrency <n> number of browsers to test concurrently, default 5
266-
-r, --retries <retries> number of retries when running a browser, default 6
267-
-t, --timeout <timeout> how long to wait for test results, default 5m
268-
--coverage enable code coverage analysis
269-
--live keep browsers open to allow repeated test runs
270-
-p, --preset <preset> select a configuration preset
271-
-s, --server <script> path to script that runs a support server
272-
--loopback <hostname> custom hostname that equals or resolves to 127.0.0.1
273-
--verbose enable airtap debug output
274-
--silly enable all debug output
275-
-h, --help display help for command
262+
-v --version Print version and exit
263+
-l --list-browsers List (effective or --all) browsers
264+
-a --all Test or list all available browsers
265+
--coverage Enable code coverage analysis
266+
--live Keep browsers open to allow repeated test runs
267+
-c --concurrency <n> Number of browsers to test concurrently, default 5
268+
-r --retries <n> Number of retries when running a browser, default 6
269+
-t --timeout <n> How long to wait for test results, default 5m. Can
270+
be a number in milliseconds or a string with unit.
271+
-p --preset <preset> Select a configuration preset
272+
-s --server <script> Path to script that runs a support server
273+
--loopback <host> Custom hostname that equals or resolves to 127.0.0.1
274+
--verbose Enable airtap debug output
275+
--silly Enable all debug output
276+
-h --help Print help and exit.
276277
```
277278

278279
<details><summary>Examples (click to expand)</summary>

bin/airtap.js

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,67 @@ if (process.version.match(/^v(\d+)\./)[1] < 10) {
88

99
require('make-promises-safe')
1010

11-
const program = require('commander')
1211
const nearest = require('find-nearest-file')
1312
const yaml = require('js-yaml')
1413
const os = require('os')
1514
const fs = require('fs')
1615
const path = require('path')
1716
const Airtap = require('../lib/airtap')
18-
const version = require('../package.json').version
1917
const hasOwnProperty = Object.prototype.hasOwnProperty
18+
const argv = require('minimist')(process.argv.slice(2), {
19+
string: [
20+
'concurrency',
21+
'retries',
22+
'timeout',
23+
'preset',
24+
'server',
25+
'loopback'
26+
],
27+
boolean: [
28+
'version',
29+
'help',
30+
'list-browsers',
31+
'all',
32+
'coverage',
33+
'live',
34+
'verbose',
35+
'silly',
36+
37+
// Legacy options (handled below)
38+
'local',
39+
'open',
40+
'electron'
41+
],
42+
alias: {
43+
v: 'version',
44+
h: 'help',
45+
l: 'list-browsers',
46+
a: 'all',
47+
c: 'concurrency',
48+
r: 'retries',
49+
t: 'timeout',
50+
p: 'preset',
51+
s: 'server'
52+
}
53+
})
2054

21-
program
22-
.version(version, '-v, --version', 'print version')
23-
.usage('[options] <files>')
24-
.option('-l, --list-browsers', 'list (effective or --all) browsers')
25-
.option('-a, --all', 'test or list all available browsers')
26-
.option('-c, --concurrency <n>', 'number of browsers to test concurrently, default 5')
27-
.option('-r, --retries <retries>', 'number of retries when running a browser, default 6')
28-
.option('-t, --timeout <timeout>', 'how long to wait for test results, default 5m')
29-
.option('--coverage', 'enable code coverage analysis')
30-
.option('--live', 'keep browsers open to allow repeated test runs')
31-
.option('-p, --preset <preset>', 'select a configuration preset')
32-
.option('-s, --server <script>', 'path to script that runs a support server')
33-
.option('--loopback <hostname>', 'custom hostname that equals or resolves to 127.0.0.1')
34-
.option('--verbose', 'enable airtap debug output')
35-
.option('--silly', 'enable all debug output')
36-
37-
// Can we hide these in help?
38-
.option('--local', 'n/a')
39-
.option('--open', 'n/a')
40-
.option('--electron', 'n/a')
41-
42-
.on('--help', function () {
43-
console.log()
44-
console.log(read('examples.txt'))
45-
})
46-
.parse(process.argv)
55+
if (argv.help) {
56+
console.log(read('help.txt'))
57+
process.exit()
58+
} else if (argv.version) {
59+
console.log(require('../package.json').version)
60+
process.exit()
61+
}
4762

4863
const config = {
4964
watchify: !process.env.CI,
5065
...readYAML(nearest('.airtaprc') || path.join(os.homedir(), '.airtaprc')),
5166
...readYAML('.airtap.yml'),
52-
...wash(program.opts())
67+
...wash(argv)
5368
}
5469

55-
if (program.preset) {
56-
usePreset(config, program.preset)
70+
if (argv.preset) {
71+
usePreset(config, argv.preset)
5772
}
5873

5974
if (config.silly) {
@@ -77,14 +92,14 @@ setCredentials(config, process.env)
7792

7893
const airtap = new Airtap()
7994
const wanted = config.all ? null : config.browsers || []
80-
const files = program.args.length ? program.args : config.files || []
95+
const files = argv._.length ? argv._ : config.files || []
8196

8297
if (!config.providers) {
8398
config.providers = ['airtap-default']
8499
if (wanted) wanted.splice(0, wanted.length, { name: 'default' })
85100
}
86101

87-
if (!files.length && !program.listBrowsers) {
102+
if (!files.length && !argv['list-browsers']) {
88103
fail('At least one file must be specified.', true)
89104
} else if (!config.providers.length) {
90105
fail(read('no-input.txt'), true)
@@ -99,7 +114,7 @@ airtap.provider(config.providers)
99114
airtap.manifests(wanted, function (err, manifests) {
100115
if (err) return fail(err)
101116

102-
if (program.listBrowsers) {
117+
if (argv['list-browsers']) {
103118
manifests.forEach(simplifyManifest)
104119
console.log(toYAML(manifests))
105120
return
@@ -157,8 +172,8 @@ function wash (opts) {
157172
const copy = {}
158173

159174
for (const k in opts) {
160-
if (!hasOwnProperty.call(opts, k)) continue
161-
if (opts[k] != null) copy[k] = opts[k]
175+
if (k.startsWith('_') || !hasOwnProperty.call(opts, k)) continue
176+
if (opts[k] != null && opts[k] !== '') copy[k] = opts[k]
162177
}
163178

164179
return copy

bin/examples.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/help.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Usage: airtap [options] <files>
2+
3+
Options:
4+
-v --version Print version and exit
5+
-l --list-browsers List (effective or --all) browsers
6+
-a --all Test or list all available browsers
7+
--coverage Enable code coverage analysis
8+
--live Keep browsers open to allow repeated test runs
9+
-c --concurrency <n> Number of browsers to test concurrently, default 5
10+
-r --retries <n> Number of retries when running a browser, default 6
11+
-t --timeout <n> How long to wait for test results, default 5m. Can
12+
be a number in milliseconds or a string with unit.
13+
-p --preset <preset> Select a configuration preset
14+
-s --server <script> Path to script that runs a support server
15+
--loopback <host> Custom hostname that equals or resolves to 127.0.0.1
16+
--verbose Enable airtap debug output
17+
--silly Enable all debug output
18+
-h --help Print help and exit.
19+
20+
List all available browsers:
21+
$ airtap -la
22+
23+
Test browsers specified in .airtap.yml:
24+
$ airtap test.js
25+
26+
Test all available browsers (careful):
27+
$ airtap -a test.js
28+
29+
Test multiple files:
30+
$ airtap "test/*.js"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"browserify": "^16.5.2",
3030
"browserify-istanbul": "^3.0.1",
3131
"bruce-millis-option": "^1.0.0",
32-
"commander": "^6.0.0",
3332
"compression": "^1.7.1",
3433
"debug": "^4.1.0",
3534
"engine.io": "^3.4.2",
@@ -43,6 +42,7 @@
4342
"load-script": "^1.0.0",
4443
"make-promises-safe": "^5.1.0",
4544
"maybe-combine-errors": "^1.0.0",
45+
"minimist": "^1.2.5",
4646
"nanoresource": "^1.3.0",
4747
"nanoresource-collection": "^1.0.0",
4848
"on-stream-close": "^1.0.0",

test/integration/fixtures/tap/out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ok 6 (unnamed assert)
1515
ok 7 true is true AWESOME
1616
# failed plan
1717
ok 8 one assert
18-
not ok 9 test timed out after 200ms
18+
not ok 9 failed plan timed out after 200ms
1919
# error
2020
not ok 10 Error: test
2121

0 commit comments

Comments
 (0)