|
3 | 3 | import { execSync } from 'node:child_process';
|
4 | 4 | import { existsSync } from 'node:fs';
|
5 | 5 |
|
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...'); |
28 | 8 |
|
29 |
| -// Run prettier on the files |
30 | 9 | 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 |
31 | 39 | console.log('Running prettier on staged files...');
|
32 | 40 | const fileList = filesToLint.join(' ');
|
33 | 41 | execSync(`bun prettier --write ${fileList}`, { stdio: 'inherit' });
|
|
0 commit comments