Skip to content

Commit

Permalink
Fixes ctdio#16 - Implement command line argument parsing using argly.
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkelleher committed Sep 8, 2017
1 parent 8c9b43f commit 1309590
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ To run your tests, you can pass in the test files to the exposed cli tool. A glo
on the shell you are using.

```
npx mocha-puppeteer ./tests/*.js
Usage: mocha-puppeteer [OPTIONS]
Examples:
Test a single file: "mocha-puppeteer /foo/bar-test.js"
Test a series of files using a glob pattern: "mocha-puppeteer /foo/**/*-test.js"
Options:
--help -h Show this help message [string]
--version -v Show the version number of mocha-puppeteer [string]
--pattern -p * Pattern to run tests. Either a single file or glob pattern. [string]
```

## Contributing
Expand Down
10 changes: 9 additions & 1 deletion bin/mocha-puppeteer
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#!/usr/bin/env node
const cli = require('../cli')

require('../cli')()
;(async () => {
try {
await cli()
} catch (err) {
console.error(err)
process.exit(1)
}
})()
48 changes: 44 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const argly = require('argly')
const mochaPuppeteer = require('./index')
const _loadConfig = require('./src/utils/loadConfig')

const args = process.argv.slice(2)
const mochaPuppeteerPkgVersion = require('./package.json').version

// pick specific values from config
function _pickConfig (config) {
Expand All @@ -11,17 +12,56 @@ function _pickConfig (config) {
/* eslint-enable */
}

const parser = argly
.createParser({
'--help -h': {
type: 'string',
description: 'Show this help message'
},
'--version -v': {
type: 'string',
description: 'Show the version number of mocha-puppeteer'
},
'--pattern -p *': {
type: 'string',
description: 'Pattern to run tests. Either a single file or glob pattern.'
}
})
.example('Test a single file: "mocha-puppeteer /foo/bar-test.js"')
.example('Test a series of files using a glob pattern: "mocha-puppeteer /foo/*/*-test.js"')
.validate(function (result) {
if (result.help) {
this.printUsage()
} else if (result.version) {
console.log(`mocha-puppeteer@${mochaPuppeteerPkgVersion}`)
} else if (!result.pattern) {
this.printUsage()
}
})
.onError(function (err) {
this.printUsage()
throw new Error('Error parsing command line args: ', err)
})

module.exports = async function runCli () {
const {
pattern,
help,
version
} = parser.parse()

// Gracefully exit if either the "help" or "version" arguments are supplied
// of if the "pattern" argument is missing because it's required.
if (help || version || !pattern) return

try {
const config = await _loadConfig({
startingDirectory: process.cwd()
})

const options = config ? _pickConfig(config) : {}

Object.assign(options, {
testFiles: args
})
Object.assign(options, { testFiles: [ pattern ] })

await mochaPuppeteer.runTests(options)
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"author": "Charlie Duong",
"license": "MIT",
"dependencies": {
"argly": "^1.2.0",
"babel-plugin-istanbul": "^4.1.4",
"bluebird": "^3.5.0",
"koa": "^2.3.0",
Expand Down

0 comments on commit 1309590

Please sign in to comment.