我在项目中添加了一些预提交和预推送脚本。我取消Husky的名称是因为它会在git上跟踪任何更改。
在我的json包中,我有:
"precommit": "npm run lint && npm run test",
当发现任何测试或棉绒错误时,最初似乎运行良好,但我无法进行提交。
现在,我发现如果发出警告,则无论如何都会发生提交。
当出现警告时,我该如何配置沙哑的,或者也许是eslint的,以停止提交。
我知道我可以覆盖所有eslint配置,使其始终为错误[2],但我期望有更好的选择
节点版本:20.11.0 pnpm 版本:8.15.0 操作系统:Windows
我按照https://prettier.io/docs/en/install.html中的说明进行操作
pnpm exec husky install命令哈士奇有问题吗?如何设置更漂亮的 git hook?
感谢帮助
git commit -m "feat: 测试 commitlint 库" @commitlint/cli@16.2.3 - 检查你的提交消息
[input] 如果省略 --edit、--env、--from 和 --to 则从 stdin 读取
Options:
-c, --color toggle colored output [boolean] [default: true]
-g, --config path to the config file [string]
--print-config print resolved config [boolean] [default: false]
-d, --cwd directory to execute in
[string] [default: (Working Directory)]
-e, --edit read last commit message from the specified file or
fallbacks to ./.git/COMMIT_EDITMSG [string]
-E, --env check message in the file at path given by environment
variable …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个自动版本控制系统,如果我git commit使用消息PATCH: {message},应用程序的补丁版本将自动更新(同样对于前缀MINOR和MAJOR)。我使用Husky作为我的pre-commit钩子pre-push,所以我尝试使用.husky/commit-msg如下所示的钩子来实现它:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run auto-version $1 && git add --all
Run Code Online (Sandbox Code Playgroud)
这可以按照需要进行,我的auto-version.js脚本会自动读取提交消息并进行./version.json相应更新。唯一的问题是提交是用旧version.json文件创建的,我不确定为什么。我可以说它git add是有效的,因为提交后我version.json在该部分中留下了更新的文件Staged Changes。我的.husky/pre-commit钩子看起来像这样,并在提交之前暂存更新的文件:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run lint && npm run format && git add --all
Run Code Online (Sandbox Code Playgroud)
commit-msg我认为这可能与钩子被触发和接受新暂存文件之间的时间有关git,但 Husky 没有提供有关钩子如何commit-msg运行的优秀文档。我也尝试使用钩子进行设置pre-commit,但新的提交消息在这个阶段不会被保存.git/COMMIT_EDITMSG(仅存在旧的提交消息)。
对于一些额外的上下文,我们目前只是立即启动 …
我必须尝试用 lint-staged 设置一只哈士奇。最初,我试图设置如下,但这不起作用。
"lint-staged": {
"*.js": [
"prettier --write",
"eslint src/ --fix",
"npm run test",
"git add"
]
}
Run Code Online (Sandbox Code Playgroud)
然后我经过一些搜索我将我的设置更改为以下但那再次报告了差异错误
"lint-staged": {
"*.js": [
"prettier --write",
"eslint src/ --fix",
"jest --bail --findRelatedTests",
"git add"
]
}
Run Code Online (Sandbox Code Playgroud)
错误描述
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". …Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试将 husky 集成到我的流程中,但我找不到让它与 Jest watchman 一起工作的方法。
我的设置:
在根级别
.husky/pre-push
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm run test
Run Code Online (Sandbox Code Playgroud)
在package.json
"scripts": {
"test": "cd packages/frontend && node scripts/test.js --watchAll",
"prepare": "husky install"
},
Run Code Online (Sandbox Code Playgroud)
现在我运行git push,哈士奇确实可以运行npm run test,但我无法退出 cli。

我一直在搜索谷歌,大多数人似乎都在package.json而不是husky目录中配置哈士奇,但官方文档是这样做的。
知道如何解决这个问题吗?
husky ×6
eslint ×2
git-husky ×2
javascript ×2
npm ×2
commitlint ×1
git ×1
githooks ×1
jestjs ×1
lint-staged ×1
reactjs ×1