Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Guidance for developing lifecycle hooks for Claude Code plugins from the official Anthropic repository.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/README.md
1# Hook Development Utility Scripts23These scripts help validate, test, and lint hook implementations before deployment.45## validate-hook-schema.sh67Validates `hooks.json` configuration files for correct structure and common issues.89**Usage:**10```bash11./validate-hook-schema.sh path/to/hooks.json12```1314**Checks:**15- Valid JSON syntax16- Required fields present17- Valid hook event names18- Proper hook types (command/prompt)19- Timeout values in valid ranges20- Hardcoded path detection21- Prompt hook event compatibility2223**Example:**24```bash25cd my-plugin26./validate-hook-schema.sh hooks/hooks.json27```2829## test-hook.sh3031Tests individual hook scripts with sample input before deploying to Claude Code.3233**Usage:**34```bash35./test-hook.sh [options] <hook-script> <test-input.json>36```3738**Options:**39- `-v, --verbose` - Show detailed execution information40- `-t, --timeout N` - Set timeout in seconds (default: 60)41- `--create-sample <event-type>` - Generate sample test input4243**Example:**44```bash45# Create sample test input46./test-hook.sh --create-sample PreToolUse > test-input.json4748# Test a hook script49./test-hook.sh my-hook.sh test-input.json5051# Test with verbose output and custom timeout52./test-hook.sh -v -t 30 my-hook.sh test-input.json53```5455**Features:**56- Sets up proper environment variables (CLAUDE_PROJECT_DIR, CLAUDE_PLUGIN_ROOT)57- Measures execution time58- Validates output JSON59- Shows exit codes and their meanings60- Captures environment file output6162## hook-linter.sh6364Checks hook scripts for common issues and best practices violations.6566**Usage:**67```bash68./hook-linter.sh <hook-script.sh> [hook-script2.sh ...]69```7071**Checks:**72- Shebang presence73- `set -euo pipefail` usage74- Stdin input reading75- Proper error handling76- Variable quoting (injection prevention)77- Exit code usage78- Hardcoded paths79- Long-running code detection80- Error output to stderr81- Input validation8283**Example:**84```bash85# Lint single script86./hook-linter.sh ../examples/validate-write.sh8788# Lint multiple scripts89./hook-linter.sh ../examples/*.sh90```9192## Typical Workflow93941. **Write your hook script**95```bash96vim my-plugin/scripts/my-hook.sh97```98992. **Lint the script**100```bash101./hook-linter.sh my-plugin/scripts/my-hook.sh102```1031043. **Create test input**105```bash106./test-hook.sh --create-sample PreToolUse > test-input.json107# Edit test-input.json as needed108```1091104. **Test the hook**111```bash112./test-hook.sh -v my-plugin/scripts/my-hook.sh test-input.json113```1141155. **Add to hooks.json**116```bash117# Edit my-plugin/hooks/hooks.json118```1191206. **Validate configuration**121```bash122./validate-hook-schema.sh my-plugin/hooks/hooks.json123```1241257. **Test in Claude Code**126```bash127claude --debug128```129130## Tips131132- Always test hooks before deploying to avoid breaking user workflows133- Use verbose mode (`-v`) to debug hook behavior134- Check the linter output for security and best practice issues135- Validate hooks.json after any changes136- Create different test inputs for various scenarios (safe operations, dangerous operations, edge cases)137138## Common Issues139140### Hook doesn't execute141142Check:143- Script has shebang (`#!/bin/bash`)144- Script is executable (`chmod +x`)145- Path in hooks.json is correct (use `${CLAUDE_PLUGIN_ROOT}`)146147### Hook times out148149- Reduce timeout in hooks.json150- Optimize hook script performance151- Remove long-running operations152153### Hook fails silently154155- Check exit codes (should be 0 or 2)156- Ensure errors go to stderr (`>&2`)157- Validate JSON output structure158159### Injection vulnerabilities160161- Always quote variables: `"$variable"`162- Use `set -euo pipefail`163- Validate all input fields164- Run the linter to catch issues165