Rule to Hook Converter Command
Converts natural language rules from CLAUDE.md into deterministic Claude Code hooks for reliable enforcement. Turns 'never push to main' into an actual PreToolUse hook that blocks it. Bridges the gap between guidelines and enforcement.
Command
/rule-to-hook
Description
Analyzes rules written in CLAUDE.md (or similar instruction files) and converts them into deterministic Claude Code hooks. Rules in CLAUDE.md are advisory -- Claude might forget them during long sessions or after compaction. Hooks are enforced programmatically and cannot be bypassed.
Behavior
Arguments
$ARGUMENTS-- Specific rule to convert, or empty to analyze all rules in CLAUDE.md
Conversion Process
-
Parse CLAUDE.md for enforceable rules:
# Rules found in CLAUDE.md: - "Never push directly to main" - "Always run tests before committing" - "Don't modify .env files" - "Use pnpm, not npm" - "Format code with Prettier after editing" -
Classify each rule by enforcement type:
Rule Hook Type Matcher Enforceability Never push to main PreToolUse Bash High -- can check git push commands Run tests before commit Stop agent Medium -- can run tests before stopping Don't modify .env PreToolUse Edit/Write High -- can check file paths Use pnpm not npm PreToolUse Bash High -- can check for npm commands Format with Prettier PostToolUse Edit/Write High -- can run formatter -
Generate hooks for each convertible rule:
{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "bash -c 'CMD=$(jq -r .tool_input.command); if echo \"$CMD\" | grep -qE \"git push.*(main|master)\"; then echo \"BLOCKED: No direct push to main\" >&2; exit 2; fi'" } ] }, { "matcher": "Bash", "hooks": [ { "type": "command", "command": "bash -c 'CMD=$(jq -r .tool_input.command); if echo \"$CMD\" | grep -qE \"^npm \"; then echo \"BLOCKED: Use pnpm instead of npm\" >&2; exit 2; fi'" } ] }, { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "bash -c 'FILE=$(jq -r .tool_input.file_path); if [[ \"$FILE\" == *.env* ]]; then echo \"BLOCKED: Cannot modify .env files\" >&2; exit 2; fi'" } ] } ], "PostToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write 2>/dev/null || true" } ] } ] } } -
Report unconvertible rules:
### Rules That Cannot Be Automated | Rule | Reason | |------|--------| | "Write clean, readable code" | Subjective, no deterministic check | | "Follow the team's coding style" | Too vague to enforce programmatically |
Output Format
## Rule to Hook Conversion ### Converted (4 rules -> 5 hooks) - "Never push to main" -> PreToolUse/Bash hook - "Don't modify .env" -> PreToolUse/Edit|Write hook - "Use pnpm" -> PreToolUse/Bash hook - "Format with Prettier" -> PostToolUse/Edit|Write hook ### Not Convertible (2 rules) - "Write clean code" -- Subjective, keep in CLAUDE.md - "Consider performance" -- Requires human judgment ### Generated Configuration (merged JSON ready to paste into settings.json)
Examples
# Convert all rules from CLAUDE.md /rule-to-hook # Convert a specific rule /rule-to-hook never use npm, always use pnpm # Convert rules from a different file /rule-to-hook --file=.claude/team-rules.md
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Git Commit Message Generator
Generates well-structured conventional commit messages by analyzing staged changes. Follows Conventional Commits spec with scope detection.
React Component Scaffolder
Scaffolds a complete React component with TypeScript types, Tailwind styles, Storybook stories, and unit tests. Follows project conventions automatically.
CI/CD Pipeline Generator
Generates GitHub Actions workflows for CI/CD including linting, testing, building, and deploying. Detects project stack automatically.