| name | setup-pre-commit |
| description (EN) | Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing. |
| 說明 (繁中) | 在目前的 repo 中以 lint-staged(Prettier)、型別檢查與測試設定 Husky pre-commit hooks。當使用者想要新增 pre-commit hooks、設定 Husky、設定 lint-staged,或新增提交時的格式化/型別檢查/測試時使用。 |
Check for package-lock.json (npm), pnpm-lock.yaml (pnpm), yarn.lock (yarn), bun.lockb (bun). Use whichever is present. Default to npm if unclear.
檢查 package-lock.json(npm)、pnpm-lock.yaml(pnpm)、yarn.lock(yarn)、bun.lockb(bun)。使用存在的那個。如果不清楚,預設為 npm。
Install as devDependencies:
安裝為 devDependencies:
husky lint-staged prettier
husky lint-staged prettier
npx husky init
npx husky init
This creates .husky/ dir and adds prepare: "husky" to package.json.
這會建立 .husky/ 目錄,並將 prepare: "husky" 加到 package.json。
.husky/pre-commit.husky/pre-commitWrite this file (no shebang needed for Husky v9+):
寫入這個檔案(Husky v9+ 不需要 shebang):
npx lint-staged
npm run typecheck
npm run test
npx lint-staged
npm run typecheck
npm run test
Adapt: Replace npm with detected package manager. If repo has no typecheck or test script in package.json, omit those lines and tell the user.
調整:將 npm 取代為偵測到的套件管理員。如果 repo 在 package.json 中沒有 typecheck 或 test 腳本,省略那些行並告知使用者。
.lintstagedrc.lintstagedrc{
"*": "prettier --ignore-unknown --write"
}
{
"*": "prettier --ignore-unknown --write"
}
.prettierrc (if missing).prettierrc(如果缺少)Only create if no Prettier config exists. Use these defaults:
只有當沒有 Prettier 設定存在時才建立。使用這些預設值:
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}
{
"useTabs": false,
"tabWidth": 2,
"printWidth": 80,
"singleQuote": false,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always"
}
.husky/pre-commit exists and is executable.lintstagedrc existsprepare script in package.json is "husky"prettier config existsnpx lint-staged to verify it works.husky/pre-commit 存在且可執行.lintstagedrc 存在prepare 腳本是 "husky"prettier 設定存在npx lint-staged 來驗證它可以運作Stage all changed/created files and commit with message: Add pre-commit hooks (husky + lint-staged + prettier)
暫存所有變更/建立的檔案,並以訊息提交:Add pre-commit hooks (husky + lint-staged + prettier)
This will run through the new pre-commit hooks: a good smoke test that everything works.
這會跑過新的 pre-commit hooks — 一個很好的煙霧測試,確認一切都能運作。
prettier --ignore-unknown skips files Prettier can't parse (images, etc.)prettier --ignore-unknown 會跳過 Prettier 無法解析的檔案(圖片等)