mirror of
https://github.com/affaan-m/ECC.git
synced 2026-10-26 18:35:05 +00:00
fix(hooks): find console.log in modified files from a subdirectory session
check-console-log took the paths from `git diff --name-only HEAD`, which are relative to the repository root, and checked them with fs.existsSync from the hook's working directory. In a session started in a subdirectory, such as a monorepo package, no path existed from there, so the hook never warned. Resolve each path from the repository root. The exclusion patterns and the warning still use the root-relative path.
This commit is contained in:
2 files changed
+31
-3
No files matched your search
@@ -14,7 +14,8 @@
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const { isGitRepo, getGitModifiedFiles, readFile, log } = require('../lib/utils');
|
||||
const path = require('path');
|
||||
const { isGitRepo, getGitModifiedFiles, readFile, log, runCommand } = require('../lib/utils');
|
||||
|
||||
// Files where console.log is expected and should not trigger warnings
|
||||
const EXCLUDED_PATTERNS = [
|
||||
@@ -66,14 +67,17 @@ process.stdin.on('end', () => {
|
||||
return;
|
||||
}
|
||||
|
||||
// git reports paths from the repository root, and the session can run in a
|
||||
// subdirectory of it, so read each file from the root.
|
||||
const root = runCommand('git rev-parse --show-toplevel').output;
|
||||
const files = getGitModifiedFiles(['\\.tsx?$', '\\.jsx?$'])
|
||||
.filter(f => fs.existsSync(f))
|
||||
.filter(f => fs.existsSync(path.join(root, f)))
|
||||
.filter(f => !EXCLUDED_PATTERNS.some(pattern => pattern.test(f)));
|
||||
|
||||
let hasConsole = false;
|
||||
|
||||
for (const file of files) {
|
||||
const content = readFile(file);
|
||||
const content = readFile(path.join(root, file));
|
||||
if (content && content.includes('console.log')) {
|
||||
log(`[Hook] WARNING: console.log found in ${file}`);
|
||||
hasConsole = true;
|
||||
|
||||
Reference in new issue
Block a user