Skip to content

Commit 4ad6edc

Browse files
authored
bin: add --list flag to list all rules (#8)
It is useful to know what rules exist. In the future, these need to be documented. Semver: minor
1 parent 04fde36 commit 4ad6edc

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ $ core-validate-commit <sha>
1919

2020
# validate since <sha>
2121
$ git rev-list <sha>..HEAD | xargs core-validate-commit
22+
23+
# list all rules
24+
$ core-validate-commit --list
25+
fixes-url enforce format of Fixes URLs
26+
line-length enforce max length of lines in commit body
27+
metadata-end enforce that metadata is at the end of commit messages
28+
pr-url enforce PR-URL
29+
reviewers enforce having reviewers
30+
subsystem enforce subsystem validity
31+
title-length enforce max length of commit title
2232
```
2333

2434
## Test

bin/cmd.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ const pretty = require('../lib/format-pretty')
1313
const formatTap = require('../lib/format-tap')
1414
const Validator = require('../lib')
1515
const Tap = require('../lib/tap')
16+
const utils = require('../lib/utils')
1617
const knownOpts = { help: Boolean
1718
, version: Boolean
1819
, 'validate-metadata': Boolean
1920
, tap: Boolean
2021
, out: path
22+
, list: Boolean
2123
}
2224
const shortHand = { h: ['--help']
2325
, v: ['--version']
2426
, V: ['--validate-metadata']
2527
, t: ['--tap']
2628
, o: ['--out']
29+
, l: ['--list']
2730
}
2831

2932
const parsed = nopt(knownOpts, shortHand)
@@ -79,6 +82,19 @@ function loadPatch(uri, cb) {
7982

8083
const v = new Validator(parsed)
8184

85+
if (parsed.list) {
86+
const ruleNames = Array.from(v.rules.keys())
87+
const max = ruleNames.reduce((m, item) => {
88+
if (item.length > m) m = item.length
89+
return m
90+
}, 0)
91+
92+
for (const rule of v.rules.values()) {
93+
utils.describeRule(rule, max)
94+
}
95+
return
96+
}
97+
8298
if (parsed.tap) {
8399
const stream = parsed.out
84100
? fs.createWriteStream(parsed.out)

bin/usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ core-validate-commit - Validate the commit message for a particular commit in no
77
-v, --version show version
88
-V, --validate-metadata validate PR-URL and revieweres (on by default)
99
-t, --tap output in tap format
10+
-l, --list list rules and their descriptions
1011

1112
examples:
1213
Validate a single sha:

lib/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ exports.rightPad = function rightPad(str, max) {
1717
return str
1818
}
1919

20+
exports.leftPad = function leftPad(str, max) {
21+
const diff = max - str.length + 1
22+
if (diff > 0) {
23+
return `${' '.repeat(diff)}${str}`
24+
}
25+
return str
26+
}
27+
2028
exports.header = (sha, status) => {
2129
switch (status) {
2230
case 'skip':
@@ -29,3 +37,11 @@ exports.header = (sha, status) => {
2937
return `${X} ${chalk.underline(sha)}`
3038
}
3139
}
40+
41+
exports.describeRule = function describeRule(rule, max = 20) {
42+
if (rule.meta && rule.meta.description) {
43+
const desc = rule.meta.description
44+
const title = exports.leftPad(rule.id, max)
45+
console.log(' %s %s', chalk.red(title), chalk.dim(desc))
46+
}
47+
}

0 commit comments

Comments
 (0)