1
|
|
|
module.exports = Updater; |
2
|
|
|
var exec = require("child_process").exec; |
3
|
|
|
var log = require("captains-log")(); |
4
|
|
|
const isOnline = require("is-online"); |
5
|
|
|
var lostConnection = false; |
6
|
|
|
const notifier = require("node-notifier"); |
7
|
|
|
const pathModule = require("path"); |
8
|
|
|
|
9
|
|
|
var commands = null; |
10
|
|
|
|
11
|
|
|
function Updater() {} |
12
|
|
|
|
13
|
|
|
Updater.prototype.verify_git = function(path, branch, command) { |
14
|
|
|
commands = command; |
15
|
|
|
|
16
|
|
|
check_connection(path, branch); |
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
function send_notification(notification_message) { |
20
|
|
|
notifier.notify({ |
21
|
|
|
title: "Devmind Vigilance", |
22
|
|
|
message: notification_message, |
23
|
|
|
icon: pathModule.join( |
24
|
|
|
pathModule.resolve(__dirname, "..") + "/includes", |
25
|
|
|
"devmind.png" |
26
|
|
|
), // Absolute path (doesn't work on balloons) |
27
|
|
|
sound: true, // Only Notification Center or Windows Toasters |
28
|
|
|
wait: false |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function check_connection(path, branch) { |
33
|
|
|
isOnline().then(online => { |
34
|
|
|
if (online == true) { |
|
|
|
|
35
|
|
|
if (lostConnection == true) { |
|
|
|
|
36
|
|
|
log.info("Connection restablished."); |
37
|
|
|
send_notification("Vigilance Connection restablished."); |
38
|
|
|
lostConnection = false; |
39
|
|
|
} |
40
|
|
|
timed_check(path, branch); |
41
|
|
|
} else { |
42
|
|
|
setTimeout(function() { |
43
|
|
|
log.info("Connection lost. Trying again in 3 seconds."); |
44
|
|
|
send_notification( |
45
|
|
|
"Vigilance connection lost. Trying again in 3 seconds." |
46
|
|
|
); |
47
|
|
|
lostConnection = true; |
48
|
|
|
check_connection(path, branch); |
49
|
|
|
}, 3000); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function timed_check(path, branch) { |
55
|
|
|
shell_exec( |
56
|
|
|
path, |
57
|
|
|
"git fetch && git diff " + |
58
|
|
|
branch + |
59
|
|
|
" origin/" + |
60
|
|
|
branch + |
61
|
|
|
' --quiet || echo "untracked"', |
62
|
|
|
false, |
63
|
|
|
branch |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function work_on_response(response, path, stop, branch) { |
68
|
|
|
if (response.trim().includes("untracked")) { |
69
|
|
|
log.info("updating local files.."); |
70
|
|
|
exec_str = |
|
|
|
|
71
|
|
|
" git reset --hard origin/" + branch + " && git pull origin " + branch; |
72
|
|
|
if (commands !== null) { |
73
|
|
|
commands.forEach(function(object, index) { |
|
|
|
|
74
|
|
|
log.info("running " + object); |
75
|
|
|
exec_str = exec_str + " && " + object; |
|
|
|
|
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
shell_exec(path, exec_str, true, branch); |
79
|
|
|
} else { |
80
|
|
|
if (stop == true) { |
|
|
|
|
81
|
|
|
log.info("Update successful"); |
82
|
|
|
send_notification("Vigilance Update successful."); |
83
|
|
|
} |
84
|
|
|
check_connection(path, branch); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function shell_exec(path, command, stop, branch) { |
89
|
|
|
exec(path + command, function(error, stdout, stderr) { |
|
|
|
|
90
|
|
|
if (typeof error != null) { |
|
|
|
|
91
|
|
|
work_on_response(stdout, path, stop, branch); |
92
|
|
|
} |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|