Spaces:
Running
Running
Create sync-notes.js
Browse files- sync-notes.js +66 -0
sync-notes.js
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const { exec } = require('child_process');
|
2 |
+
const path = require('path');
|
3 |
+
const cron = require('node-cron');
|
4 |
+
|
5 |
+
const WATCH_DIR = path.join(__dirname, '.data');
|
6 |
+
|
7 |
+
function gitStatusHasChanges(callback) {
|
8 |
+
exec('git status --porcelain', { cwd: WATCH_DIR }, (err, stdout, stderr) => {
|
9 |
+
if (err) {
|
10 |
+
console.error('Error running git status:', err);
|
11 |
+
callback(false);
|
12 |
+
return;
|
13 |
+
}
|
14 |
+
callback(stdout.trim().length > 0);
|
15 |
+
});
|
16 |
+
}
|
17 |
+
|
18 |
+
function gitCommitAndPush() {
|
19 |
+
const now = new Date();
|
20 |
+
const timeString = now.toISOString();
|
21 |
+
|
22 |
+
exec('git add .', { cwd: WATCH_DIR }, (err, stdout, stderr) => {
|
23 |
+
if (err) {
|
24 |
+
console.error('Error running git add:', err);
|
25 |
+
return;
|
26 |
+
}
|
27 |
+
exec(`git commit -m "Database Sync - ${timeString}"`, { cwd: WATCH_DIR }, (err2, stdout2, stderr2) => {
|
28 |
+
if (err2) {
|
29 |
+
if (stderr2.includes('nothing to commit')) {
|
30 |
+
console.log('No changes to commit.');
|
31 |
+
} else {
|
32 |
+
console.error('Error running git commit:', err2);
|
33 |
+
}
|
34 |
+
return;
|
35 |
+
}
|
36 |
+
exec('git push', { cwd: WATCH_DIR }, (err3, stdout3, stderr3) => {
|
37 |
+
if (err3) {
|
38 |
+
console.error('Error running git push:', err3);
|
39 |
+
return;
|
40 |
+
}
|
41 |
+
console.log('Changes pushed successfully at', timeString);
|
42 |
+
});
|
43 |
+
});
|
44 |
+
});
|
45 |
+
}
|
46 |
+
|
47 |
+
function checkForChanges() {
|
48 |
+
gitStatusHasChanges((hasChanges) => {
|
49 |
+
if (hasChanges) {
|
50 |
+
console.log('Changes detected, committing and pushing...');
|
51 |
+
gitCommitAndPush();
|
52 |
+
} else {
|
53 |
+
console.log('No changes detected.');
|
54 |
+
}
|
55 |
+
});
|
56 |
+
}
|
57 |
+
|
58 |
+
function startWatching() {
|
59 |
+
console.log(`Starting to watch directory: ${WATCH_DIR}`);
|
60 |
+
// Schedule to run every minute
|
61 |
+
cron.schedule('* * * * *', () => {
|
62 |
+
checkForChanges();
|
63 |
+
});
|
64 |
+
}
|
65 |
+
|
66 |
+
startWatching();
|