forked from affaan-m/ECC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-edit-console-warn.js
More file actions
65 lines (58 loc) · 1.76 KB
/
Copy pathpost-edit-console-warn.js
File metadata and controls
65 lines (58 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
/**
* PostToolUse Hook: Warn about console.log statements after edits
*
* Cross-platform (Windows, macOS, Linux)
*
* Runs after Edit tool use. If the edited JS/TS file contains console.log
* statements, warns with line numbers to help remove debug statements
* before committing.
*/
const { readFile } = require('../lib/utils');
const MAX_STDIN = 1024 * 1024; // 1MB limit
function run(data) {
const warnings = [];
try {
const input = JSON.parse(data);
const filePath = input.tool_input?.file_path;
if (filePath && /\.(ts|tsx|js|jsx)$/.test(filePath)) {
const content = readFile(filePath);
if (content) {
const matches = content
.split('\n')
.map((line, index) => ({ line, index }))
.filter(item => /console\.log/.test(item.line))
.map(item => `${item.index + 1}: ${item.line.trim()}`);
if (matches.length > 0) {
warnings.push(`[Hook] WARNING: console.log found in ${filePath}`);
warnings.push(...matches.slice(0, 5));
warnings.push('[Hook] Remove console.log before committing');
}
}
}
} catch {
// Invalid input — pass through
}
return {
stdout: data,
stderr: warnings.join('\n'),
exitCode: 0,
};
}
if (require.main === module) {
let data = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', chunk => {
if (data.length < MAX_STDIN) {
const remaining = MAX_STDIN - data.length;
data += chunk.substring(0, remaining);
}
});
process.stdin.on('end', () => {
const result = run(data);
if (result.stderr) process.stderr.write(`${result.stderr}\n`);
process.stdout.write(result.stdout);
process.exitCode = result.exitCode;
});
}
module.exports = { run };