Last active
June 9, 2024 19:20
-
-
Save thisjt/aea6de8461aba5f9d769feffe01b01c8 to your computer and use it in GitHub Desktop.
Vite Plugin - Git Unstaged Files Checker
This file contains hidden or 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
/* | |
I've always hated being a bit too in the zone during coding | |
only to know that I've written too many lines of code before a commit. | |
This plugin makes sure that I'm always in check of around how | |
many lines of code I've made/modified and commit them when needed. | |
*/ | |
import { defineConfig } from 'vite'; | |
import { exec } from 'child_process'; | |
export default defineConfig({ | |
plugins: [ | |
{ | |
name: 'git-unstaged-files-checker', | |
handleHotUpdate() { | |
exec('git --no-pager diff', (err, stdout) => { | |
if (!stdout) return; | |
const diffString = stdout.split('\n'); | |
let addedLines = 0; | |
let removedLines = 0; | |
diffString.forEach((line) => { | |
if (line.charAt(0) === '+' && line.charAt(1) !== '+') { | |
addedLines++; | |
} | |
if (line.charAt(0) === '-' && line.charAt(1) !== '-') { | |
removedLines++; | |
} | |
}); | |
const totalLinesChanged = addedLines > removedLines ? addedLines : removedLines; | |
if (totalLinesChanged < 50) { | |
console.log(`All good to go. You have less than 50 [${totalLinesChanged}] lines of unstaged changes.`); | |
} else { | |
console.log(`⛔⛔⛔ You have ${totalLinesChanged} lines of unstaged changes. I think it's time to commit! ⛔⛔⛔`); | |
} | |
}); | |
}, | |
}, | |
], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment