telebit.js/lib/updater.js

142 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-06-14 11:24:28 +00:00
'use strict';
module.exports = function (pkg) {
function checkUpgrade() {
var https = require('https');
function getFile(url, cb) {
https.get(url, function (resp) {
var str = '';
resp.on('data', function (chunk) {
//var chunk = conn.read();
2018-06-14 11:24:28 +00:00
str += chunk.toString('utf8');
});
resp.on('end', function () {
cb(null, str);
});
resp.on('error', function (err) {
// ignore
cb(err);
});
}).on('error', function (err) {
// ignore
cb(err);
});
}
function isNewer(latest, myPkg) {
2018-06-14 11:35:39 +00:00
//console.log('sort result:', sortLatest(latest, myPkg));
return sortLatest(latest, myPkg) < 0;
}
function sortLatest(latest, myPkg) {
var m = /^(v)?(\d+)\.(\d+)\.(\d+)(.*)/.exec(latest);
var n = /^(v)?(\d+)\.(\d+)\.(\d+)(.*)/.exec(myPkg);
//console.log('m', m);
//console.log('n', n);
2018-06-14 11:24:28 +00:00
if (!m) {
2018-06-14 11:35:39 +00:00
if (!n) {
return 0;
}
2018-06-14 11:24:28 +00:00
return 1;
2018-06-14 11:35:39 +00:00
} else if (!n) {
return -1;
2018-06-14 11:24:28 +00:00
}
2018-06-14 11:35:39 +00:00
2018-06-14 11:24:28 +00:00
if (parseInt(m[2], 10) > parseInt(n[2], 10)) {
return -1;
} else if (parseInt(m[2], 10) === parseInt(n[2], 10)) {
if (parseInt(m[3], 10) > parseInt(n[3], 10)) {
return -1;
} else if (parseInt(m[3], 10) === parseInt(n[3], 10)) {
if (parseInt(m[4], 10) > parseInt(n[4], 10)) {
return -1;
} else if (parseInt(m[4], 10) === parseInt(n[4], 10)) {
// lex sorting
if (m[5] > n[5]) {
return -1;
} else if (m[5] === n[5]) {
return 0;
} else {
return 1;
}
} else {
return 1;
}
} else {
return 1;
}
} else {
return 1;
}
}
getFile("https://telebit.cloud/dist/index.tab", function (err, tab) {
if (err) { /*ignore*/ return; }
if (tab) { tab = tab && tab.toString() || ''; }
var versions = [];
var lines = tab.split(/[\r\n]/g);
var headers = lines.shift().split(/\t/g);
var chan = 'prod';
var next;
lines.forEach(function (line) {
var tsv = {};
var fields = line.split(/\t/g);
fields.forEach(function (value, i) {
tsv[headers[i]] = value;
});
versions.push(tsv);
});
// find matching version
versions.some(function (v) {
if (('v' + pkg.version) === v.version) {
chan = v.channel;
return true;
}
});
// find first (most recent) version in channel
versions.some(function (v) {
if (chan === v.channel) {
next = v;
return true;
}
});
if (!next || !isNewer(next.version, pkg.version)) {
2018-06-14 11:35:39 +00:00
//console.log('DEBUG can\'t upgrade from', pkg.version, 'in channel', chan);
2018-06-14 11:24:28 +00:00
return;
}
console.log('Upgrade Available: ' + next.version + ' in \'' + next.channel + '\'channel');
getFile("https://telebit.cloud/dist/upgrade.js", function (err, script) {
if (err) { /*ignore*/ return; }
var os = require('os');
var fs = require('fs');
var path = require('path');
var scriptname = 'telebit-upgrade-' + Math.round(Math.random() * 99999) + '.js';
var pathname = path.join(os.tmpdir(), scriptname);
fs.writeFile(pathname, script, function (err) {
if (err) { /*ignore*/ return; }
// console.log('DEBUG wrote', pathname);
//var str =
require(pathname)({
package: pkg
, root: path.resolve(__dirname, '..')
, latest: next
, channel: chan
}, function () {
// console.log('upgrade complete');
});
//console.log(str);
});
});
});
}
2018-06-29 02:35:58 +00:00
var _interval = setInterval(checkUpgrade, 2 * 60 * 60 * 1000);
2018-06-14 11:24:28 +00:00
process.nextTick(function () {
checkUpgrade();
});
2018-06-29 02:35:58 +00:00
return function cancel() {
clearInterval(_interval);
};
2018-06-14 11:24:28 +00:00
};