将 husky 预提交挂钩添加到 package.json

问题描述

我正在处理一个项目,我希望每次尝试提交新更改时,都会运行我的测试,并根据此决定是否会发生提交。为此,我进行了研究并发现我可以使用 husky's pre-commit hook

我首先在我的项目中安装了 husky npm i husky --save-dev 在我的 "husky": "^6.0.0" 中安装了 package.json

然后我按照教程在package.json添加了以下对象

 "husky": {
    "hooks": {
      "applypatch-msg": "echo \"[Husky] applypatch-msg\"","pre-applypatch": "echo \"[Husky] pre-applypatch\"","post-applypatch": "echo \"[Husky] post-applypatch\"","pre-commit": "echo \"[Husky] pre-commit\""
    }
  }

如您所见,运行 git commit -m "some message!" 应该会回显一堆东西,这意味着 husky 的 pre-commit 钩子正在工作,但没有类似的东西得到回显。现在我不知道为什么这不起作用。如果它有效,我会继续在预提交钩子中添加脚本来运行我的测试。

顺便说一下,这是 package.json 文件

{
  "name": "test app","version": "1.0.1","description": "test app","main": "index.js","scripts": {
    "start": "node index.js","dev": nodemon index.js","test": "find ./plugins -name '*test.js' | xargs mocha -R spec"
  },"dependencies": {
    "@hapi/boom": "^9.0.0","@hapi/glue": "^7.0.0","@hapi/good": "^9.0.0","@hapi/good-console": "^9.0.0","@hapi/good-squeeze": "^6.0.0","@hapi/hapi": "^19.1.1","@hapi/joi": "^17.1.1","axios": "^0.19.2","babel-eslint": "^10.1.0","base-64": "0.1.0","confit": "2.3.0","hapi-auth-jwt2": "^8.8.1","hapi-mongodb": "^9.0.0","jws": "4.0.0","mongodb": "^3.5.7","pad-left": "2.1.0","pino": "^5.16.0","query-string": "^6.13.1","redis": "^2.8.0","selectn": "^1.1.2","superagent": "3.8.2","utf8": "^3.0.0","uuid": "^3.4.0",},"devDependencies": {
    "chai": "^4.1.2","chai-datetime": "^1.6.0","chai-http": "^4.0.0","husky": "^6.0.0","mocha": "^5.1.1","nodemon": "^2.0.4","proxyquire": "^2.0.1","sinon": "^5.0.10","sinon-test": "^2.2.0"
  },"husky": {
    "hooks": {
      "applypatch-msg": "echo \"[Husky] applypatch-msg\"","pre-commit": "echo \"[Husky] pre-commit\""
    }
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)