From a0b84f7b862f3bee93add8600bc98761657bd029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=88=90=E5=96=86?= Date: Mon, 26 Jan 2026 12:00:39 +0800 Subject: [PATCH] Fix: Move Stop hook inline code to separate script file Fixes #78 ## Problem The Stop hook used inline JavaScript code with `node -e`, which caused shell syntax errors on macOS/zsh due to special characters (parentheses, braces, arrow functions) being misinterpreted by the shell. Error message: /bin/sh: -c: line 0: syntax error near unexpected token \`(' ## Solution - Created scripts/hooks/check-console-log.js with the hook logic - Updated hooks/hooks.json to reference the external script - This follows the same pattern as other hooks in the plugin ## Benefits - Fixes shell compatibility issues across different environments - Improves code maintainability (separate, well-documented script) - Follows plugin's own best practices - Makes the code easier to test and debug ## Testing Tested on macOS with zsh - no more syntax errors. The hook still functions correctly to detect console.log statements. --- hooks/hooks.json | 2 +- scripts/hooks/check-console-log.js | 61 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100755 scripts/hooks/check-console-log.js diff --git a/hooks/hooks.json b/hooks/hooks.json index 5307329..04b4a82 100644 --- a/hooks/hooks.json +++ b/hooks/hooks.json @@ -137,7 +137,7 @@ "hooks": [ { "type": "command", - "command": "node -e \"const{execSync}=require('child_process');const fs=require('fs');let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{execSync('git rev-parse --git-dir',{stdio:'pipe'})}catch{console.log(d);process.exit(0)}try{const files=execSync('git diff --name-only HEAD',{encoding:'utf8',stdio:['pipe','pipe','pipe']}).split('\\n').filter(f=>/\\.(ts|tsx|js|jsx)$/.test(f)&&fs.existsSync(f));let hasConsole=false;for(const f of files){if(fs.readFileSync(f,'utf8').includes('console.log')){console.error('[Hook] WARNING: console.log found in '+f);hasConsole=true}}if(hasConsole)console.error('[Hook] Remove console.log statements before committing')}catch(e){}console.log(d)})\"" + "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/hooks/check-console-log.js\"" } ], "description": "Check for console.log in modified files after each response" diff --git a/scripts/hooks/check-console-log.js b/scripts/hooks/check-console-log.js new file mode 100755 index 0000000..e1aa3c2 --- /dev/null +++ b/scripts/hooks/check-console-log.js @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +/** + * Stop Hook: Check for console.log statements in modified files + * + * This hook runs after each response and checks if any modified + * JavaScript/TypeScript files contain console.log statements. + * It provides warnings to help developers remember to remove + * debug statements before committing. + */ + +const { execSync } = require('child_process'); +const fs = require('fs'); + +let data = ''; + +// Read stdin +process.stdin.on('data', chunk => { + data += chunk; +}); + +process.stdin.on('end', () => { + try { + // Check if we're in a git repository + try { + execSync('git rev-parse --git-dir', { stdio: 'pipe' }); + } catch { + // Not in a git repo, just pass through the data + console.log(data); + process.exit(0); + } + + // Get list of modified files + const files = execSync('git diff --name-only HEAD', { + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'pipe'] + }) + .split('\n') + .filter(f => /\.(ts|tsx|js|jsx)$/.test(f) && fs.existsSync(f)); + + let hasConsole = false; + + // Check each file for console.log + for (const file of files) { + const content = fs.readFileSync(file, 'utf8'); + if (content.includes('console.log')) { + console.error(`[Hook] WARNING: console.log found in ${file}`); + hasConsole = true; + } + } + + if (hasConsole) { + console.error('[Hook] Remove console.log statements before committing'); + } + } catch (error) { + // Silently ignore errors (git might not be available, etc.) + } + + // Always output the original data + console.log(data); +});