|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execSync } from 'child_process' |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | + |
| 6 | +// Get the root directory |
| 7 | +const rootDir = path.resolve(__dirname, '..') |
| 8 | +const eslintConfigPath = path.join(rootDir, 'eslint.config.mjs') |
| 9 | + |
| 10 | +// Get list of staged files |
| 11 | +function getStagedFiles(): string[] { |
| 12 | + try { |
| 13 | + const output = execSync( |
| 14 | + 'git diff --cached --name-only --diff-filter=ACMR', |
| 15 | + { encoding: 'utf8' } |
| 16 | + ) |
| 17 | + return output |
| 18 | + .trim() |
| 19 | + .split('\n') |
| 20 | + .filter(file => file.length > 0) |
| 21 | + } catch (error) { |
| 22 | + console.error('Error getting staged files:', error) |
| 23 | + return [] |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Remove staged files from the warnings list |
| 28 | +function removeStagedFilesFromWarningsList(): void { |
| 29 | + const stagedFiles = getStagedFiles() |
| 30 | + if (stagedFiles.length === 0) { |
| 31 | + console.log('No staged files to process') |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + console.log('Processing staged files:', stagedFiles.length) |
| 36 | + |
| 37 | + // Read the config file |
| 38 | + let configContent = fs.readFileSync(eslintConfigPath, 'utf8') |
| 39 | + |
| 40 | + // Track what was removed |
| 41 | + let removedCount = 0 |
| 42 | + const removedFiles: string[] = [] |
| 43 | + |
| 44 | + // Process each staged file |
| 45 | + for (const stagedFile of stagedFiles) { |
| 46 | + // Create regex to match the file in the array with surrounding quotes and possible comma |
| 47 | + // This handles files with quotes like 'file.ts' or "file.ts" |
| 48 | + const patterns = [ |
| 49 | + new RegExp( |
| 50 | + `^(\\s*)(['"])${stagedFile.replace( |
| 51 | + /[.*+?^${}()|[\]\\]/g, |
| 52 | + '\\$&' |
| 53 | + )}\\2,?\\s*$`, |
| 54 | + 'gm' |
| 55 | + ), |
| 56 | + new RegExp( |
| 57 | + `^(\\s*)(['"])${stagedFile.replace( |
| 58 | + /[.*+?^${}()|[\]\\]/g, |
| 59 | + '\\$&' |
| 60 | + )}\\2,?\\s*\n`, |
| 61 | + 'gm' |
| 62 | + ) |
| 63 | + ] |
| 64 | + |
| 65 | + for (const pattern of patterns) { |
| 66 | + const matches = configContent.match(pattern) |
| 67 | + if (matches != null) { |
| 68 | + configContent = configContent.replace(pattern, '') |
| 69 | + removedFiles.push(stagedFile) |
| 70 | + removedCount++ |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (removedCount > 0) { |
| 77 | + // Clean up any double newlines that might have been created |
| 78 | + configContent = configContent.replace(/\n\s*\n\s*\n/g, '\n\n') |
| 79 | + |
| 80 | + // Fix any trailing commas before the closing bracket |
| 81 | + configContent = configContent.replace(/,(\s*\])/g, '$1') |
| 82 | + |
| 83 | + // Write the updated config |
| 84 | + fs.writeFileSync(eslintConfigPath, configContent, 'utf8') |
| 85 | + |
| 86 | + // Stage the updated ESLint config |
| 87 | + try { |
| 88 | + execSync(`git add ${eslintConfigPath}`) |
| 89 | + console.log( |
| 90 | + `\nRemoved ${removedCount} file(s) from ESLint warnings list:` |
| 91 | + ) |
| 92 | + removedFiles.forEach(file => { |
| 93 | + console.log(` - ${file}`) |
| 94 | + }) |
| 95 | + console.log('\nUpdated eslint.config.mjs has been staged') |
| 96 | + } catch (error) { |
| 97 | + console.error('Error staging ESLint config:', error) |
| 98 | + process.exit(1) |
| 99 | + } |
| 100 | + } else { |
| 101 | + console.log('No staged files found in the ESLint warnings list') |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Main execution |
| 106 | +try { |
| 107 | + removeStagedFilesFromWarningsList() |
| 108 | +} catch (error) { |
| 109 | + console.error('Error:', error) |
| 110 | + process.exit(1) |
| 111 | +} |
0 commit comments