Skip to content

Commit

Permalink
Add bump command to add a new version to changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Oct 31, 2020
1 parent 6469e03 commit 9e2b53b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 43 deletions.
42 changes: 27 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
This module saves you time in three ways:

- **No configuration.** The easiest way to enforce markdown code quality in your project. No decisions to make. No `remark` plugins to manage.
- **Automatically format markdown.** Run `hallmark --fix` to format markdown, wrap GitHub issues and usernames in links, autocomplete a `CHANGELOG.md` following [Keep A Changelog](https://keepachangelog.com/en/1.0.0/), and more.
- **Automatically format markdown.** Run `hallmark fix` to format markdown, wrap GitHub issues and usernames in links, autocomplete a `CHANGELOG.md` following [Keep A Changelog](https://keepachangelog.com/en/1.0.0/), and more.
- **Catch style issues & mistakes early.** Save code review time by eliminating back-and-forth between reviewer & contributor.

## Quick Start
Expand All @@ -52,16 +52,22 @@ Lint `*.md` files:
hallmark
```

Lint and fix:
Fix markdown files in place:

```
hallmark --fix
hallmark fix
```

Lint and fix custom files:
Fix custom files:

```
hallmark --fix CHANGELOG.md docs/*.md
hallmark fix CHANGELOG.md docs/*.md
```

Add new minor version to changelog:

```
hallmark bump minor
```

## What You Might Do
Expand Down Expand Up @@ -92,8 +98,8 @@ README.md

## Requirements

- A `package.json` must exist, with a [`repository`](https://docs.npmjs.com/files/package.json#repository) property
- The working directory must be a git repository.
- The working directory must be a git repository
- It must either contain a `package.json` with a [`repository`](https://docs.npmjs.com/files/package.json#repository) property, or have a git `origin` remote

## Rules

Expand Down Expand Up @@ -125,18 +131,24 @@ README.md

## Usage

`hallmark [options] [pattern ...]`
`hallmark [command] [options] [pattern ...]`

Lint or fix files in the current working directory. By default `hallmark` includes files matching `*.md`. Pass one or more glob patterns to override this. Files matching `.gitignore` patterns are ignored. To ignore additional files, use the `--ignore / -i` option.

Commands:

Lint or fix files in the current working directory. By default `hallmark` includes files matching `*.md`. Pass one or more glob patterns to override this. A pattern can either be a path to a file or a glob pattern. Files matching `.gitignore` patterns are ignored. To ignore additional files, use the `--ignore / -i` option.
- `lint`: lint markdown files (default)
- `fix`: fix markdown files
- `bump <target>`: add new entry to changelog. Target must be a release type (major, minor, patch, premajor, preminor, prepatch, prerelease) or a version.

Options:

- `--fix`: fix issues. Modifies files in place.
- `--ignore / -i <pattern>`: files to ignore. Repeat to specify multiple patterns (e.g. `-i a.md -i docs/*.md`). Can also be configured via [Package Options](#package-options).
- `--help`: print usage and exit
- `--version`: print version and exit
- `--report <reporter>`: see [Reporters](#reporters)
- `--[no-]color`: force color in report (detected by default)
- `--fix`: backwards-compatible alias for fix command

## Package Options

Expand All @@ -156,7 +168,7 @@ You can add a `hallmark` object to your `package.json` with additional configura

### `ignore`

A string or array of files to ignore. Merged with `--ignore / -i` if any (see [Usage](#usage)).
A string or array of files to ignore. Merged with `--ignore / -i` if any.

### `validateLinks`

Expand All @@ -176,11 +188,11 @@ String or array. See [Contributors Table](#contributors-table) for details. Alia

### `plugins`

An array of extra plugins, to be applied in both lint and `--fix` mode.
An array of extra plugins, to be applied in both lint and fix mode.

### `fixers`

An array of extra plugins, to be applied in `--fix` mode.
An array of extra plugins, to be applied in fix mode.

## Opt-in Features

Expand All @@ -194,7 +206,7 @@ Add this heading to a markdown file:
## Table of Contents
```

Running `hallmark --fix` will then create or update a table of contents.
Running `hallmark fix` will then create or update a table of contents.

### Contributors Table

Expand All @@ -212,7 +224,7 @@ Or this heading to a `README.md`:
## Contributors
```

Running `hallmark --fix` will then render contributors from `git` history to a markdown table. To add links to GitHub and social profiles of contributors, add the `contributors` [Package Option](#package-options):
Running `hallmark fix` will then render contributors from `git` history to a markdown table. To add links to GitHub and social profiles of contributors, add the `contributors` [Package Option](#package-options):

```json
"hallmark": {
Expand Down
30 changes: 20 additions & 10 deletions USAGE
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
Usage: hallmark [options] [pattern ...]
Usage: hallmark [command] [options] [pattern ...]

Markdown Style Guide, with linter and automatic fixer.
Lint or fix files in the current working directory. By default hallmark
includes files matching "*.md". Pass one or more glob patterns to override
this. Files matching .gitignore patterns are ignored. To ignore additional
files, use the --ignore option.

Commands:

lint Lint markdown files (default)
fix Fix markdown files
bump <target> Add new entry to changelog. Target must be a
release type (major, minor, patch, premajor,
preminor, prepatch, prerelease) or a version.

Options:

--fix Fix issues. Modifies files in place.
-i --ignore <pattern> Files to ignore. Repeat to specify multiple patterns
-h --help Print usage and exit
-v --version Print version and exit
--report <reporter> Specify reporter
--[no-]color Force color in report (detected by default)
--fix Backwards-compatible alias for fix command

Examples:

# Lint *.md files
$ hallmark

# Lint and fix
$ hallmark --fix
# Fix *.md files
$ hallmark fix

# Lint and fix custom files
$ hallmark --fix CHANGELOG.md docs/*.md
# Fix custom files
$ hallmark fix CHANGELOG.md docs/*.md

# Use a custom reporter
$ hallmark --report json
$ hallmark --report [ json --pretty ]
# Add new minor version to changelog
$ hallmark bump minor
51 changes: 40 additions & 11 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,49 @@ const argv = require('subarg')(process.argv.slice(2), {
})

if (argv.help) {
const fs = require('fs')
const path = require('path')
const usage = path.join(__dirname, 'USAGE')

fs.createReadStream(usage).pipe(process.stdout)
usage()
} else if (argv.version) {
console.log(require('./package.json').version)
} else {
if (argv._.length) {
argv.files = argv._
const rest = argv._

if (rest[0] === 'lint') {
argv.files = files(rest.slice(1))
require('./index.js').lint(argv, done)
} else if (rest[0] === 'fix') {
argv.files = files(rest.slice(1))
require('./index.js').fix(argv, done)
} else if (rest[0] === 'bump') {
const target = rest[1]

if (!target) {
usage()
process.exit(1)
}

argv.files = files(rest.slice(2))
require('./index.js').bump(target, argv, done)
} else {
// Old usage (no commands)
// TODO: deprecate?
argv.files = files(rest)
require('./index.js')[argv.fix ? 'fix' : 'lint'](argv, done)
}
}

require('./index.js')(argv, function (err, code) {
if (err) throw err
process.exit(code)
})
function files (rest) {
return rest.length ? rest : null
}

function done (err, result) {
if (err) throw err
process.exit(result.code)
}

function usage () {
const fs = require('fs')
const path = require('path')
const usage = path.join(__dirname, 'USAGE')

fs.createReadStream(usage).pipe(process.stdout)
}
51 changes: 44 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ const processor = require('remark')
const path = require('path')
const fs = require('fs')

module.exports = function hallmark (options, callback) {
if (typeof options === 'function') {
return hallmark({}, options)
}

function hallmark (options, callback) {
const fix = !!options.fix
const cwd = path.resolve(options.cwd || '.')
const pkg = read('package.json', cwd) || {}
Expand All @@ -25,7 +21,7 @@ module.exports = function hallmark (options, callback) {
if (err) throw err

if (!files.length) {
callback(null, 0)
callback(null, { code: 0, files: [] })
return
}

Expand Down Expand Up @@ -107,10 +103,51 @@ module.exports = function hallmark (options, callback) {
frail: true,
// "Do not report successful files"
quiet: true
}, callback)
}, function (err, code, context) {
if (err) return callback(err)
callback(null, { code, files: context.files })
})
})
}

exports.lint = function (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

hallmark({ ...options, fix: false }, callback)
}

exports.fix = function (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

hallmark({ ...options, fix: true }, callback)
}

exports.bump = function (target, options, callback) {
if (!target) {
throw new TypeError('First argument "target" is required')
} else if (typeof target !== 'string') {
throw new TypeError('First argument "target" must be a string')
}

if (typeof options === 'function') {
callback = options
options = {}
}

const changelog = {
...options.changelog,
add: target
}

hallmark({ ...options, changelog, fix: true }, callback)
}

function read (file, cwd) {
const fp = find.sync(file, cwd, 100)
return fp ? JSON.parse(fs.readFileSync(fp, 'utf8')) : null
Expand Down

0 comments on commit 9e2b53b

Please sign in to comment.