agentleFS
shumingyang-opencodeshumingyang-opencode/mattpocock-skills-zh-tw/skills/misc/setup-pre-commit/SKILL.md

setup-pre-commit

Skill3 starsChanged 54 days agoMITView on GitHub

在目前的 repo 中以 lint-staged(Prettier)、型別檢查與測試設定 Husky pre-commit hooks。當使用者想要新增 pre-commit hooks、設定 Husky、設定 lint-staged,或新增提交時的格式化/型別檢查/測試時使用。

150 other projects use a version of this, 130 of them edited.

Adopted by

Most recently taken up by

Used with

Projects with it also use edit-article, request-refactor-plan and 2 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • simo97Skill

    edit-article

    The user will invoke this skill to help them edit an article.

    Also in 41 projects that use this

  • simo97Skill

    request-refactor-plan

    Use this skill when user wants to create a refactor plan.

    Also in 40 projects that use this

  • FabienDehopreSkill

    decision-mapping

    Turn a loose idea into a sequenced map of investigation tickets, then drive them to resolution one at a time.

    Also in 6 projects that use this

  • +1 more from simo97

The file

Files
SKILL.md

設定 Pre-Commit Hooks

這會設定什麼

  • Husky pre-commit hook
  • lint-staged 在所有已暫存的檔案上執行 Prettier
  • Prettier 設定(如果缺少)
  • pre-commit hook 中的 typecheck 與 test 腳本

步驟

1. 偵測套件管理員

檢查 package-lock.json(npm)、pnpm-lock.yaml(pnpm)、yarn.lock(yarn)、bun.lockb(bun)。使用存在的那個。如果不清楚,預設為 npm。

2. 安裝相依套件

安裝為 devDependencies:

husky lint-staged prettier

3. 初始化 Husky

npx husky init

這會建立 .husky/ 目錄,並將 prepare: "husky" 加到 package.json。

4. 建立 .husky/pre-commit

寫入這個檔案(Husky v9+ 不需要 shebang):

npx lint-staged
npm run typecheck
npm run test

調整:將 npm 取代為偵測到的套件管理員。如果 repo 在 package.json 中沒有 typecheck 或 test 腳本,省略那些行並告知使用者。

5. 建立 .lintstagedrc

{
  "*": "prettier --ignore-unknown --write"
}

6. 建立 .prettierrc(如果缺少)

只有當沒有 Prettier 設定存在時才建立。使用這些預設值:

{
  "useTabs": false,
  "tabWidth": 2,
  "printWidth": 80,
  "singleQuote": false,
  "trailingComma": "es5",
  "semi": true,
  "arrowParens": "always"
}

7. 驗證

  • .husky/pre-commit 存在且可執行
  • .lintstagedrc 存在
  • package.json 中的 prepare 腳本是 "husky"
  • prettier 設定存在
  • 執行 npx lint-staged 來驗證它可以運作

8. 提交

暫存所有變更/建立的檔案,並以訊息提交:Add pre-commit hooks (husky + lint-staged + prettier)

這會跑過新的 pre-commit hooks — 一個很好的煙霧測試,確認一切都能運作。

備註

  • Husky v9+ 不需要 hook 檔案中的 shebangs
  • prettier --ignore-unknown 會跳過 Prettier 無法解析的檔案(圖片等)
  • pre-commit 先執行 lint-staged(快速、只針對已暫存),然後是完整的型別檢查與測試

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.