R

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.

CommandCommunityautomationv1.0.0MIT
0 views0 copies

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

  1. 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"
  2. Classify each rule by enforcement type:

    RuleHook TypeMatcherEnforceability
    Never push to mainPreToolUseBashHigh -- can check git push commands
    Run tests before commitStopagentMedium -- can run tests before stopping
    Don't modify .envPreToolUseEdit/WriteHigh -- can check file paths
    Use pnpm not npmPreToolUseBashHigh -- can check for npm commands
    Format with PrettierPostToolUseEdit/WriteHigh -- can run formatter
  3. 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" } ] } ] } }
  4. 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
Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates