setup-pre-commit
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese设置 Pre-Commit 钩子
Set Up Pre-Commit Hooks
将要设置的内容
What Will Be Set Up
- Husky pre-commit 钩子
- lint-staged 对所有暂存文件运行 Prettier
- Prettier 配置(如果缺失)
- typecheck 和 test 脚本在 pre-commit 钩子中运行
- Husky pre-commit hook
- lint-staged runs Prettier on all staged files
- Prettier configuration (if missing)
- typecheck and test scripts run in the pre-commit hook
步骤
Steps
1. 检测包管理器
1. Detect Package Manager
检查 (npm)、(pnpm)、(yarn)、(bun)。使用已存在的那个。如果不确定,默认使用 npm。
package-lock.jsonpnpm-lock.yamlyarn.lockbun.lockbCheck for (npm), (pnpm), (yarn), (bun). Use the existing one. Default to npm if unsure.
package-lock.jsonpnpm-lock.yamlyarn.lockbun.lockb2. 安装依赖
2. Install Dependencies
安装为 devDependencies:
husky lint-staged prettierInstall as devDependencies:
husky lint-staged prettier3. 初始化 Husky
3. Initialize Husky
bash
npx husky init这会创建 目录并在 package.json 中添加 。
.husky/prepare: "husky"bash
npx husky initThis creates the directory and adds to package.json.
.husky/prepare: "husky"4. 创建 .husky/pre-commit
.husky/pre-commit4. Create .husky/pre-commit
.husky/pre-commit写入以下内容(Husky v9+ 不需要 shebang):
npx lint-staged
npm run typecheck
npm run test适配:将 替换为检测到的包管理器。如果仓库的 package.json 中没有 或 脚本,则省略对应行并告知用户。
npmtypechecktestWrite the following content (shebang is not required for Husky v9+):
npx lint-staged
npm run typecheck
npm run testAdaptation: Replace with the detected package manager. If the repository's package.json does not have or scripts, omit the corresponding lines and inform the user.
npmtypechecktest5. 创建 .lintstagedrc
.lintstagedrc5. Create .lintstagedrc
.lintstagedrcjson
{
"*": "prettier --ignore-unknown --write"
}json
{
"*": "prettier --ignore-unknown --write"
}6. 创建 .prettierrc
(如果缺失)
.prettierrc6. Create .prettierrc
(if missing)
.prettierrc仅当不存在 Prettier 配置时才创建。使用以下默认值:
json
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}Only create it if no Prettier configuration exists. Use the following default values:
json
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}7. 验证
7. Verification
- 存在且可执行
.husky/pre-commit - 存在
.lintstagedrc - package.json 中的 脚本为
prepare"husky" - 配置存在
prettier - 运行 验证其是否正常工作
npx lint-staged
- exists and is executable
.husky/pre-commit - exists
.lintstagedrc - The script in package.json is
prepare"husky" - configuration exists
prettier - Run to verify it works properly
npx lint-staged
8. 提交
8. Commit
暂存所有更改/创建的文件,并使用消息提交:
Add pre-commit hooks (husky + lint-staged + prettier)这将触发新的 pre-commit 钩子执行——是一个很好的冒烟测试,验证一切正常。
Stage all changed/created files and commit with the message:
Add pre-commit hooks (husky + lint-staged + prettier)This will trigger the new pre-commit hook execution — it's a good smoke test to verify everything works correctly.
注意事项
Notes
- Husky v9+ 不需要在钩子文件中加 shebang
- 会跳过 Prettier 无法解析的文件(如图片等)
prettier --ignore-unknown - pre-commit 钩子先运行 lint-staged(快速,仅处理暂存文件),然后运行完整的 typecheck 和 test
- Shebang is not required in hook files for Husky v9+
- skips files that Prettier cannot parse (such as images, etc.)
prettier --ignore-unknown - The pre-commit hook runs lint-staged first (fast, only processes staged files), then runs full typecheck and test