diff --git a/.github/README.md b/.github/README.md index 2f2e189a6..8d8ef6026 100644 --- a/.github/README.md +++ b/.github/README.md @@ -1,6 +1,6 @@ # husky -[![Open Collective](https://opencollective.com/husky/all/badge.svg?label=financial+contributors)](https://opencollective.com/husky) [![](https://img.shields.io/npm/dm/husky.svg?style=flat)](https://www.npmjs.org/package/husky) [![Node.js CI](https://github.com/typicode/husky/workflows/Node.js%20CI/badge.svg)](https://github.com/typicode/husky/actions) +[![](https://img.shields.io/npm/dm/husky.svg?style=flat)](https://www.npmjs.org/package/husky) [![Node.js CI](https://github.com/typicode/husky/workflows/Node.js%20CI/badge.svg)](https://github.com/typicode/husky/actions) > Modern native Git hooks made easy @@ -12,43 +12,23 @@ https://typicode.github.io/husky **Important** Upgrading from v4 to v9 requires migrating previous config, please see the docs. -## Articles +## Sponsors -- [Why husky has dropped conventional JS config](https://blog.typicode.com/posts/husky-git-hooks-javascript-config/) -- [Why husky doesn't autoinstall anymore](https://blog.typicode.com/posts/husky-git-hooks-autoinstall/) +Support this project by becoming a sponsor [here](https://github.com/sponsors/typicode) 💖 -## License - -MIT - -# Sponsors +### GitHub

- - + +

- -## Companies - -Does your company use husky? Ask your manager or marketing team if your company would be interested in supporting this project. +### Open Collective - - - - - - -## Individuals - -Find husky helpful? Become a backer and show your appreciation with a monthly donation on [Open Collective](https://opencollective.com/husky). You can also tip with a one-time donation. - - - -GitHub sponsors can be viewed on my [profile](https://github.com/typicode). All past and current Open Collective sponsors can be viewed on [here](https://opencollective.com/husky). + \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit index 043a7518d..778fb0b71 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,2 +1 @@ -#!/usr/bin/env sh ./test.sh \ No newline at end of file diff --git a/docs/sponsors.svg b/docs/sponsors.svg deleted file mode 100644 index 117b94be1..000000000 --- a/docs/sponsors.svg +++ /dev/null @@ -1,89 +0,0 @@ - - - -Gold Sponsors - risedotcom - -Silver Sponsors - Cased - -Sponsors - Kyle - - - - Ehren - - - - Erik Gross - - - - Dustin - - - - Seyed - - - - Extremely - -Backers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/docs/zh/getting-started.md b/docs/zh/getting-started.md deleted file mode 100644 index 1cf3d90f0..000000000 --- a/docs/zh/getting-started.md +++ /dev/null @@ -1,165 +0,0 @@ -# 开始使用 - -## 自动 - -`husky-init` 是一个一次性命令,用于快速初始化一个带有 husky 的项目。 - -::: code-group - -```shell [npm] -npx husky-init && npm install -``` - -```shell [pnpm] -pnpm dlx husky-init && pnpm install -``` - -```shell [yarn] -yarn dlx husky-init --yarn2 && yarn -``` - -::: - -它将会: - -1. 添加 `prepare` 脚本到 `package.json` -2. 创建一个你能够编辑的 `pre-commit` hook 示例(默认情况下,`npm test` 将在你提交时运行) -3. 配置 Git hooks 路径 - -要添加另一个 hook,请使用 `husky add`。例如: - -```shell -npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"' -``` - -::: info 注意 -对于 Windows 用户,如果在运行 `npx husky add ...` 时看到帮助信息,请尝试使用 `node node_modules/husky/lib/bin add ...`。这不是 husky 代码的问题。 -::: - -## 手动 - -### 安装 - -1. 安装 `husky` - -```shell -npm install husky --save-dev -``` - -2. 启用 Git hooks - -```shell -npx husky install -``` - -3. 要在安装后自动启用 Git hooks,请编辑 `package.json` - -```shell -npm pkg set scripts.prepare="husky install" -``` - -你应该这样做: - -::: code-group - -```json [package.json] -{ - "scripts": { - "prepare": "husky install" // [!code hl] - } -} -``` - -::: - -::: info 注意 -Yarn 2+ 不支持编写生命周期脚本,所以 husky 需要以不同的方式安装(但这不适用于 Yarn1)。参见 [Yarn 2+ 安装](#yarn-2)。 -::: - -## 创建一个 hook - -要向 hook 添加命令或创建新的 hook,可以使用 `husky add [cmd]`(再此之前不要忘记执行 `husky install`)。 - -```shell -npx husky add .husky/pre-commit "npm test" -git add .husky/pre-commit -``` - -尝试提交 - -```shell -git commit -m "Keep calm and commit" -``` - -如果 `npm test` 命令执行失败,你的提交会被自动终止。 - -::: warning 警告 -**想要使用 Yarn 运行命令?Git Bash 在 Windows 上有个问题,请参阅 [在 Windows 上使用 Yarn](./troubleshooting.md#在-windows-上使用-yarn)。** -::: - -_对于 Windows 用户,如果在运行 `npx husky add ...` 时看到帮助信息,请尝试使用 `node node_modules/husky/lib/bin add ...`。这不是 husky 代码的问题,在 npm 8 的最新版本已经修复了。_ - -### 卸载 - -```shell -npm uninstall husky && git config --unset core.hooksPath -``` - -## Yarn 2 - -### 安装 - -1. 安装 `husky` - -```shell -yarn add husky --dev -yarn add pinst --dev # ONLY if your package is not private -``` - -2. 启用 Git hooks - -```shell -yarn husky install -``` - -3. 要在安装后自动启用 Git hooks,请编辑 `package.json` - -::: code-group - -```js [package.json] -{ - "private": true, // ← 你的 package 是私有的,你只需要 postinstall - "scripts": { - "postinstall": "husky install" - } -} -``` - -::: - -::: tip 提示 -如果您的软件包不是私有的,并且您正在像 [npmjs.com](https://npmjs.com) 这样的仓库上发布它,那么您需要使用 [pinst](https://github.com/typicode/pinst)\*\* 禁用 postinstall 脚本。否则,当有人安装你的软件包并导致错误时,将运行 postinstall。 -::: - -::: code-group - -```js [package.json] -{ - "private": false, // ← 你的 package 是共有的 - "scripts": { - "postinstall": "husky install", - "prepack": "pinst --disable", - "postpack": "pinst --enable" - } -} -``` - -::: - -### 卸载 - -从 `package.json` 中移除 `"postinstall": "husky install"`,并执行: - -```shell -yarn remove husky && git config --unset core.hooksPath -``` diff --git a/docs/zh/guide.md b/docs/zh/guide.md deleted file mode 100644 index eee2413c9..000000000 --- a/docs/zh/guide.md +++ /dev/null @@ -1,175 +0,0 @@ -# 指南 - -## Monorepo - -建议在根目录下的 `package.json` 中添加 husky。你可以使用诸如 [lerna](https://github.com/lerna/lerna) 和 filter 之类的工具来仅在已更改的包中运行脚本。 - -## 自定义目录 - -如果你想在另一个目录安装 husky,比如 `.config` 目录,你可以在 `install` 命令后面添加参数。示例如下: - -::: code-group - -```js [package.json] -{ - "scripts": { - "prepare": "husky install .config/husky" - } -} -``` - -::: - -另一种情况,如果你的 `package.json` 文件和 `.git` 目录不在同一级目录。例如,`project/.git` 和 `project/front/package.json`。 - -设计上,`husky install` 必须运行在于 `.git` 相同的目录,但是你可以通过在 `prepare` 脚本中传入一个子目录来改变目录: - -::: code-group - -```js [package.json] -{ - "scripts": { - "prepare": "cd .. && husky install front/.husky" - } -} -``` - -::: - -在你的 hooks 中,你也需要去更改目录: - -::: code-group - -```shell [.husky/pre-commit] -# ... -cd front -npm test -``` - -::: - -## 绕过 hooks - -你能使用 Git命令的 `-n/--no-verify` 选项来绕过 `pre-commit` 和 `commit-msg` hooks: - -```shell -git commit -m "yolo!" --no-verify -``` - -对于没有使用 `--no-verify` 选项的 Git 命令,你可以使用 `HUSKY` 环境变量: - -```shell -HUSKY=0 git push # yolo! -``` - -## 在 CI/Docker/Prod 中禁用 husky - -在 CI/Docker/Prod上下文中禁用 husky 没有对错之分,这在很大程度上 **取决于你的使用情况**。 - -### 使用 npm - -如果你想阻止 husky 完全安装 - -```shell -npm ci --omit=dev --ignore-scripts -``` - -或者,你也能明确地禁用 `prepare` 脚本 - -```shell -npm pkg delete scripts.prepare -npm ci --omit=dev -``` - -### 使用自定义脚本 - -您可以创建一个自定义 JS 脚本,有条件地要求使用 husky 和安装 hooks。 - -::: code-group - -```json [package.json] -"prepare": "node ./prepare.js" -``` - -```js [prepare.js] -const isCi = process.env.CI !== undefined -if (!isCi) { - require('husky').install() -} -``` - -::: - -或者在未安装 husky 的情况下,让 `prepare` 脚本无声地失败: -Or make `prepare` script fail silently if husky is not installed: - -```json [package.json] -"prepare": "node -e \"try { require('husky').install() } catch (e) {if (e.code !== 'MODULE_NOT_FOUND') throw e}\"" -``` - -### 使用环境变量 - -你可以在你的 CI 配置文件中,将 `HUSKY` 环境变量设置为 `0`,来禁用 hooks 安装。 - -另外,大多数持续集成服务器都会设置一个 `CI` 环境变量。你可以在钩子中使用它来检测是否在 CI 中运行。 - -::: code-group - -```shell [.husky/pre-commit] -# ... -[ -n "$CI" ] && exit 0 -``` - -::: - -### 使用 is-ci - -您还可以在 `prepare` 脚本中使用 [is-ci](https://github.com/watson/is-ci),有条件地安装 husky - -```shell -npm install is-ci --save-dev -``` - -::: code-group - -```js [package.json] -{ - "scripts": { - "prepare": "is-ci || husky install" - } -} -``` - -::: - -## 测试 hooks - -如果要测试 hook,可以在脚本末尾添加 `exit 1` 来终止 git 命令。 - -::: code-group - -```shell [.husky/pre-commit] -# ... -exit 1 # Commit will be aborted -``` - -::: - -## Git-flow - -如果使用 [git-flow](https://github.com/petervanderdoes/gitflow-avh/),需要确保 git-flow hooks 目录设置为使用 husky(默认为 `.husky`)。 - -```shell -git config gitflow.path.hooks .husky -``` - -**注意:** - -- 如果在安装 husky 之后配置 git-flow,那么 git-flow 设置过程将正确地建议使用 `.husky` 目录。 -- 如果您已经为 husky 设置了一个 [自定义目录](#自定义目录),那么您需要指定这个目录(比如 `git config gitflow. path.hooks. config/husky`) - -要将 Git-flow hook 目录 **恢复** 到默认目录,需要重置配置,使其指向默认的 Git hook 目录。 - -```shell -git config gitflow.path.hooks .git/hooks -``` diff --git a/docs/zh/index.md b/docs/zh/index.md deleted file mode 100644 index 20190a0d6..000000000 --- a/docs/zh/index.md +++ /dev/null @@ -1,81 +0,0 @@ -[![Financial Contributors on Open Collective](https://opencollective.com/husky/all/badge.svg?label=financial+contributors)](https://opencollective.com/husky) - -[![](https://img.shields.io/npm/dm/husky.svg?style=flat)](https://www.npmjs.org/package/husky) - -[![Node.js CI](https://github.com/typicode/husky/workflows/Node.js%20CI/badge.svg)](https://github.com/typicode/husky/actions) - -> 使现代的原生 Git Hooks 变得简单 - -Husky 能使你的提交变得更好 🐶 _汪!_ - -在提交或推送时,你可以用它来 **检查提交信息**、**运行测试**、**检查代码** 等。Husky 支持 [所有 Git hooks](https://git-scm.com/docs/githooks)。 - -点击 [这里](./getting-started) 开始使用。 - -## 特点 - -- 轻量级,零依赖 (`6 kB`) -- 由现代化的新 Git 特性驱动 (`core.hooksPath`) -- 遵循 [npm](https://docs.npmjs.com/cli/v8/using-npm/scripts#best-practices) 和 [Yarn](https://yarnpkg.com/advanced/lifecycle-scripts#a-note-about-postinstall) 的 `自动安装` 最佳实践 -- 对用户友好的信息 -- Opt-in/opt-out -- **支持** - - macOS, Linux 和 Windows - - Git GUIs - - 自定义 hooks 目录 - - 嵌套项目 - - Monorepos - -## 谁在使用 - -Husky 被运用在这些伟大的项目中: - -- [webpack/webpack](https://github.com/webpack/webpack) -- [angular/angular](https://github.com/angular/angular) -- [angular/angular-cli](https://github.com/angular/angular-cli) -- [angular/components](https://github.com/angular/components) -- [vercel/hyper](https://github.com/vercel/hyper) -- [blitz-js/blitz](https://github.com/blitz-js/blitz) -- [facebook/docusaurus](https://github.com/facebook/docusaurus) -- [typescript-eslint/typescript-eslint](https://github.com/typescript-eslint/typescript-eslint) -- [11ty/eleventy](https://github.com/11ty/eleventy) -- [stylelint/stylelint](https://github.com/stylelint/stylelint) -- [rollup/rollup](https://github.com/rollup/rollup) -- [tauri-apps/tauri](https://github.com/tauri-apps/tauri) -- [NativeScript/NativeScript](https://github.com/NativeScript/NativeScript) -- [formatjs/formatjs](https://github.com/formatjs/formatjs) -- [react-bootstrap/react-bootstrap](https://github.com/react-bootstrap/react-bootstrap) -- [react-dnd/react-dnd](https://github.com/react-dnd/react-dnd) -- [react-grid-layout/react-grid-layout](https://github.com/react-grid-layout/react-grid-layout) -- [snabbdom/snabbdom](https://github.com/snabbdom/snabbdom) -- [logaretm/vee-validate](https://github.com/logaretm/vee-validate) -- [zenorocha/clipboard.js](https://github.com/zenorocha/clipboard.js) -- [NodeBB/NodeBB](https://github.com/NodeBB/NodeBB) -- [ant-design/ant-design](https://github.com/ant-design/ant-design) -- 还有 [**超过 110 万个项目**](https://github.com/typicode/husky/network/dependents?package_id=UGFja2FnZS0xODQzNTgwNg%3D%3D) - -## 相关文章 - -- [Why husky has dropped conventional JS config](https://blog.typicode.com/husky-git-hooks-javascript-config/) -- [Why husky doesn't autoinstall anymore](https://blog.typicode.com/husky-git-hooks-autoinstall/) - -## 赞助者 - -你的公司在使用 Husky 吗?询问你的经理或者营销团队是否有兴趣支持这个项目。 - - - - - - - - - - - - -觉得哈士奇有用吗?成为一个支持者,并通过每月在 [Open Collective](https://opencollective.com/husky) 上的捐款来表达你的感激之情。你也可以通过一次性捐赠来给小费。 - - - -GitHub 的赞助商可以在我的 [个人资料](https://github.com/typicode) 中查看。所有过去和现在的 Open Collective 赞助商都可以在 [这里](https://opencollective.com/husky) 查看。 diff --git a/docs/zh/migrating-from-v4.md b/docs/zh/migrating-from-v4.md deleted file mode 100644 index 6422bb988..000000000 --- a/docs/zh/migrating-from-v4.md +++ /dev/null @@ -1,76 +0,0 @@ -# 从 v4 迁移 - -## CLI - -请参见 [husky-4-to-8](https://github.com/typicode/husky-4-to-8) CLI 以快速从 v4迁移到 v8。 - -## 手动迁移 - -如果使用 `npm` 或 `yarn` 调用 `package.json` 脚本,只需将配置文件中的命令 **复制** 到相应的钩子即可: - -::: code-group - -```js [.huskyrc.json (v4)] -{ - "hooks": { - "pre-commit": "npm test && npm run foo" - } -} -``` - -```shell [.husky/commit-msg (v8)] -# ... -npm test -npm run foo -``` - -::: - -如果您正在调用本地安装的二进制文件,**现在您需要通过您的包管理器运行它们**: - -::: code-group - -```js [.huskyrc.json (v4)] -{ - "hooks": { - "pre-commit": "jest" - } -} -``` - -```shell [.husky/commit-msg (v8)] -# ... -npx --no jest -# or -yarn jest -``` - -::: - -`HUSKY_GIT_PARAMS` 环境变量现在替换为原生参数 `$1`、`$2`。 - -::: code-group - -```js [.huskyrc.json (v4)] -{ - "hooks": { - "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" - } -} -``` - -```shell [.husky/commit-msg (v8)] -# ... -npx --no -- commitlint --edit $1 -# or -yarn commitlint --edit $1 -``` - -::: - -其他环境变量改动: - -- `HUSKY_SKIP_HOOKS` 替换为 `HUSKY`。 -- `HUSKY_SKIP_INSTALL` 替换为 `HUSKY`。 -- `HUSKY_GIT_PARAMS` 已被移除。取而代之的是,Git 参数应该直接在脚本中使用(例如 `$1`)。 -- 本地安装工具的 `PATH` 不再自动设置。你需要使用包管理器来运行它们。 diff --git a/docs/zh/troubleshooting.md b/docs/zh/troubleshooting.md deleted file mode 100644 index b77d28a2d..000000000 --- a/docs/zh/troubleshooting.md +++ /dev/null @@ -1,78 +0,0 @@ -# 疑难解答 - -## 找不到命令 - -如果您在应用程序中运行 Git,而命令可以在终端中找到,这意味着应用程序中的 `PATH` 与终端中的不同。 - -你可以在终端中使用 `echo $PATH` 命令,然后配置你的应用为相同的值。 - -如果你使用 `brew` 安装了你的命令,请参阅 [Homebrew FAQ](https://docs.brew.sh/FAQ),让你的应用程序可以使用你的命令。 - -最后,如果你正在使用管理版本的脚本,例如 `nvm`、`n`、`rbenv`、`pyenv`......你可以在运行 hooks 之前使用 `~/.huskyrc` 加载必要的脚本。 -Finally, if you're using a script for managing versions like `nvm`, `n`, `rbenv`, `pyenv`, ... you can use `~/.huskyrc` to load the necessary before running hooks. - -例如,对于 nvm,可以使用: - -::: code-group - -```shell [~/.huskyrc] -# This loads nvm.sh, sets the correct PATH before running hook, and ensures the project version of Node -export NVM_DIR="$HOME/.nvm" - -[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - -# If you have an .nvmrc file, we use the relevant node version -if [[ -f ".nvmrc" ]]; then - nvm use -fi -``` - -::: - -::: info 注意 -对于某些应用程序(如 VS Code),只需重启应用程序即可解决此问题。在采取上述任何步骤之前,请先尝试一下!\*\* -::: - -## Hooks 未运行 - -1. 确保文件名中没有错别字。例如 `precommit` 或 `pre-commit.sh` 都是无效名称。有关有效名称,请参阅 Git hooks [文档](https://git-scm.com/docs/githooks)。 -2. 检查 `git config core.hooksPath` 是否返回 `.husky`(或你的自定义 hook 目录)。 -3. 确认钩子文件是可执行的。使用 `husky add` 命令时会自动设置为可执行,但可以运行 `chmod +x .husky/` 来修复。 -4. 检查 Git 版本是否大于 `2.9`。 - -## 卸载后 .git/hooks/ 无法运行 - -如果卸载 `husky` 后,`.git/hooks/` 中的 hooks 不起作用。运行 `git config --unset core.hooksPath`。 - -注意:如果 `npm <7` 在卸载 husky 时会自动执行此操作,但现在已不支持 `preuninstall`。 - -## 在 Windows 上使用 Yarn - -在 Windows 上使用 Yarn 和 Git Bash(`stdin 不是 tty`)时,Git hooks 可能会失败。如果用户使用的是 Windows 系统,强烈建议添加以下解决方案。 - -1. 创建 `.husky/common.sh`: - -```shell -command_exists () { - command -v "$1" >/dev/null 2>&1 -} - -# Workaround for Windows 10, Git Bash and Yarn -if command_exists winpty && test -t 1; then - exec < /dev/tty -fi -``` - -2. 在使用 Yarn 运行命令的地方输入源代码: - -```shell -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" -. "$(dirname -- "$0")/common.sh" - -yarn ... -``` - -## 它能在 Windows 上运行吗? - -可以。在 Windows 上安装 Git 时,会附带运行 shell 脚本所需的软件。