125 lines
4.1 KiB
JavaScript
125 lines
4.1 KiB
JavaScript
'use strict';
|
|
|
|
exports = module.exports = {
|
|
checkForUpdates,
|
|
|
|
getUpdateInfo,
|
|
|
|
_checkAppUpdates: checkAppUpdates
|
|
};
|
|
|
|
var apps = require('./apps.js'),
|
|
appstore = require('./appstore.js'),
|
|
assert = require('assert'),
|
|
async = require('async'),
|
|
debug = require('debug')('box:updatechecker'),
|
|
notifications = require('./notifications.js'),
|
|
paths = require('./paths.js'),
|
|
safe = require('safetydance');
|
|
|
|
function setUpdateInfo(state) {
|
|
// appid -> update info { creationDate, manifest }
|
|
// box -> { version, changelog, upgrade, sourceTarballUrl }
|
|
state.version = 2;
|
|
safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(state, null, 4), 'utf8');
|
|
}
|
|
|
|
function getUpdateInfo() {
|
|
const state = safe.JSON.parse(safe.fs.readFileSync(paths.UPDATE_CHECKER_FILE, 'utf8'));
|
|
if (!state || state.version !== 2) return {};
|
|
delete state.version;
|
|
return state;
|
|
}
|
|
|
|
function checkAppUpdates(options, callback) {
|
|
assert.strictEqual(typeof options, 'object');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
debug('Checking App Updates');
|
|
|
|
let state = getUpdateInfo();
|
|
let newState = { }; // create new state so that old app ids are removed
|
|
|
|
var pendingNotifications = [];
|
|
|
|
apps.getAll(function (error, result) {
|
|
if (error) return callback(error);
|
|
|
|
async.eachSeries(result, function (app, iteratorDone) {
|
|
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
|
|
|
|
appstore.getAppUpdate(app, options, function (error, updateInfo) {
|
|
if (error) {
|
|
debug('Error getting app update info for %s', app.id, error);
|
|
return iteratorDone(); // continue to next
|
|
}
|
|
|
|
if (!updateInfo) return iteratorDone(); // skip if no next version is found
|
|
|
|
newState[app.id] = updateInfo;
|
|
|
|
if (safe.query(state[app.id], 'manifest.version') === updateInfo.manifest.version) {
|
|
debug(`Skipping app update notification of ${app.id} since user was already notified of ${updateInfo.manifest.version}`);
|
|
return iteratorDone();
|
|
}
|
|
|
|
pendingNotifications.push({ app, updateInfo });
|
|
iteratorDone();
|
|
});
|
|
}, function () {
|
|
if ('box' in state) newState.box = state.box; // preserve the latest box state information
|
|
|
|
setUpdateInfo(newState);
|
|
|
|
notifications.appUpdatesAvailable(pendingNotifications, callback);
|
|
});
|
|
});
|
|
}
|
|
|
|
function checkBoxUpdates(options, callback) {
|
|
assert.strictEqual(typeof options, 'object');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
debug('Checking Box Updates');
|
|
|
|
appstore.getBoxUpdate(options, function (error, updateInfo) {
|
|
if (error || !updateInfo) return callback(error);
|
|
|
|
let state = getUpdateInfo();
|
|
|
|
if (state.box && state.box.version === updateInfo.version) {
|
|
debug('Skipping notification of box update as user was already notified');
|
|
return callback();
|
|
}
|
|
|
|
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
|
|
|
|
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
|
|
|
|
notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message, function (error) {
|
|
if (error) return callback(error);
|
|
|
|
state.box = updateInfo;
|
|
setUpdateInfo(state);
|
|
|
|
notifications.boxUpdateAvailable(updateInfo, callback);
|
|
});
|
|
});
|
|
}
|
|
|
|
function checkForUpdates(options, callback) {
|
|
assert.strictEqual(typeof options, 'object');
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
checkBoxUpdates(options, function (boxError) {
|
|
if (boxError) debug('checkForUpdates: error checking for box updates:', boxError);
|
|
|
|
checkAppUpdates(options, function (appError) {
|
|
if (appError) debug('checkForUpdates: error checking for app updates:', appError);
|
|
|
|
callback(boxError || appError || null);
|
|
});
|
|
});
|
|
|
|
}
|