File size: 1,765 Bytes
72c5a4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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}`);
  // Schedule to run every minute
  cron.schedule('* * * * *', () => {
    checkForChanges();
  });
}

startWatching();