Skip to content

Commit 7217892

Browse files
committed
Testing pre-commit hook with multiple files
1 parent e6822a7 commit 7217892

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

formatting-test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// This file has intentional formatting issues for testing the pre-commit hook
2-
function badlyFormattedFunction() {
3-
const greeting = 'hello world';
4-
console.log(greeting);
5-
return 1 + 1;
1+
// This file has VERY BAD formatting issues for testing
2+
function reallyBadlyFormattedFunction() {
3+
const newGreeting = 'THIS IS A TEST';
4+
for (let i = 0; i < 5; i++) {
5+
console.log(newGreeting + i);
6+
}
7+
return 1 + 2 + 3 + 4 + 5;
68
}

scripts/pre-commit-lint.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,39 @@
33
import { execSync } from 'node:child_process';
44
import { existsSync } from 'node:fs';
55

6-
// Get all staged files
7-
const stagedFiles = execSync('git diff --cached --name-only --diff-filter=ACMR')
8-
.toString()
9-
.trim()
10-
.split('\n')
11-
.filter(Boolean);
12-
13-
if (stagedFiles.length === 0) {
14-
console.log('No staged files to lint');
15-
process.exit(0);
16-
}
17-
18-
// Filter for files we want to process
19-
const filesToLint = stagedFiles.filter((file) => {
20-
const extensions = ['.js', '.jsx', '.ts', '.tsx', '.json', '.css', '.md'];
21-
return extensions.some((ext) => file.endsWith(ext));
22-
});
23-
24-
if (filesToLint.length === 0) {
25-
console.log('No matching files to lint');
26-
process.exit(0);
27-
}
6+
// Log that we're starting
7+
console.log('Running pre-commit hook...');
288

29-
// Run prettier on the files
309
try {
10+
// Get all staged files using git diff --staged instead
11+
console.log('Checking for staged files...');
12+
const stagedFiles = execSync('git diff --staged --name-only')
13+
.toString()
14+
.trim()
15+
.split('\n')
16+
.filter(Boolean);
17+
18+
console.log(`Found ${stagedFiles.length} staged files.`);
19+
20+
if (stagedFiles.length === 0) {
21+
console.log('No staged files to lint');
22+
process.exit(0);
23+
}
24+
25+
// Filter for files we want to process
26+
const filesToLint = stagedFiles.filter((file) => {
27+
const extensions = ['.js', '.jsx', '.ts', '.tsx', '.json', '.css', '.md'];
28+
return extensions.some((ext) => file.endsWith(ext));
29+
});
30+
31+
console.log(`Found ${filesToLint.length} files to format: ${filesToLint.join(', ')}`);
32+
33+
if (filesToLint.length === 0) {
34+
console.log('No matching files to lint');
35+
process.exit(0);
36+
}
37+
38+
// Run prettier on the files
3139
console.log('Running prettier on staged files...');
3240
const fileList = filesToLint.join(' ');
3341
execSync(`bun prettier --write ${fileList}`, { stdio: 'inherit' });

test-bad-format.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This is a completely new file with bad formatting
2+
function veryBadFormatting() {
3+
const x = 10;
4+
const y = 20;
5+
for (let i = 0; i < x; i++) {
6+
console.log(i + y);
7+
}
8+
return x + y;
9+
}

0 commit comments

Comments
 (0)