appstore and support: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-08-18 15:54:53 -07:00
parent 200018a022
commit 03e22170da
16 changed files with 458 additions and 633 deletions

View File

@@ -11,11 +11,11 @@ exports = module.exports = {
const 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');
safe = require('safetydance'),
util = require('util');
function setUpdateInfo(state) {
// appid -> update info { creationDate, manifest }
@@ -31,95 +31,75 @@ function getUpdateInfo() {
return state;
}
function checkAppUpdates(options, callback) {
async function checkAppUpdates(options) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
debug('checkAppUpdates: checking for updates');
let state = getUpdateInfo();
let newState = { }; // create new state so that old app ids are removed
apps.getAll(function (error, result) {
if (error) return callback(error);
const appsGetAllAsync = util.promisify(apps.getAll);
async.eachSeries(result, function (app, iteratorDone) {
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
const result = await appsGetAllAsync();
appstore.getAppUpdate(app, options, function (error, updateInfo) {
if (error) {
debug('checkAppUpdates: Error getting app update info for %s', app.id, error);
return iteratorDone(); // continue to next
}
for (const app of result) {
if (app.appStoreId === '') continue; // appStoreId can be '' for dev apps
if (!updateInfo) return iteratorDone(); // skip if no next version is found
const [error, updateInfo] = await safe(appstore.getAppUpdate(app, options));
if (error) {
debug('checkAppUpdates: Error getting app update info for %s', app.id, error);
continue; // continue to next
}
newState[app.id] = updateInfo;
if (!updateInfo) continue; // skip if no next version is found
newState[app.id] = updateInfo;
}
iteratorDone();
});
}, function () {
if ('box' in state) newState.box = state.box; // preserve the latest box state information
setUpdateInfo(newState);
callback();
});
});
if ('box' in state) newState.box = state.box; // preserve the latest box state information
setUpdateInfo(newState);
}
function checkBoxUpdates(options, callback) {
async function checkBoxUpdates(options) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
debug('checkBoxUpdates: checking for updates');
appstore.getBoxUpdate(options, async function (error, updateInfo) {
if (error) return callback(error);
const updateInfo = await appstore.getBoxUpdate(options);
let state = getUpdateInfo();
let state = getUpdateInfo();
if (!updateInfo) { // no update
if ('box' in state) {
delete state.box;
setUpdateInfo(state);
}
debug('checkBoxUpdates: no updates');
return callback(null);
if (!updateInfo) { // no update
if ('box' in state) {
delete state.box;
setUpdateInfo(state);
}
debug('checkBoxUpdates: no updates');
return;
}
if (state.box && state.box.version === updateInfo.version) {
debug(`checkBoxUpdates: Skipping notification of box update ${updateInfo.version} as user was already notified`);
return callback(null);
}
if (state.box && state.box.version === updateInfo.version) {
debug(`checkBoxUpdates: Skipping notification of box update ${updateInfo.version} as user was already notified`);
return;
}
debug(`checkBoxUpdates: ${updateInfo.version} is available`);
debug(`checkBoxUpdates: ${updateInfo.version} is available`);
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
await notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message);
[error] = await safe(notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message));
if (error) return callback(error);
state.box = updateInfo;
setUpdateInfo(state);
callback(null);
});
state.box = updateInfo;
setUpdateInfo(state);
}
function checkForUpdates(options, callback) {
async function checkForUpdates(options) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
checkBoxUpdates(options, function (boxError) {
if (boxError) debug('checkForUpdates: error checking for box updates:', boxError);
const [boxError] = await safe(checkBoxUpdates(options));
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);
});
});
const [appError] = await safe(checkAppUpdates(options));
if (appError) debug('checkForUpdates: error checking for app updates:', appError);
}