feat: add jq syntax support to fix-hook-variables command

- Add official jq JSON parsing approach based on Claude Code docs
- Update command to support --syntax jq option
- Create comprehensive test scripts for all approaches
- Add test configurations to help users verify which syntax works
- Update documentation to reflect uncertainty about env vars

The jq approach is based on official Claude Code documentation and is
likely more reliable than the undocumented environment variable approach.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
rUvandClaude committed 2025-07-14 21:51:27 +00:00
1 parent 081f9392e3
commit 740a2bc40b
5 files changed
+204 -5

No files matched your search

+34 -5
View File
@@ -21,7 +21,12 @@ const VARIABLE_SYNTAXES = {
'environment': {
pattern: /\$(\w+)/g,
example: '$CLAUDE_FILE',
description: 'Environment variable syntax'
description: 'Environment variable syntax (unverified)'
},
'jq': {
pattern: null,
example: 'jq parsing of JSON input',
description: 'Official Claude Code approach using jq'
},
'wrapper': {
pattern: null,
@@ -61,6 +66,26 @@ function transformHookCommand(command, fromSyntax, toSyntax) {
});
}
if (fromSyntax === 'legacy' && toSyntax === 'jq') {
// Transform to use jq parsing of JSON input
// Extract the actual command and wrap it with jq parsing
const fileVarMatch = command.match(/\$\{file\}/);
const commandVarMatch = command.match(/\$\{command\}/);
if (fileVarMatch) {
// Replace ${file} with jq extraction
const baseCommand = command.replace(/\$\{file\}/g, '{}');
return `cat | jq -r '.tool_input.file_path // .tool_input.path // ""' | xargs -I {} ${baseCommand}`;
} else if (commandVarMatch) {
// Replace ${command} with jq extraction
const baseCommand = command.replace(/\$\{command\}/g, '{}');
return `cat | jq -r '.tool_input.command // ""' | xargs -I {} ${baseCommand}`;
}
// Fallback for other variables
return `cat | jq -r '.' | xargs -I {} ${command.replace(/\$\{(\w+)\}/g, '{}')}`;
}
if (toSyntax === 'wrapper') {
// Generate wrapper script path
const scriptName = command.includes('post-edit') ? 'post-edit-hook.sh' :
@@ -302,7 +327,7 @@ export const fixHookVariablesCommandConfig = {
usage: 'fix-hook-variables [settings-file...]',
options: [
{ flag: '--no-backup', description: 'Skip creating backup files' },
{ flag: '--syntax <type>', description: 'Force specific syntax: environment, wrapper' },
{ flag: '--syntax <type>', description: 'Force specific syntax: environment, jq, wrapper' },
{ flag: '--test', description: 'Create test hook configuration' }
],
examples: [
@@ -320,9 +345,13 @@ This command will:
• Create wrapper scripts if needed
• Backup original settings files
Known working syntaxes:
• Environment variables: $CLAUDE_EDITED_FILE, $CLAUDE_COMMAND
• Wrapper scripts: Intercept and pass variables correctly
Available syntaxes:
• environment: Use environment variables like $CLAUDE_EDITED_FILE (unverified)
• jq: Use official jq JSON parsing approach (recommended)
• wrapper: Create wrapper scripts to handle variables
Note: The 'jq' syntax is based on official Claude Code documentation and is likely
the most reliable approach for Claude Code 1.0.51+.
For more information: https://github.com/ruvnet/claude-flow/issues/249`
};
@@ -0,0 +1,27 @@
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "echo \"=== HOOK TEST $(date) ===\" >> .claude/hook-test-all.log"
},
{
"type": "command",
"command": "echo \"1. ENV VARS - CLAUDE_EDITED_FILE: '$CLAUDE_EDITED_FILE', CLAUDE_FILE: '$CLAUDE_FILE'\" >> .claude/hook-test-all.log"
},
{
"type": "command",
"command": "echo \"2. ALL ENV VARS:\" >> .claude/hook-test-all.log && env | grep -i claude >> .claude/hook-test-all.log || echo \"No CLAUDE env vars found\" >> .claude/hook-test-all.log"
},
{
"type": "command",
"command": "echo \"3. STDIN CONTENT:\" >> .claude/hook-test-all.log && cat >> .claude/hook-test-all.log"
}
]
}
]
}
}
+15
View File
@@ -0,0 +1,15 @@
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "echo \"ENV TEST - CLAUDE_EDITED_FILE: $CLAUDE_EDITED_FILE, CLAUDE_FILE: $CLAUDE_FILE, FILE: $FILE\" >> .claude/env-test.log"
}
]
}
]
}
}
+113
View File
@@ -0,0 +1,113 @@
#!/bin/bash
# Test script for Claude Code hook variables
# This script helps determine which variable approach works with your Claude Code version
echo "🧪 Claude Code Hook Variable Test"
echo "================================="
echo ""
echo "This script will help you test which hook variable approach works with your Claude Code version."
echo ""
# Check Claude Code version
echo "📌 Claude Code Version:"
claude --version 2>/dev/null || echo "Claude Code not found in PATH"
echo ""
# Clean up previous test logs
rm -f .claude/hook-test-*.log
# Test 1: Environment Variables
echo "Test 1: Environment Variables Approach"
echo "--------------------------------------"
cat > .claude/test-env-vars.json << 'EOF'
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write",
"hooks": [
{
"type": "command",
"command": "echo \"[ENV_VAR_TEST] File: $CLAUDE_EDITED_FILE\" >> .claude/hook-test-env.log"
}
]
}
]
}
}
EOF
echo "✅ Created .claude/test-env-vars.json"
echo ""
# Test 2: JQ JSON Parsing
echo "Test 2: JQ JSON Parsing Approach"
echo "--------------------------------"
cat > .claude/test-jq.json << 'EOF'
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write",
"hooks": [
{
"type": "command",
"command": "cat | jq -r '\"[JQ_TEST] File: \\(.tool_input.file_path // .tool_input.path // \\\"NONE\\\")\"' >> .claude/hook-test-jq.log 2>&1 || echo \"[JQ_TEST] Error parsing JSON\" >> .claude/hook-test-jq.log"
}
]
}
]
}
}
EOF
echo "✅ Created .claude/test-jq.json"
echo ""
# Test 3: Comprehensive Test
echo "Test 3: Comprehensive Test (All Approaches)"
echo "------------------------------------------"
cat > .claude/test-comprehensive.json << 'EOF'
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write",
"hooks": [
{
"type": "command",
"command": "echo \"=== COMPREHENSIVE TEST $(date) ===\" >> .claude/hook-test-comprehensive.log"
},
{
"type": "command",
"command": "echo \"ENV: CLAUDE_EDITED_FILE='$CLAUDE_EDITED_FILE'\" >> .claude/hook-test-comprehensive.log"
},
{
"type": "command",
"command": "echo \"STDIN:\" >> .claude/hook-test-comprehensive.log && cat >> .claude/hook-test-comprehensive.log"
}
]
}
]
}
}
EOF
echo "✅ Created .claude/test-comprehensive.json"
echo ""
echo "📋 Instructions:"
echo "1. Copy one of the test files to .claude/settings.json:"
echo " cp .claude/test-env-vars.json .claude/settings.json"
echo ""
echo "2. Open Claude Code and create a test file:"
echo " claude -c \"Create a file test.txt with content: Hello World\""
echo ""
echo "3. Check the corresponding log file:"
echo " - Environment vars: .claude/hook-test-env.log"
echo " - JQ parsing: .claude/hook-test-jq.log"
echo " - Comprehensive: .claude/hook-test-comprehensive.log"
echo ""
echo "4. Report which approach worked at:"
echo " https://github.com/ruvnet/claude-flow/issues/249"
echo ""
echo "🔍 To check results after testing:"
echo " ls -la .claude/hook-test-*.log"
echo " cat .claude/hook-test-*.log"
+15
View File
@@ -0,0 +1,15 @@
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "echo \"JQ TEST - Input: $(cat)\" >> .claude/jq-test.log && cat | jq -r '\"File: \\(.tool_input.file_path // .tool_input.path // .file_path // \"NONE\")\"' >> .claude/jq-test.log"
}
]
}
]
}
}