mirror of
https://github.com/sweetwisdom/everything-claude-code-zh.git
synced 2026-03-21 22:10:09 +00:00
Merge pull request #80 from lichengzhe/fix/stop-hook-shell-error
Fixes #78 - Stop hook shell syntax error on macOS/zsh by moving inline Node.js to separate script
This commit is contained in:
@@ -137,7 +137,7 @@
|
|||||||
"hooks": [
|
"hooks": [
|
||||||
{
|
{
|
||||||
"type": "command",
|
"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"
|
"description": "Check for console.log in modified files after each response"
|
||||||
|
|||||||
61
scripts/hooks/check-console-log.js
Executable file
61
scripts/hooks/check-console-log.js
Executable file
@@ -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);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user