eienmojiki commited on
Commit
fabe61e
·
verified ·
1 Parent(s): 9174be1

Update sync-notes.js

Browse files
Files changed (1) hide show
  1. sync-notes.js +55 -15
sync-notes.js CHANGED
@@ -1,13 +1,25 @@
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, 'notes');
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
  }
@@ -15,52 +27,80 @@ function gitStatusHasChanges(callback) {
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();
 
1
+ const { execSync, exec } = require('child_process');
2
+ const fs = require('fs');
3
  const path = require('path');
4
  const cron = require('node-cron');
5
 
6
  const WATCH_DIR = path.join(__dirname, 'notes');
7
 
8
+ function gitPull() {
9
+ try {
10
+ console.log('Đang thực hiện git pull...');
11
+ execSync('git pull', { cwd: WATCH_DIR, stdio: 'inherit' });
12
+ console.log('Git pull hoàn tất.');
13
+ } catch (error) {
14
+ console.error('Lỗi khi thực hiện git pull:', error.message);
15
+ // Có thể xử lý lỗi ở đây, ví dụ: cố gắng merge hoặc reset
16
+ }
17
+ }
18
+
19
  function gitStatusHasChanges(callback) {
20
  exec('git status --porcelain', { cwd: WATCH_DIR }, (err, stdout, stderr) => {
21
  if (err) {
22
+ console.error('Lỗi khi chạy git status:', err);
23
  callback(false);
24
  return;
25
  }
 
27
  });
28
  }
29
 
30
+ function gitCommitAndPush(callback) {
31
  const now = new Date();
32
  const timeString = now.toISOString();
33
 
34
  exec('git add .', { cwd: WATCH_DIR }, (err, stdout, stderr) => {
35
  if (err) {
36
+ console.error('Lỗi khi chạy git add:', err);
37
+ callback(false);
38
  return;
39
  }
40
  exec(`git commit -m "Database Sync - ${timeString}"`, { cwd: WATCH_DIR }, (err2, stdout2, stderr2) => {
41
  if (err2) {
42
  if (stderr2.includes('nothing to commit')) {
43
+ console.log('Không thay đổi để commit.');
44
+ callback(true); // Coi như thành công vì không có gì để commit
45
  } else {
46
+ console.error('Lỗi khi chạy git commit:', err2);
47
+ callback(false);
48
  }
49
  return;
50
  }
51
  exec('git push', { cwd: WATCH_DIR }, (err3, stdout3, stderr3) => {
52
  if (err3) {
53
+ console.error('Lỗi khi chạy git push:', err3);
54
+ callback(false);
55
  return;
56
  }
57
+ console.log('Thay đổi đã được đẩy thành công vào lúc', timeString);
58
+ callback(true);
59
  });
60
  });
61
  });
62
  }
63
 
64
+ function initialSync() {
65
+ console.log('Đang thực hiện đồng bộ hóa ban đầu...');
66
  gitStatusHasChanges((hasChanges) => {
67
  if (hasChanges) {
68
+ console.log('Phát hiện thay đổi cục bộ, đang commit và đẩy...');
69
+ gitCommitAndPush((success) => {
70
+ if (success) {
71
+ gitPull();
72
+ } else {
73
+ console.error('Không thể đẩy thay đổi cục bộ. Bỏ qua git pull.');
74
+ }
75
+ });
76
  } else {
77
+ console.log('Không thay đổi cục bộ. Đang thực hiện git pull.');
78
+ gitPull();
79
  }
80
  });
81
  }
82
 
83
  function startWatching() {
84
+ console.log(`Bắt đầu theo dõi thư mục: ${WATCH_DIR}`);
85
+ // Lên lịch chạy mỗi phút
86
  cron.schedule('* * * * *', () => {
87
+ gitStatusHasChanges((hasChanges) => {
88
+ if (hasChanges) {
89
+ console.log('Phát hiện thay đổi, đang commit và đẩy...');
90
+ gitCommitAndPush((success) => {
91
+ if (success) {
92
+ gitPull();
93
+ }
94
+ });
95
+ } else {
96
+ console.log('Không phát hiện thay đổi.');
97
+ gitPull(); // Vẫn pull định kỳ để đảm bảo cập nhật từ xa
98
+ }
99
+ });
100
  });
101
  }
102
 
103
+ // Thực hiện đồng bộ hóa ban đầu một lần khi script khởi động
104
+ initialSync();
105
+ // Sau đó bắt đầu theo dõi định kỳ
106
  startWatching();