|
const { exec } = require('child_process'); |
|
const path = require('path'); |
|
const cron = require('node-cron'); |
|
|
|
const WATCH_DIR = path.join(__dirname, '.data'); |
|
|
|
function gitStatusHasChanges(callback) { |
|
exec('git status --porcelain', { cwd: WATCH_DIR }, (err, stdout, stderr) => { |
|
if (err) { |
|
console.error('Error running git status:', err); |
|
callback(false); |
|
return; |
|
} |
|
callback(stdout.trim().length > 0); |
|
}); |
|
} |
|
|
|
function gitCommitAndPush() { |
|
const now = new Date(); |
|
const timeString = now.toISOString(); |
|
|
|
exec('git add .', { cwd: WATCH_DIR }, (err, stdout, stderr) => { |
|
if (err) { |
|
console.error('Error running git add:', err); |
|
return; |
|
} |
|
exec(`git commit -m "Database Sync - ${timeString}"`, { cwd: WATCH_DIR }, (err2, stdout2, stderr2) => { |
|
if (err2) { |
|
if (stderr2.includes('nothing to commit')) { |
|
console.log('No changes to commit.'); |
|
} else { |
|
console.error('Error running git commit:', err2); |
|
} |
|
return; |
|
} |
|
exec('git push', { cwd: WATCH_DIR }, (err3, stdout3, stderr3) => { |
|
if (err3) { |
|
console.error('Error running git push:', err3); |
|
return; |
|
} |
|
console.log('Changes pushed successfully at', timeString); |
|
}); |
|
}); |
|
}); |
|
} |
|
|
|
function checkForChanges() { |
|
gitStatusHasChanges((hasChanges) => { |
|
if (hasChanges) { |
|
console.log('Changes detected, committing and pushing...'); |
|
gitCommitAndPush(); |
|
} else { |
|
console.log('No changes detected.'); |
|
} |
|
}); |
|
} |
|
|
|
function startWatching() { |
|
console.log(`Starting to watch directory: ${WATCH_DIR}`); |
|
|
|
cron.schedule('* * * * *', () => { |
|
checkForChanges(); |
|
}); |
|
} |
|
|
|
startWatching(); |
|
|