Issues (2242)

application/services/updater.js (8 issues)

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) {
0 ignored issues
show
Comparing online to true using the == operator is not safe. Consider using === instead.
Loading history...
35
      if (lostConnection == true) {
0 ignored issues
show
Comparing lostConnection to true using the == operator is not safe. Consider using === instead.
Loading history...
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 =
0 ignored issues
show
The variable exec_str seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.exec_str.
Loading history...
71
      " git reset --hard origin/" + branch + " && git pull origin " + branch;
72
    if (commands !== null) {
73
      commands.forEach(function(object, index) {
0 ignored issues
show
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
74
        log.info("running " + object);
75
        exec_str = exec_str + " && " + object;
0 ignored issues
show
The variable exec_str seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.exec_str.
Loading history...
76
      });
77
    }
78
    shell_exec(path, exec_str, true, branch);
79
  } else {
80
    if (stop == true) {
0 ignored issues
show
Comparing stop to true using the == operator is not safe. Consider using === instead.
Loading history...
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) {
0 ignored issues
show
The parameter stderr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
90
    if (typeof error != null) {
0 ignored issues
show
Comparing typeof error to null using the != operator is not safe. Consider using !== instead.
Loading history...
91
      work_on_response(stdout, path, stop, branch);
92
    }
93
  });
94
}
95