From 100a3d245e9a545271f098b15bc97ab1b22d8082 Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Tue, 27 Sep 2016 06:05:33 -0500 Subject: [PATCH] bin: add --list flag to list all rules It is useful to know what rules exist. In the future, these need to be documented. Semver: minor --- README.md | 10 ++++++++++ bin/cmd.js | 16 ++++++++++++++++ bin/usage.txt | 1 + lib/utils.js | 16 ++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 6e9c5ae..75d802d 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,16 @@ $ core-validate-commit # validate since $ git rev-list ..HEAD | xargs core-validate-commit + +# list all rules +$ core-validate-commit --list + fixes-url enforce format of Fixes URLs + line-length enforce max length of lines in commit body + metadata-end enforce that metadata is at the end of commit messages + pr-url enforce PR-URL + reviewers enforce having reviewers + subsystem enforce subsystem validity + title-length enforce max length of commit title ``` ## Test diff --git a/bin/cmd.js b/bin/cmd.js index efc4812..c3d5a90 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -13,17 +13,20 @@ const pretty = require('../lib/format-pretty') const formatTap = require('../lib/format-tap') const Validator = require('../lib') const Tap = require('../lib/tap') +const utils = require('../lib/utils') const knownOpts = { help: Boolean , version: Boolean , 'validate-metadata': Boolean , tap: Boolean , out: path + , list: Boolean } const shortHand = { h: ['--help'] , v: ['--version'] , V: ['--validate-metadata'] , t: ['--tap'] , o: ['--out'] + , l: ['--list'] } const parsed = nopt(knownOpts, shortHand) @@ -79,6 +82,19 @@ function loadPatch(uri, cb) { const v = new Validator(parsed) +if (parsed.list) { + const ruleNames = Array.from(v.rules.keys()) + const max = ruleNames.reduce((m, item) => { + if (item.length > m) m = item.length + return m + }, 0) + + for (const rule of v.rules.values()) { + utils.describeRule(rule, max) + } + return +} + if (parsed.tap) { const stream = parsed.out ? fs.createWriteStream(parsed.out) diff --git a/bin/usage.txt b/bin/usage.txt index d8d936f..0e2efba 100644 --- a/bin/usage.txt +++ b/bin/usage.txt @@ -7,6 +7,7 @@ core-validate-commit - Validate the commit message for a particular commit in no -v, --version show version -V, --validate-metadata validate PR-URL and revieweres (on by default) -t, --tap output in tap format + -l, --list list rules and their descriptions examples: Validate a single sha: diff --git a/lib/utils.js b/lib/utils.js index 9f766ad..8f32e0c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -17,6 +17,14 @@ exports.rightPad = function rightPad(str, max) { return str } +exports.leftPad = function leftPad(str, max) { + const diff = max - str.length + 1 + if (diff > 0) { + return `${' '.repeat(diff)}${str}` + } + return str +} + exports.header = (sha, status) => { switch (status) { case 'skip': @@ -29,3 +37,11 @@ exports.header = (sha, status) => { return `${X} ${chalk.underline(sha)}` } } + +exports.describeRule = function describeRule(rule, max = 20) { + if (rule.meta && rule.meta.description) { + const desc = rule.meta.description + const title = exports.leftPad(rule.id, max) + console.log(' %s %s', chalk.red(title), chalk.dim(desc)) + } +}