From aca257f09407e91b5de4b3d9f7736d5d487b6c34 Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Wed, 6 Feb 2019 20:47:26 +0100 Subject: [PATCH] feat: allow check/apply commands for commit msgs --- package.json | 2 +- src/cmds/git-commit.js | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 9e470bcb..d2d4cce8 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/cmds/git-commit.js b/src/cmds/git-commit.js index 36198610..c1d4375f 100644 --- a/src/cmds/git-commit.js +++ b/src/cmds/git-commit.js @@ -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 [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], @@ -39,7 +66,7 @@ exports.handler = async function(argv) { process.exit(1) } } catch (err) { - process.stderr.write(err) + log.error(err) process.exit(1) } }