Skip to content

Instantly share code, notes, and snippets.

@thisjt
Last active June 9, 2024 19:20
Show Gist options
  • Save thisjt/aea6de8461aba5f9d769feffe01b01c8 to your computer and use it in GitHub Desktop.
Save thisjt/aea6de8461aba5f9d769feffe01b01c8 to your computer and use it in GitHub Desktop.
Vite Plugin - Git Unstaged Files Checker
/*
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