Library/workflow
workflowintermediateClaude Code

Claude Code Hooks & CLAUDE.md: Automate Formatting, Testing, and Context Setup

Claude Code hooks let you run shell commands automatically at key moments in your AI coding session—like formatting code after every edit or running type checks after writes—without manual intervention. CLAUDE.md is a project-level file that tells Claude about your codebase conventions, build commands, and architecture upfront, so it doesn't waste time scanning your project repeatedly. Together, these two features turn Claude Code from a chat-based assistant into a semi-automated coding pipeline.

MISSION OBJECTIVES

  1. 01Create a `.claude/settings.json` file in your project root and add a Prettier hook using the exact JSON config shown below, then open a file and make an edit to confirm auto-formatting triggers on save.
  2. 02Add a `CLAUDE.md` file to your project root and paste in your build command (e.g., `npm run build`), your linter command, and 3–5 coding conventions your team follows—Claude will read this on every session start.
  3. 03Install the `event-logger.py` script from github.com/karanb192/claude-code-hooks into your `.claude/` directory and run a Claude Code session to inspect raw event JSON, identifying which hook events fire for your most common workflows.

CODE INTELLIGENCE

bash
{
  "hooks": [
    {
      "matcher": "Edit|Write",
      "hooks": [
        {
          "type": "command",
          "command": "prettier --write \"$CLAUDE_FILE_PATHS\""
        }
      ]
    }
  ]
}

FIELD OPERATIONS

Auto-Test-on-Write Pipeline

Configure a PostToolUse hook that triggers your test runner (e.g., `jest --testPathPattern=$CLAUDE_FILE_PATHS`) every time Claude writes to a file, so you see pass/fail results inline during AI-assisted development without switching terminals.

Dangerous Command Blocker

Build a PreToolUse hook with a regex matcher that intercepts shell commands containing `rm -rf`, `DROP TABLE`, or other destructive patterns, exits with a non-zero code to block execution, and logs the attempted command to a local audit file.

STRATEGIC APPLICATIONS

  • A dev agency onboarding new clients can ship a pre-configured `.claude/` folder and `CLAUDE.md` with each project handoff, ensuring Claude enforces the client's code style and linting rules automatically without developer training.
  • A SaaS startup's CI-adjacent workflow uses hooks to run TypeScript checks after every AI-generated edit, catching type errors before they reach pull requests and reducing code review cycles.
#claude-code#hooks#automation#CLAUDE.md#prettier#formatting#lifecycle#shell#configuration#workflow
Source: WEB · Quality score: 7/10
VIEW SOURCE