| name | auto-formatter |
| description | Generate PostToolUse auto-formatter hook. Triggers: /harn:format, 'auto format', 'formatter hook', 'PostToolUse formatter' |
Auto-Formatter PostToolUse Hook Generator
Generate scripts/harness/auto_formatter.sh — a PostToolUse hook that auto-formats files after every Edit or Write tool call.
Steps
1. Detect the project formatter
Scan the project root to determine which formatter to use:
| Check | Formatter command |
|---|
.prettierrc, prettier.config.*, or "prettier" in package.json | npx prettier --write |
pyproject.toml containing [tool.black], or .black config file | black |
rustfmt.toml or Cargo.toml | rustfmt |
go.mod | gofmt -w |
| None of the above | Skip formatting (echo to stderr and exit 0) |
Use Glob and Read to check for these files. Pick the first match in the order listed above.
2. Create the formatter script
Create scripts/harness/auto_formatter.sh with the following structure. Replace {{FORMATTER_CMD}} with the detected formatter command from step 1.
#!/usr/bin/env bash
set -euo pipefail
PAYLOAD="$(cat)"
TOOL_NAME="$(echo "$PAYLOAD" | jq -r '.tool_name // empty')"
if [[ "$TOOL_NAME" != "Edit" && "$TOOL_NAME" != "Write" ]]; then
exit 0
fi
FILE_PATH="$(echo "$PAYLOAD" | jq -r '.parameters.file_path // empty')"
if [[ -z "$FILE_PATH" || ! -f "$FILE_PATH" ]]; then
exit 0
fi
timeout 5 {{FORMATTER_CMD}} "$FILE_PATH" 2>/dev/null && \
echo "Harn: Formatted $FILE_PATH" >&2 || true
exit 0
Make the script executable:
chmod +x scripts/harness/auto_formatter.sh
3. Wire into .claude/settings.json
Read the existing .claude/settings.json (or create it if missing). Add the PostToolUse hook configuration:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "bash scripts/harness/auto_formatter.sh",
"timeout": 5
}
]
}
]
}
}
If settings.json already has other hooks, merge this entry into the existing PostToolUse array without overwriting other hook types.
4. Verify
Run the script with a test payload to confirm it exits cleanly:
echo '{"tool_name":"Edit","parameters":{"file_path":"/dev/null"}}' | bash scripts/harness/auto_formatter.sh
Report which formatter was detected and confirm the hook is wired.