自定义 git hook in package.json with husky

Custom git hook in package.json with husky

我正在尝试在提交时验证提交消息。为此,我正在使用 Husky 和 ​​commit-msg 挂钩。

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

"commitmsg": "node validation.js"

但是,我无法正确执行验证。现在,validation.js 看起来像这样:

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

module.exports = config;
显示了

Here,但是没有调用函数中的console.log

知道如何调用我的函数吗?另外,如何访问提交消息?

我是傻了,我找到了解决办法。以防将来对其他人有用:

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);
}