Skip to content

Commit

Permalink
Refactor yargs (print help when no command; use commandDir) (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas authored Oct 13, 2020
1 parent fdc3b49 commit 40b979e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ This repository is managed by the [Package Maintenance Working Group](https://gi

### Github Token

Wiby requires an environment variable `GITHUB_TOKEN` set to a Github access token. This token needs to be granted push permissions to the dependent repos.
wiby requires an environment variable `GITHUB_TOKEN` set to a Github access token. This token needs to be granted push permissions to the dependent repos.

Example: `export GITHUB_TOKEN=XXXXX`

For more information on creating a github token see [Github's docs](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)

### Running location

Wiby is designed to be run from inside the folder containing your source code. This folder needs to be a git repository with a `package.json` that contains information about the packages source on Github.
wiby is designed to be run from inside the folder containing your source code. This folder needs to be a git repository with a `package.json` that contains information about the packages source on Github.
Example:

```
Expand Down
27 changes: 18 additions & 9 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
## wiby.js test
# Usage


## `wiby result`


Use this command to test your breaking changes against any one of your
dependents. Wiby will go off to the dependent's repo and create a branch with a
patch to the `package.json` pointing to your latest version (with the new
changes) triggering the dependents CI to run.

Use this command to fetch the results of your latest test against a dependent.
wiby will go off to the dependent’s repo and fetch the results of the CI run
against the patch branch wiby had created.

```
Options:
Expand All @@ -12,15 +16,20 @@ Options:
--dependent URL of a dependent [string] [required]
```

## wiby.js result

Use this command to fetch the results of your latest test against a dependent.
Wiby will go off to the dependent's repo and fetch the results of the CI run
against the patch branch Wiby had created
## `wiby test`



Use this command to test your breaking changes against any one of your
dependents. wiby will go off to the dependent’s repo and create a branch with a
patch to the `package.json` pointing to your latest version (with the new
changes) triggering the dependent’s CI to run.

```
Options:
--version Show version number [boolean]
--help Show help [boolean]
--dependent URL of a dependent [string] [required]
```

14 changes: 14 additions & 0 deletions bin/commands/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const wiby = require('../..')

exports.desc = 'Use this command to fetch the results of your latest test against a dependent. wiby will go off to the dependent’s repo and fetch the results of the CI run against the patch branch wiby had created.'

exports.builder = (yargs) => yargs
.option('dependent', {
desc: 'URL of a dependent',
demandOption: true,
type: 'string'
})

exports.handler = (params) => wiby.result(params.dependent)
14 changes: 14 additions & 0 deletions bin/commands/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const wiby = require('../..')

exports.desc = 'Use this command to test your breaking changes against any one of your dependents. wiby will go off to the dependent’s repo and create a branch with a patch to the `package.json` pointing to your latest version (with the new changes) triggering the dependent’s CI to run.'

exports.builder = (yargs) => yargs
.option('dependent', {
desc: 'URL of a dependent',
demandOption: true,
type: 'string'
})

exports.handler = (params) => wiby.test(params.dependent)
36 changes: 18 additions & 18 deletions bin/generate-usage.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/usr/bin/env node

/**
* Builds new content for USAGE.md page according to wiby.js --help commands list
* Builds new content for USAGE.md page according to wiby --help commands list
*/

const { execSync } = require('child_process')
const path = require('path')
const wibyCommand = path.join(__dirname, '..', 'bin', 'wiby.js')
const wibyCommand = path.join(__dirname, '..', 'bin', 'wiby')
const cwd = path.join(__dirname, '..')
const fs = require('fs')

/**
* Parses wiby.js --help output and returns block with commands list
* Parses wiby --help output and returns block with commands list
*/
const getGeneralHelpOutput = () => {
const helpOutput = execSync(`${wibyCommand} --help`, { cwd: cwd }).toString()
const helpParseResult = /(Commands:\n)([\S\s]+)(Options:)/.exec(helpOutput)

if (helpParseResult !== null) {
return helpParseResult[2].trim()
return helpParseResult[2]
} else {
throw new Error('wiby help command parsing failed')
}
Expand All @@ -29,7 +29,7 @@ const getGeneralHelpOutput = () => {
*/
const parseCommandsListOutput = (commandsOutput) => {
// parse commands list
const re = /wiby\.js ([\w]+)/g
const re = /^ {2}wiby ([\w]+)/gm
const commandsList = []
let commandsParseResult
while ((commandsParseResult = re.exec(commandsOutput)) !== null) {
Expand Down Expand Up @@ -59,28 +59,28 @@ const getCommandsHelp = (commandsList) => {
return commandsHelp
}

const renderCommand = (command) => {
return `
## \`wiby ${command.get('commandName')}\`
${command.get('help').replace(/^wiby.*$/m, '').replace(/Options:(.+)$/s, '```\n$&\n```')}
`
}

/**
* Generates new markdown for USAGE page
*/
const generateUsageMd = (commandsData) => {
let usageMd = ''
commandsData.map((command) => {
usageMd += `
## ${command.get('commandName')}
\`\`\`
${command.get('help')}
\`\`\`
const renderUsage = (commandsData) => {
return `# Usage
${commandsData.map((command) => renderCommand(command)).join('\n')}
`
})

return usageMd
}

const commandsList = getGeneralHelpOutput()
const commandsListParsed = parseCommandsListOutput(commandsList)
const commandsHelp = getCommandsHelp(commandsListParsed)
const resultMd = generateUsageMd(commandsHelp)
const resultMd = renderUsage(commandsHelp)

// write result to USAGE.md
fs.writeFileSync(path.join(__dirname, '..', 'USAGE.md'), resultMd)
30 changes: 30 additions & 0 deletions bin/wiby
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

require('dotenv').config()

const chalk = require('chalk')
const yargs = require('yargs')

yargs
.commandDir('./commands')
.demandCommand()
.help()
.strict()
.fail((msg, err, yargs) => {

if (err) {
console.error(err);
}

if (msg) {
console.error(chalk.red.bold(msg));
console.error();
}

if (!err || err.showHelp) {
yargs.showHelp();
}

process.exit(1);
})
.parse()
48 changes: 0 additions & 48 deletions bin/wiby.js

This file was deleted.

5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

exports.test = require('./test')

exports.result = require('./result')
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
"version": "0.1.0",
"description": "Will I Break You?",
"bin": {
"wiby": "bin/wiby.js"
"wiby": "bin/wiby"
},
"main": "bin/wiby.js",
"main": "lib/index.js",
"directories": {
"test": "test"
},
"scripts": {
"lint": "standard",
"pretest": "npm run lint",
"test": "npm run tests-only",
"tests-only": "tap"
"tests-only": "tap",
"generate-docs": "./bin/generate-usage.js"
},
"repository": {
"type": "git",
Expand All @@ -28,6 +29,7 @@
"dependencies": {
"@octokit/graphql": "^4.5.0",
"@octokit/rest": "^18.0.0",
"chalk": "^4.1.0",
"dotenv": "^8.2.0",
"git-url-parse": "^11.1.2",
"node-fetch": "^2.6.0",
Expand Down
2 changes: 1 addition & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const tap = require('tap')
const childProcess = require('child_process')
const path = require('path')

const wibyCommand = path.join(__dirname, '..', 'bin', 'wiby.js')
const wibyCommand = path.join(__dirname, '..', 'bin', 'wiby')
const cwd = path.join(__dirname, '..')

tap.test('test command', async (tap) => {
Expand Down

0 comments on commit 40b979e

Please sign in to comment.