Claude Cursor Skill

add-pre-commit-checks

Use when asked to add pre-commit hooks, set up Husky or lint-staged, stop bad commits, or run Prettier and type checks on commit. Sets up a pre-commit hook that formats staged files, type-checks with the project config, and runs related tests, proven by blocked and fast commits.

LLM Mart · 0 points · 0 views 0 listing impressions 0 install-command copies
Download heyvhuang-copybook-skills-plugins_skills_skills_add-pre-commit-checks-c2e4cc2.zip · 3 KB

Install

skills CLI npx skills add https://github.com/Heyvhuang/copybook-skills/tree/main/plugins/skills/skills/add-pre-commit-checks
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install heyvhuang-copybook-skills@llmmart
Git git clone https://github.com/Heyvhuang/copybook-skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole heyvhuang/copybook-skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Add pre-commit checks

A pre-commit hook should stop broken code before it lands while keeping doc edits fast.

1. Inspect the repo first

Check the lockfile (pnpm-lock.yaml, yarn.lock, bun.lockb, or package-lock.json) to pick the package manager. Read package.json for prepare, typecheck, and test scripts, plus any existing .husky/ hooks, tsconfig files, or Prettier config.

Keep existing hooks such as .husky/commit-msg intact. If no Prettier config exists, ask the user before creating .prettierrc.

2. Install and wire Husky safely

Install husky and lint-staged as dev dependencies (plus prettier if missing) using the repo's package manager.

Do not run husky init when .husky/pre-commit or a prepare script already exists, because it can overwrite both. Instead, add husky to prepare (extending an existing script with && husky), run husky through the package manager to configure git hooks, and create or extend .husky/pre-commit while keeping it executable.

3. Format staged files only

Configure lint-staged in .lintstagedrc.json or package.json to format staged files and skip unknown types:

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

Do not put tsc as a string task in lint-staged. Because lint-staged appends staged file paths to string tasks, tsc ignores tsconfig.json and breaks path aliases and compiler options.

4. Gate project type-check and tests

In .husky/pre-commit, run lint-staged first. Then check staged file paths so doc-only commits such as README.md skip type-checking and tests:

set -e
pnpm exec lint-staged

staged=$(git diff --cached --name-only --diff-filter=ACMRD)
if printf '%s\n' "$staged" | grep -Eq '\.(ts|tsx|mts|cts|js|jsx)$'
then
  pnpm exec tsc --noEmit -p tsconfig.json
  pnpm test
fi

Adapt the package manager, tsconfig file, and test command to the repo. Run tsc --noEmit -p <project config> without passing filenames, or call the repo's project-wide typecheck script. If the test runner supports related-file mode (such as vitest related --run or jest --findRelatedTests), pass the staged code files to it, else run the fast test suite. Omit the test line if the repo has no test script and tell the user.

5. Verify in an isolated fixture

Test the real hook in a temporary git clone or worktree so you do not pollute the user's history:

  • Stage a TypeScript file with a real type error and run git commit: the hook must fail and block the commit.
  • Stage a clean TypeScript file using a tsconfig path alias (such as @/lib): the hook must pass and commit cleanly.
  • Confirm any pre-existing hook (such as .husky/commit-msg) is still present and unchanged.
  • Stage only a README.md edit and time git commit: it must skip type-check and tests and finish in under 5 seconds.

Clean up the temporary fixture and leave the repo ready without auto-committing.

Based on mattpocock/skills (MIT), rewritten by Copybook.

Files (copybook-skills)
  • CHANGES.md 346 B
    Loading…
  • example.md 1.2 KB
    Loading…
  • LICENSE 1 KB · in bundle
  • SKILL.md 3.3 KB
    ---
    name: add-pre-commit-checks
    description: "Use when asked to add pre-commit hooks, set up Husky or lint-staged, stop bad commits, or run Prettier and type checks on commit. Sets up a pre-commit hook that formats staged files, type-checks with the project config, and runs related tests, proven by blocked and fast commits. Not for picking a test set for a change (run-affected-tests)."
    license: MIT (see LICENSE)
    ---
    
    # Add pre-commit checks
    
    A pre-commit hook should stop broken code before it lands while keeping doc edits fast.
    
    ## 1. Inspect the repo first
    
    Check the lockfile (`pnpm-lock.yaml`, `yarn.lock`, `bun.lockb`, or `package-lock.json`) to pick the package manager. Read `package.json` for `prepare`, `typecheck`, and `test` scripts, plus any existing `.husky/` hooks, `tsconfig` files, or Prettier config.
    
    Keep existing hooks such as `.husky/commit-msg` intact. If no Prettier config exists, ask the user before creating `.prettierrc`.
    
    ## 2. Install and wire Husky safely
    
    Install `husky` and `lint-staged` as dev dependencies (plus `prettier` if missing) using the repo's package manager.
    
    Do not run `husky init` when `.husky/pre-commit` or a `prepare` script already exists, because it can overwrite both. Instead, add `husky` to `prepare` (extending an existing script with `&& husky`), run `husky` through the package manager to configure git hooks, and create or extend `.husky/pre-commit` while keeping it executable.
    
    ## 3. Format staged files only
    
    Configure `lint-staged` in `.lintstagedrc.json` or `package.json` to format staged files and skip unknown types:
    
    ```json
    {
      "*": "prettier --ignore-unknown --write"
    }
    ```
    
    Do not put `tsc` as a string task in `lint-staged`. Because `lint-staged` appends staged file paths to string tasks, `tsc` ignores `tsconfig.json` and breaks path aliases and compiler options.
    
    ## 4. Gate project type-check and tests
    
    In `.husky/pre-commit`, run `lint-staged` first. Then check staged file paths so doc-only commits such as `README.md` skip type-checking and tests:
    
    ```sh
    set -e
    pnpm exec lint-staged
    
    staged=$(git diff --cached --name-only --diff-filter=ACMRD)
    if printf '%s\n' "$staged" | grep -Eq '\.(ts|tsx|mts|cts|js|jsx)$'
    then
      pnpm exec tsc --noEmit -p tsconfig.json
      pnpm test
    fi
    ```
    
    Adapt the package manager, tsconfig file, and test command to the repo. Run `tsc --noEmit -p <project config>` without passing filenames, or call the repo's project-wide `typecheck` script. If the test runner supports related-file mode (such as `vitest related --run` or `jest --findRelatedTests`), pass the staged code files to it, else run the fast test suite. Omit the test line if the repo has no test script and tell the user.
    
    ## 5. Verify in an isolated fixture
    
    Test the real hook in a temporary git clone or worktree so you do not pollute the user's history:
    
    - Stage a TypeScript file with a real type error and run `git commit`: the hook must fail and block the commit.
    - Stage a clean TypeScript file using a `tsconfig` path alias (such as `@/lib`): the hook must pass and commit cleanly.
    - Confirm any pre-existing hook (such as `.husky/commit-msg`) is still present and unchanged.
    - Stage only a `README.md` edit and time `git commit`: it must skip type-check and tests and finish in under 5 seconds.
    
    Clean up the temporary fixture and leave the repo ready without auto-committing.
    
    Based on mattpocock/skills (MIT), rewritten by Copybook.
    

Comments

Sign in to join the conversation.

No comments yet. Start the conversation.

Reviews (0)

No reviews yet.