72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
#!/usr/bin/env bun
|
|
|
|
const chokidar = require('chokidar');
|
|
const { execSync } = require('child_process');
|
|
const path = process.cwd();
|
|
let lastActivity = Date.now();
|
|
const TIMEOUT = 600000; // 10 minutes
|
|
const AMEND_COMMITS = true; // Amend auto-commits
|
|
let isProcessing = false; // Prevent recursive calls
|
|
|
|
// Check if in a Git repo
|
|
try {
|
|
execSync('git rev-parse --is-inside-work-tree', { cwd: path, stdio: 'ignore' });
|
|
console.log(`Starting auto-commit in ${path}`);
|
|
} catch (e) {
|
|
console.error('Error: Not a Git repository. Please run in a Git repo.');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Function to check and commit changes
|
|
function commitChanges() {
|
|
if (isProcessing) {
|
|
console.log('Already processing, skipping...');
|
|
return;
|
|
}
|
|
isProcessing = true;
|
|
|
|
try {
|
|
const status = execSync('git status --porcelain', { cwd: path }).toString();
|
|
if (status.trim()) {
|
|
console.log('Changes detected:', status.trim());
|
|
execSync('git add .', { cwd: path });
|
|
const lastCommit = execSync('git log -1 --oneline', { cwd: path }).toString();
|
|
if (AMEND_COMMITS && lastCommit.includes('Auto-commit')) {
|
|
execSync('git commit --amend --no-edit', { cwd: path });
|
|
console.log('Amended previous auto-commit.');
|
|
} else {
|
|
execSync(`git commit -m "Auto-commit: ${new Date().toISOString()}"`, { cwd: path });
|
|
console.log('Created new commit.');
|
|
}
|
|
} else {
|
|
console.log('No changes to commit.');
|
|
}
|
|
lastActivity = Date.now();
|
|
} catch (e) {
|
|
console.error(`Commit error: ${e.message}`);
|
|
} finally {
|
|
isProcessing = false;
|
|
}
|
|
}
|
|
|
|
// Watch files, ignoring .git
|
|
chokidar.watch(path, {
|
|
ignoreInitial: false,
|
|
ignored: [/.git\//, /.git$/], // Exclude .git directory and files
|
|
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 100 }
|
|
})
|
|
.on('all', (event, filePath) => {
|
|
console.log(`Event: ${event} on ${filePath}`);
|
|
commitChanges();
|
|
})
|
|
.on('error', (error) => {
|
|
console.error(`Watcher error: ${error.message}`);
|
|
});
|
|
|
|
// Timeout check
|
|
setInterval(() => {
|
|
if (Date.now() - lastActivity >= TIMEOUT) {
|
|
console.log('No activity for 10 minutes, exiting.');
|
|
process.exit(0);
|
|
}
|
|
}, 1000);
|