Skip to content

Commit

Permalink
feat: allow check/apply commands for commit msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
varl committed Feb 6, 2019
1 parent 6988cf9 commit aca257f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"husky": {
"hooks": {
"commit-msg": "./bin/d2-style commit",
"commit-msg": "./bin/d2-style commit apply",
"pre-commit": "./bin/d2-style js apply"
}
}
Expand Down
37 changes: 32 additions & 5 deletions src/cmds/git-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,45 @@ const load = require('@commitlint/load')
const lint = require('@commitlint/lint')
const format = require('@commitlint/format')

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

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

exports.builder = {}
exports.describe = 'Format commit messages according to standards.'

exports.builder = {
cmd: {
describe: 'check or apply style',
choices: ['check', 'apply'],
type: 'string',
},
msg: {
describe: 'arbitrary string to check',
type: 'string',
},
}

exports.handler = async function(argv) {
const config = require('@commitlint/config-conventional')
const { cmd, msg } = argv

const check = cmd === 'check'
const apply = cmd === 'apply'

if (check && !msg) {
log.error('A commit msg is required when using check')
process.exit(1)
}

try {
const opts = await load(config)
const commit = await read({ edit: true })

let commit
if (apply) {
commit = await read({ edit: true })
} else {
commit = [msg]
}

const report = await lint(
commit[0],
Expand All @@ -39,7 +66,7 @@ exports.handler = async function(argv) {
process.exit(1)
}
} catch (err) {
process.stderr.write(err)
log.error(err)
process.exit(1)
}
}

0 comments on commit aca257f

Please sign in to comment.