带有husky的package.json中的自定义git钩子

问题描述:

我正在尝试在提交时验证提交消息.为此,我使用了Husky和commit-msg钩子.

I am trying to validate the commit message at commit. For that, I am using Husky and the commit-msg hook.

但是,由于我也在构建时提交了消息验证,因此我希望验证代码在单独的JS文件中可用.所以我试图调用一个外部JS文件来执行我的提交验证.在我的package.json文件中,我有:

However, as I also do commit message validation at build time, I want the validation code to be available in a separate JS file. So I am trying to call an external JS file to perform my commit validation. In my package.json file, I have:

"commitmsg": "node validation.js"

但是,我无法正确执行验证.现在,validation.js如下所示:

However, I cannot get the validation to be performed properly. Right now, validation.js looks like this:

console.log('Here');
const config = (a, b) => {
  console.log(a);
  console.log(b);
};

module.exports = config;

显示

Here,但未调用函数中的console.log.

Here is displayed, but the console.logs in the function are not called.

有什么想法可以让我的函数被调用吗?另外,如何访问提交消息?

Any idea how I can get my function to get called? Also, how can I access the commit message?

我很傻,我找到了解决方案.以防将来对其他人有用:

I was being silly, I found the solution. In case it is useful to somebody else in the future:

const myRegex = new RegExp('.*');
const commitMsg = require('fs').readFileSync(process.env.HUSKY_GIT_PARAMS, 'utf8');

if (!myRegex.test(commitMsg) ) {
  console.error(`Invalid commit message!`);
  process.exit(1);
}