🎣writing-hookify-rules
- プラグイン
- hookify
- ソース
- GitHub で見る ↗
説明
次のような場合に使用: ユーザーが「hookify ルールを作成する」「フックルールを書く」「hookify を設定する」「hookify ルールを追加する」と依頼した場合、またはhookify のルール構文やパターンに関するガイダンスを必要としている場合。
原文を表示
This skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns.
ユースケース
- ✓hookify ルールを作成する
- ✓フックルールを書く
- ✓hookify を設定する
- ✓hookify ルールを追加する
- ✓ルール構文やパターンのガイダンスが必要
本文(日本語訳)
Hookify ルールの作成
概要
Hookify ルールは、YAMLフロントマターを持つMarkdownファイルです。
監視するパターンと、そのパターンにマッチした際に表示するメッセージを定義します。
ルールは .claude/hookify.{rule-name}.local.md ファイルとして保存されます。
ルールファイルの形式
基本構造
---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: regex-pattern-here
---
このルールがトリガーされたときにClaudeへ表示するメッセージ。
Markdownの書式、警告、提案などを含めることができます。
フロントマターのフィールド
name(必須): ルールの一意な識別子
- ケバブケースを使用:
warn-dangerous-rm、block-console-log - 説明的かつアクション指向の名前にする
- 動詞で始める: warn、prevent、block、require、check
enabled(必須): ルールを有効/無効にするBoolean値
true: ルールが有効false: ルールが無効(トリガーされない)- ルールを削除せずに切り替え可能
event(必須): トリガーするhookイベントの種類
bash: Bashツールのコマンドfile: Edit、Write、MultiEditツールstop: agentが停止しようとするときprompt: ユーザーがプロンプトを送信するときall: すべてのイベント
action(省略可): ルールにマッチした際の動作
warn: メッセージを表示するが操作は許可する(デフォルト)block: 操作を禁止する(PreToolUse)またはセッションを停止する(Stopイベント)- 省略した場合は
warnがデフォルト
pattern(シンプル形式): マッチさせる正規表現パターン
- 単一条件のシンプルなルールに使用
- コマンド(bash)またはnew_text(file)に対してマッチ
- Pythonの正規表現構文を使用
例:
event: bash
pattern: rm\s+-rf
高度な形式(複数条件)
複数の条件を持つ複雑なルールの場合:
---
name: warn-env-file-edits
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
.envファイルにAPIキーを追加しようとしています。このファイルが.gitignoreに含まれているか確認してください!
conditionのフィールド:
field: チェック対象のフィールド- bash の場合:
command - file の場合:
file_path、new_text、old_text、content
- bash の場合:
operator: マッチ方法regex_match: 正規表現によるパターンマッチcontains: 部分文字列チェックequals: 完全一致not_contains: 部分文字列が含まれていないことstarts_with: プレフィックスチェックends_with: サフィックスチェック
pattern: マッチさせるパターンまたは文字列
すべての条件が一致した場合にのみルールがトリガーされます。
メッセージ本文
フロントマター以降のMarkdownコンテンツが、ルールのトリガー時にClaudeへ表示されます。
良いメッセージの条件:
- 何を検出したかを説明する
- なぜ問題があるかを説明する
- 代替手段やベストプラクティスを提案する
- 書式(太字、リストなど)を使って見やすくする
例:
⚠️ **console.logが検出されました!**
本番コードにconsole.logを追加しようとしています。
**なぜ問題なのか:**
- デバッグログを本番環境にリリースすべきではありません
- console.logは機密データを露出させる可能性があります
- ブラウザのパフォーマンスに影響します
**代替手段:**
- 適切なロギングライブラリを使用する
- コミット前に削除する
- 条件付きデバッグビルドを使用する
イベントタイプガイド
bash イベント
Bashコマンドのパターンにマッチします:
---
event: bash
pattern: sudo\s+|rm\s+-rf|chmod\s+777
---
危険なコマンドが検出されました!
よく使うパターン:
- 危険なコマンド:
rm\s+-rf、dd\s+if=、mkfs - 権限昇格:
sudo\s+、su\s+ - 権限の問題:
chmod\s+777、chown\s+root
file イベント
Edit/Write/MultiEdit 操作にマッチします:
---
event: file
pattern: console\.log\(|eval\(|innerHTML\s*=
---
問題のある可能性があるコードパターンが検出されました!
異なるフィールドでマッチさせる場合:
---
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.tsx?$
- field: new_text
operator: regex_match
pattern: console\.log\(
---
TypeScriptファイルにconsole.logがあります!
よく使うパターン:
- デバッグコード:
console\.log\(、debugger、print\( - セキュリティリスク:
eval\(、innerHTML\s*=、dangerouslySetInnerHTML - 機密ファイル:
\.env$、credentials、\.pem$ - 生成ファイル:
node_modules/、dist/、build/
stop イベント
agentが停止しようとするタイミングにマッチします(完了チェック用):
---
event: stop
pattern: .*
---
停止する前に以下を確認してください:
- [ ] テストを実行した
- [ ] ビルドが成功した
- [ ] ドキュメントを更新した
次のような場合に使用:
- 必須ステップのリマインダー
- 完了チェックリスト
- プロセスの強制確認
prompt イベント
ユーザーのプロンプト内容にマッチします(上級者向け):
---
event: prompt
conditions:
- field: user_prompt
operator: contains
pattern: deploy to production
---
本番デプロイのチェックリスト:
- [ ] テストはパスしていますか?
- [ ] チームによるレビューは完了しましたか?
- [ ] モニタリングの準備は整っていますか?
パターン作成のヒント
正規表現の基本
リテラル文字: ほとんどの文字はそのままマッチします
rmは "rm" にマッチconsole.logは "console.log" にマッチ
特殊文字はエスケープが必要:
.(任意の文字)→\.(リテラルのドット)()→\(\)(リテラルの括弧)[]→\[\](リテラルの角括弧)
よく使うメタ文字:
\s- 空白文字(スペース、タブ、改行)\d- 数字(0〜9)\w- 単語文字(a〜z、A〜Z、0〜9、_).- 任意の文字+- 1回以上*- 0回以上?- 0回または1回|- OR
例:
rm\s+-rf マッチ: rm -rf, rm -rf
console\.log\( マッチ: console.log(
(eval|exec)\( マッチ: eval( または exec(
chmod\s+777 マッチ: chmod 777, chmod 777
API_KEY\s*= マッチ: API_KEY=, API_KEY =
パターンのテスト
使用する前に正規表現パターンをテストしてください:
python3 -c "import re; print(re.search(r'your_pattern', 'test text'))"
またはオンラインの正規表現テスター(regex101.com でPythonフレーバーを選択)を使用してください。
よくある落とし穴
パターンが広すぎる:
pattern: log # "log"、"login"、"dialog"、"catalog" にもマッチしてしまう
改善例: console\.log\(|logger\.
パターンが限定的すぎる:
pattern: rm -rf /tmp # この完全なパスにしかマッチしない
改善例: rm\s+-rf
エスケープの問題:
- YAMLのクォートあり文字列:
"pattern"ではバックスラッシュを二重に\\sと書く必要がある - YAMLのクォートなし:
pattern: \sはそのまま使える - 推奨: YAMLではクォートなしでパターンを記述する
ファイルの整理
保存場所: すべてのルールを .claude/ ディレクトリに配置
命名規則: .claude/hookify.{descriptive-name}.local.md
Gitignore: .claude/*.local.md を .gitignore に追加
良い名前の例:
hookify.dangerous-rm.local.mdhookify.console-log.local.mdhookify.require-tests.local.mdhookify.sensitive-files.local.md
悪い名前の例:
hookify.rule1.local.md(説明的でない)hookify.md(.localが欠けている)danger.local.md(hookifyプレフィックスが欠けている)
ワークフロー
ルールの作成
- 防ぎたい動作を特定する
- 関連するツールを特定する(Bash、Editなど)
- イベントタイプを選択する(bash、file、stopなど)
- 正規表現パターンを作成する
- プロジェクトルートに
.claude/hookify.{name}.local.mdファイルを作成する - すぐにテストする — ルールは次のツール使用時に動的に読み込まれます
ルールの改良
.local.mdファイルを編集する- パターンまたはメッセージを調整する
- すぐにテストする — 変更は次のツール使用時に反映されます
ルールの無効化
一時的に: フロントマターで enabled: false に設定する
恒久的に: .local.md ファイルを削除する
サンプル
完全なサンプルは ${CLAUDE_PLUGIN_ROOT}/examples/ を参照してください:
dangerous-rm.local.md- 危険なrmコマンドをブロックconsole-log-warning.local.md- console.logについて警告sensitive-files-warning.local.md- .envファイルの編集について警告
クイックリファレンス
最小構成のルール:
---
name: my-rule
enabled: true
event: bash
pattern: dangerous_command
---
警告メッセージをここに記述
条件付きのルール:
---
name: my-rule
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.ts$
- field: new_text
operator: contains
pattern: any
---
警告メッセージ
イベントタイプ:
bash- Bashコマンドfile- ファイル編集stop- 完了チェックprompt- ユーザー入力all- すべてのイベント
フィールドのオプション:
- Bash:
command - File:
file_path、new_text、old_text、content - Prompt:
user_prompt
オペレーター:
regex_match、contains、equals、not_contains、starts_with、ends_with
原文(English)を表示
Writing Hookify Rules
Overview
Hookify rules are markdown files with YAML frontmatter that define patterns to watch for and messages to show when those patterns match. Rules are stored in .claude/hookify.{rule-name}.local.md files.
Rule File Format
Basic Structure
---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: regex-pattern-here
---
Message to show Claude when this rule triggers.
Can include markdown formatting, warnings, suggestions, etc.
Frontmatter Fields
name (required): Unique identifier for the rule
- Use kebab-case:
warn-dangerous-rm,block-console-log - Be descriptive and action-oriented
- Start with verb: warn, prevent, block, require, check
enabled (required): Boolean to activate/deactivate
true: Rule is activefalse: Rule is disabled (won't trigger)- Can toggle without deleting rule
event (required): Which hook event to trigger on
bash: Bash tool commandsfile: Edit, Write, MultiEdit toolsstop: When agent wants to stopprompt: When user submits a promptall: All events
action (optional): What to do when rule matches
warn: Show message but allow operation (default)block: Prevent operation (PreToolUse) or stop session (Stop events)- If omitted, defaults to
warn
pattern (simple format): Regex pattern to match
- Used for simple single-condition rules
- Matches against command (bash) or new_text (file)
- Python regex syntax
Example:
event: bash
pattern: rm\s+-rf
Advanced Format (Multiple Conditions)
For complex rules with multiple conditions:
---
name: warn-env-file-edits
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
You're adding an API key to a .env file. Ensure this file is in .gitignore!
Condition fields:
field: Which field to check- For bash:
command - For file:
file_path,new_text,old_text,content
- For bash:
operator: How to matchregex_match: Regex pattern matchingcontains: Substring checkequals: Exact matchnot_contains: Substring must NOT be presentstarts_with: Prefix checkends_with: Suffix check
pattern: Pattern or string to match
All conditions must match for rule to trigger.
Message Body
The markdown content after frontmatter is shown to Claude when the rule triggers.
Good messages:
- Explain what was detected
- Explain why it's problematic
- Suggest alternatives or best practices
- Use formatting for clarity (bold, lists, etc.)
Example:
⚠️ **Console.log detected!**
You're adding console.log to production code.
**Why this matters:**
- Debug logs shouldn't ship to production
- Console.log can expose sensitive data
- Impacts browser performance
**Alternatives:**
- Use a proper logging library
- Remove before committing
- Use conditional debug builds
Event Type Guide
bash Events
Match Bash command patterns:
---
event: bash
pattern: sudo\s+|rm\s+-rf|chmod\s+777
---
Dangerous command detected!
Common patterns:
- Dangerous commands:
rm\s+-rf,dd\s+if=,mkfs - Privilege escalation:
sudo\s+,su\s+ - Permission issues:
chmod\s+777,chown\s+root
file Events
Match Edit/Write/MultiEdit operations:
---
event: file
pattern: console\.log\(|eval\(|innerHTML\s*=
---
Potentially problematic code pattern detected!
Match on different fields:
---
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.tsx?$
- field: new_text
operator: regex_match
pattern: console\.log\(
---
Console.log in TypeScript file!
Common patterns:
- Debug code:
console\.log\(,debugger,print\( - Security risks:
eval\(,innerHTML\s*=,dangerouslySetInnerHTML - Sensitive files:
\.env$,credentials,\.pem$ - Generated files:
node_modules/,dist/,build/
stop Events
Match when agent wants to stop (completion checks):
---
event: stop
pattern: .*
---
Before stopping, verify:
- [ ] Tests were run
- [ ] Build succeeded
- [ ] Documentation updated
Use for:
- Reminders about required steps
- Completion checklists
- Process enforcement
prompt Events
Match user prompt content (advanced):
---
event: prompt
conditions:
- field: user_prompt
operator: contains
pattern: deploy to production
---
Production deployment checklist:
- [ ] Tests passing?
- [ ] Reviewed by team?
- [ ] Monitoring ready?
Pattern Writing Tips
Regex Basics
Literal characters: Most characters match themselves
rmmatches "rm"console.logmatches "console.log"
Special characters need escaping:
.(any char) →\.(literal dot)()→\(\)(literal parens)[]→\[\](literal brackets)
Common metacharacters:
\s- whitespace (space, tab, newline)\d- digit (0-9)\w- word character (a-z, A-Z, 0-9, _).- any character+- one or more*- zero or more?- zero or one|- OR
Examples:
rm\s+-rf Matches: rm -rf, rm -rf
console\.log\( Matches: console.log(
(eval|exec)\( Matches: eval( or exec(
chmod\s+777 Matches: chmod 777, chmod 777
API_KEY\s*= Matches: API_KEY=, API_KEY =
Testing Patterns
Test regex patterns before using:
python3 -c "import re; print(re.search(r'your_pattern', 'test text'))"
Or use online regex testers (regex101.com with Python flavor).
Common Pitfalls
Too broad:
pattern: log # Matches "log", "login", "dialog", "catalog"
Better: console\.log\(|logger\.
Too specific:
pattern: rm -rf /tmp # Only matches exact path
Better: rm\s+-rf
Escaping issues:
- YAML quoted strings:
"pattern"requires double backslashes\\s - YAML unquoted:
pattern: \sworks as-is - Recommendation: Use unquoted patterns in YAML
File Organization
Location: All rules in .claude/ directory
Naming: .claude/hookify.{descriptive-name}.local.md
Gitignore: Add .claude/*.local.md to .gitignore
Good names:
hookify.dangerous-rm.local.mdhookify.console-log.local.mdhookify.require-tests.local.mdhookify.sensitive-files.local.md
Bad names:
hookify.rule1.local.md(not descriptive)hookify.md(missing .local)danger.local.md(missing hookify prefix)
Workflow
Creating a Rule
- Identify unwanted behavior
- Determine which tool is involved (Bash, Edit, etc.)
- Choose event type (bash, file, stop, etc.)
- Write regex pattern
- Create
.claude/hookify.{name}.local.mdfile in project root - Test immediately - rules are read dynamically on next tool use
Refining a Rule
- Edit the
.local.mdfile - Adjust pattern or message
- Test immediately - changes take effect on next tool use
Disabling a Rule
Temporary: Set enabled: false in frontmatter
Permanent: Delete the .local.md file
Examples
See ${CLAUDE_PLUGIN_ROOT}/examples/ for complete examples:
dangerous-rm.local.md- Block dangerous rm commandsconsole-log-warning.local.md- Warn about console.logsensitive-files-warning.local.md- Warn about editing .env files
Quick Reference
Minimum viable rule:
---
name: my-rule
enabled: true
event: bash
pattern: dangerous_command
---
Warning message here
Rule with conditions:
---
name: my-rule
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.ts$
- field: new_text
operator: contains
pattern: any
---
Warning message
Event types:
bash- Bash commandsfile- File editsstop- Completion checksprompt- User inputall- All events
Field options:
- Bash:
command - File:
file_path,new_text,old_text,content - Prompt:
user_prompt
Operators:
regex_match,contains,equals,not_contains,starts_with,ends_with
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。