我正在尝试为我公司的项目设置提交时的自动 linting,我见过的最好方法是 NPMhusky包。我正在按照、 、Husky的设置说明进行操作,但我一直遇到同样的问题,但我不知道如何解决。eslintprettierlint-staged
我的问题是我的项目是这样设置的:
\n- Parent Directory\n - .git\n - Working Project Directory\n - node_modules\n - package.json\nRun Code Online (Sandbox Code Playgroud)\n当我尝试安装 husky 时,出现错误,提示我缺少该.git文件,但它就在那里,只是文件结构中的上一级!
我在其他帖子中看到了一些有关使用以下技术来修复它的内容:
\n\n\n\n
npx mrm lint-staged这将失败,\xe2\x80\x99s 预期
\n\n然后修复 npm 准备脚本\n
\n"prepare": "husky install" -> "prepare": "cd .. && husky install some-folder/.husky"
\n\n然后运行
\nnpm i
然而它对我不起作用。
\n我也尝试在中安装 husky,parent directory但它不太有效,并且它使我们项目的安装过程变得更加困难
有没有人有什么建议?
\n编辑:这是我收到的错误消息:
\n% …Run Code Online (Sandbox Code Playgroud) 直到最近——我几天前才注意到这一点——我的 git pre-commit 钩子正在工作。我正在编写一个 React 应用程序,并在提交之前使用 Husky、TSLint 和 Prettier 来清理和整理我的代码。现在,当我更改和提交文件时,预提交钩子不会运行。
我的项目结构如下所示:
- project
- .git/
- react/ <- the frontend
- node_modules/
- src/
- package.json
- (other files)
- nodejs/ <- the server
- node_modules/
- src/
- package.json
- (other files)
- package.json
- (other files)
Run Code Online (Sandbox Code Playgroud)
如果我手动执行钩子,它似乎运行良好:
[/project/react] # git status
On branch fixHusky
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: MyFile.ts
[/project/react] # ../.git/hooks/pre-commit
husky > pre-commit (node v12.6.0)
? Stashing changes... …Run Code Online (Sandbox Code Playgroud) 我正在设置一个节点项目,husky我想手动运行一个 git-hook 脚本,而不必在 git 中触发它。
例子:
我的 package.json 有以下内容:
{
...
"scripts": {
"precommit": // something goes here
}
...
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
...
}
Run Code Online (Sandbox Code Playgroud)
如果我运行npm run precommit,lint-staged钩子步骤将运行,而没有实际发生在 git 中的提交。
这可能吗?
我已经像以前一样配置了 prettier 和预提交钩子,但在我的新项目中 husky 在任何提交之前都不会运行!
以下是项目package.json文件的一部分:
{
...,
"devDependencies": {
"husky": "^4.3.6",
"prettier": "2.2.1",
"pretty-quick": "^3.1.0"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个有角度的项目,我想在其中强制执行常规提交。我未能成功地使用正确的钩子来防止错误的钩子。
我从本教程开始,其中提到将以下内容添加到 package.json:
{
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用(它仍然会导致错误的提交),所以我做了一些研究,发现这篇文章说上面是针对 husky 4 的,对于 husky 5 我应该运行这个命令:
npx husky add .husky/commit-msg 'npx commitlint --edit $1'
Run Code Online (Sandbox Code Playgroud)
据我所知,commitlint 官方文档说以同样的方式进行。但是,当我运行它时,我收到这个奇怪的提示,但它没有执行任何操作:
PS C:\...\MyProj> npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
Usage
husky install [dir] (default: .husky)
husky uninstall
husky set|add <file> [cmd]
Run Code Online (Sandbox Code Playgroud)
这只是令人困惑,因为我所写的实际上是在提示的第三行之后。
有没有人经历过这个并可以帮助我理解我需要做什么?
package.json 中的相关部分:
"scripts": {
"postinstall": "husky install"
},
"private": true,
"devDependencies": {
"@commitlint/cli": "^12.1.1",
"@commitlint/config-conventional": "^12.1.1",
"husky": "^6.0.0"
}
}
Run Code Online (Sandbox Code Playgroud) 这是我所做的:
git status 显示我的所有更改。git add -Agit commit -m "Foo". 预提交的 git 钩子用husky和触发lint-staged。git commit -m "Foo"一次,立即取消了。git status是干净的,git log并且git reflog不显示新的提交。为什么我的更改被还原?我如何恢复它们?
例如,我在 .rb 和 .js 文件上匹配了它。
例如**/*.{js,rb}
例如,还想匹配Gemfile。Rakefile
我有一个问题husky。我希望 huskyeslint在 git commit 即将完成时运行并且更漂亮,以便它可以强制执行干净的编码检查。我已经设置eslint并prettier集成了两者。手动使用时它们工作正常。eslint然而,如果违反其中一条规则,哈士奇允许提交。
我还将pre-commit.sample本地项目.git/hooks目录中的文件重命名为pre-commit.
这是我的package.json文件:
"scripts": {
"prettier-format": "prettier --config .prettierrc 'src/**/*.ts' 'test/**/*.ts' --write",
"lint": "eslint . --ext .ts"
},
"husky": {
"hooks": {
"pre-commit": "npm run prettier-format && npm run lint"
}
},
"devDependencies": {
"husky": "^7.0.1"
}
Run Code Online (Sandbox Code Playgroud)
请问这里可能是什么问题?非常感谢。
我已经husky在我的 npm 项目中安装了prepare如下脚本
{
"name": "functions",
"scripts": {
"build": "tsc",
"start": "npm run serve",
"deploy": "firebase deploy --only functions",
"prepare": "husky install functions/.husky"
}
"dependencies": {
"firebase-admin": "^11.4.1",
"firebase-functions": "^4.1.1",
},
"devDependencies": {
"husky": "^8.0.2",
"typescript": "^4.9.4"
}
}
Run Code Online (Sandbox Code Playgroud)
husky声明为devDependencies这个 npm 模块仅在本地开发时需要,在运行时应用程序中不需要。
所以当我运行时npm run deploy,我收到以下错误
i functions: updating Node.js 16 function funName(us-central1)...
Build failed:
> prepare
> husky install functions/.husky
sh: 1: husky: not found
npm ERR! code 127
npm ERR! …Run Code Online (Sandbox Code Playgroud) husky ×10
git ×4
git-husky ×2
githooks ×2
javascript ×2
lint-staged ×2
node.js ×2
npm ×2
commit ×1
docker ×1
eslint ×1
firebase ×1
firebase-cli ×1
lint ×1
package.json ×1
prettier ×1