Last active
September 30, 2023 02:20
-
-
Save ThomasMcDonald/6b9e5a6d0ccceaab6b2d9f0fd65973f6 to your computer and use it in GitHub Desktop.
Run eslint on staged Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { execSync } = require('child_process'); | |
const path = require('path'); | |
/** | |
* This script will run eslint only on staged files. | |
* Currently is being run as a pre-commit hook | |
* @usage | |
* npm run fix-staged | |
*/ | |
try { | |
const stagedFiles = execSync('git diff --name-only --cached') | |
.toString() | |
.split('\n'); | |
const filteredFiles = stagedFiles.filter((filePath) => filePath !== path.basename(filePath) && filePath.endsWith('.js')); | |
if (filteredFiles.length) { | |
execSync(`.\\node_modules\\.bin\\eslint --fix ${filteredFiles.join(' ')}`, { stdio: 'inherit' }); | |
} | |
} catch (err) { | |
// the eslint execSync will return an error code 1 | |
// if it finds linting issues | |
// exit with error code 1 to make commit fail | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment