Skip to content

Commit aca257f

Browse files
committed
feat: allow check/apply commands for commit msgs
1 parent 6988cf9 commit aca257f

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"husky": {
3030
"hooks": {
31-
"commit-msg": "./bin/d2-style commit",
31+
"commit-msg": "./bin/d2-style commit apply",
3232
"pre-commit": "./bin/d2-style js apply"
3333
}
3434
}

src/cmds/git-commit.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,45 @@ const load = require('@commitlint/load')
33
const lint = require('@commitlint/lint')
44
const format = require('@commitlint/format')
55

6-
exports.command = 'commit'
6+
const log = require('@dhis2/cli-helpers-engine').reporter
77

8-
exports.describe = 'Format commit messages according to DHIS2 rules.'
8+
exports.command = 'commit <cmd> [msg]'
99

10-
exports.builder = {}
10+
exports.describe = 'Format commit messages according to standards.'
11+
12+
exports.builder = {
13+
cmd: {
14+
describe: 'check or apply style',
15+
choices: ['check', 'apply'],
16+
type: 'string',
17+
},
18+
msg: {
19+
describe: 'arbitrary string to check',
20+
type: 'string',
21+
},
22+
}
1123

1224
exports.handler = async function(argv) {
1325
const config = require('@commitlint/config-conventional')
26+
const { cmd, msg } = argv
27+
28+
const check = cmd === 'check'
29+
const apply = cmd === 'apply'
30+
31+
if (check && !msg) {
32+
log.error('A commit msg is required when using check')
33+
process.exit(1)
34+
}
1435

1536
try {
1637
const opts = await load(config)
17-
const commit = await read({ edit: true })
38+
39+
let commit
40+
if (apply) {
41+
commit = await read({ edit: true })
42+
} else {
43+
commit = [msg]
44+
}
1845

1946
const report = await lint(
2047
commit[0],
@@ -39,7 +66,7 @@ exports.handler = async function(argv) {
3966
process.exit(1)
4067
}
4168
} catch (err) {
42-
process.stderr.write(err)
69+
log.error(err)
4370
process.exit(1)
4471
}
4572
}

0 commit comments

Comments
 (0)