Skip to content

Commit 30145f9

Browse files
lholmquistrichardlau
authored andcommitted
bin: add a flag to list the subsystems (#67)
Adds the flag --list-subsystems to list all the subsystems that are available for use. Fixes: #66
1 parent 671125e commit 30145f9

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

bin/cmd.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ const formatTap = require('../lib/format-tap')
1414
const Validator = require('../lib')
1515
const Tap = require('../lib/tap')
1616
const utils = require('../lib/utils')
17+
const subsystem = require('../lib/rules/subsystem')
1718
const knownOpts = { help: Boolean
1819
, version: Boolean
1920
, 'validate-metadata': Boolean
2021
, tap: Boolean
2122
, out: path
2223
, list: Boolean
24+
, 'list-subsystems': Boolean
2325
}
2426
const shortHand = { h: ['--help']
2527
, v: ['--version']
2628
, V: ['--validate-metadata']
2729
, t: ['--tap']
2830
, o: ['--out']
2931
, l: ['--list']
32+
, ls: ['--list-subsystems']
3033
}
3134

3235
const parsed = nopt(knownOpts, shortHand)
@@ -84,6 +87,11 @@ function loadPatch(uri, cb) {
8487

8588
const v = new Validator(parsed)
8689

90+
if (parsed['list-subsystems']) {
91+
utils.describeSubsystem(subsystem.defaults.subsystems.sort())
92+
return
93+
}
94+
8795
if (parsed.list) {
8896
const ruleNames = Array.from(v.rules.keys())
8997
const max = ruleNames.reduce((m, item) => {

bin/usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ core-validate-commit - Validate the commit message for a particular commit in no
88
-V, --validate-metadata validate PR-URL and reviewers (on by default)
99
-t, --tap output in tap format
1010
-l, --list list rules and their descriptions
11+
-ls --list-subsystems list the available subsystems
1112

1213
examples:
1314
Validate a single sha:

lib/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ exports.describeRule = function describeRule(rule, max = 20) {
4545
console.log(' %s %s', chalk.red(title), chalk.dim(desc))
4646
}
4747
}
48+
49+
exports.describeSubsystem = function describeSubsystem(subsystems, max = 20) {
50+
if (subsystems) {
51+
for (let sub = 0; sub < subsystems.length; sub = sub + 3) {
52+
console.log('%s %s %s',
53+
chalk.green(exports.leftPad(subsystems[sub] || '', max)),
54+
chalk.green(exports.leftPad(subsystems[sub + 1] || '', max)),
55+
chalk.green(exports.leftPad(subsystems[sub + 2] || '', max))
56+
)
57+
}
58+
}
59+
}

test/cli-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { spawn } = require('child_process')
5+
const subsystems = require('../lib/rules/subsystem')
6+
7+
test('Test cli flags', (t) => {
8+
t.test('test list-subsystems', (tt) => {
9+
const ls = spawn('./bin/cmd.js', ['--list-subsystems'])
10+
let compiledData = ''
11+
ls.stdout.on('data', (data) => {
12+
compiledData += data
13+
})
14+
15+
ls.stderr.on('data', (data) => {
16+
tt.fail('This should not happen')
17+
})
18+
19+
ls.on('close', (code) => {
20+
// Get the list of subsytems as an Array.
21+
// Need to match words that also have the "-" in them
22+
const subsystemsFromOutput = compiledData.match(/[\w'-]+/g)
23+
const defaultSubsystems = subsystems.defaults.subsystems
24+
25+
tt.equal(subsystemsFromOutput.length,
26+
defaultSubsystems.length,
27+
'Should have the same length')
28+
29+
// Loop through the output list and compare with the real list
30+
// to make sure they are all there
31+
const missing = []
32+
subsystemsFromOutput.forEach((sub) => {
33+
if (!defaultSubsystems.find((x) => {return x === sub})) {
34+
missing.push(sub)
35+
}
36+
})
37+
38+
tt.equal(missing.length, 0, 'Should have no missing subsystems')
39+
tt.end()
40+
})
41+
})
42+
43+
t.end()
44+
})

0 commit comments

Comments
 (0)