-
Notifications
You must be signed in to change notification settings - Fork 7
/
versionChecker.js
72 lines (41 loc) · 1.93 KB
/
versionChecker.js
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
68
69
70
71
72
/****************************************************************************
* versionChecker.js
* openacousticdevices.info
* November 2020
*****************************************************************************/
'use strict';
/* global XMLHttpRequest */
const {app} = require('@electron/remote');
const semver = require('semver');
const pjson = require('./package.json');
/* Check current app version in package.json against latest version in repository's releases */
exports.checkLatestRelease = (callback) => {
/* Check for internet connection */
if (!navigator.onLine) {
callback({updateNeeded: false, error: 'No internet connection, failed to request app version information.'});
return;
}
const version = app.getVersion();
/* Transform repository URL into release API URL */
const repoGitURL = pjson.repository.url;
let repoURL = repoGitURL.replace('.git', '/releases');
repoURL = repoURL.replace('github.com', 'api.github.com/repos');
const xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', repoURL, true);
xmlHttp.onload = () => {
if (xmlHttp.status === 200) {
const responseJson = JSON.parse(xmlHttp.responseText);
const latestVersion = responseJson[0].tag_name;
console.log('Comparing latest release (' + latestVersion + ') with currently installed version (' + version + ')');
/* Compare current version in package.json to latest version pulled from Github */
const updateNeeded = semver.lt(version, latestVersion);
callback({updateNeeded, latestVersion: updateNeeded ? latestVersion : version});
}
};
xmlHttp.onerror = () => {
console.error('Failed to pull release information.');
callback({updateNeeded: false, error: 'HTTP connection error, failed to request app version information.'});
};
/* Send request */
xmlHttp.send(null);
};