Files
cloudron-box/src/updatechecker.js

103 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2020-08-19 21:39:58 -07:00
checkForUpdates,
2020-08-19 21:39:58 -07:00
getUpdateInfo,
2017-07-23 10:53:16 -07:00
2020-08-19 22:40:04 -07:00
_checkAppUpdates: checkAppUpdates
};
const apps = require('./apps.js'),
appstore = require('./appstore.js'),
2019-03-07 14:27:23 -08:00
assert = require('assert'),
debug = require('debug')('box:updatechecker'),
2019-03-07 13:34:46 -08:00
notifications = require('./notifications.js'),
paths = require('./paths.js'),
2021-08-20 09:19:44 -07:00
safe = require('safetydance');
function setUpdateInfo(state) {
// appid -> update info { creationDate, manifest }
// box -> { version, changelog, upgrade, sourceTarballUrl }
2020-12-21 12:39:21 -08:00
state.version = 2;
2021-06-03 12:20:44 -07:00
if (!safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(state, null, 4), 'utf8')) debug(`setUpdateInfo: Error writing to update checker file: ${safe.error.message}`);
}
function getUpdateInfo() {
const state = safe.JSON.parse(safe.fs.readFileSync(paths.UPDATE_CHECKER_FILE, 'utf8'));
2020-12-21 12:39:21 -08:00
if (!state || state.version !== 2) return {};
delete state.version;
return state;
2016-01-25 16:46:54 -08:00
}
2021-08-18 15:54:53 -07:00
async function checkAppUpdates(options) {
assert.strictEqual(typeof options, 'object');
2016-01-23 05:35:57 -08:00
2021-03-03 13:49:22 -08:00
debug('checkAppUpdates: checking for updates');
2021-09-21 19:53:05 -07:00
const state = getUpdateInfo();
const newState = { }; // create new state so that old app ids are removed
2021-08-20 09:19:44 -07:00
const result = await apps.list();
2021-08-18 15:54:53 -07:00
for (const app of result) {
if (app.appStoreId === '') continue; // appStoreId can be '' for dev apps
2017-06-19 22:17:43 -07:00
2021-08-18 15:54:53 -07:00
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
}
2021-08-18 15:54:53 -07:00
if (!updateInfo) continue; // skip if no next version is found
newState[app.id] = updateInfo;
}
2021-08-18 15:54:53 -07:00
if ('box' in state) newState.box = state.box; // preserve the latest box state information
setUpdateInfo(newState);
}
2021-08-18 15:54:53 -07:00
async function checkBoxUpdates(options) {
assert.strictEqual(typeof options, 'object');
2021-03-03 13:49:22 -08:00
debug('checkBoxUpdates: checking for updates');
2021-08-18 15:54:53 -07:00
const updateInfo = await appstore.getBoxUpdate(options);
2021-09-21 19:53:05 -07:00
const state = getUpdateInfo();
2021-08-18 15:54:53 -07:00
if (!updateInfo) { // no update
if ('box' in state) {
delete state.box;
setUpdateInfo(state);
2017-10-25 20:52:05 -07:00
}
2021-08-18 15:54:53 -07:00
debug('checkBoxUpdates: no updates');
return;
}
2021-08-18 15:54:53 -07:00
if (state.box && state.box.version === updateInfo.version) {
debug(`checkBoxUpdates: Skipping notification of box update ${updateInfo.version} as user was already notified`);
return;
}
2021-03-03 13:49:22 -08:00
2021-08-18 15:54:53 -07:00
debug(`checkBoxUpdates: ${updateInfo.version} is available`);
2021-08-18 15:54:53 -07:00
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
const message = `Changelog:\n${changelog}\n\nGo to the settings view to update.\n\n`;
2021-08-18 15:54:53 -07:00
await notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message);
2021-08-18 15:54:53 -07:00
state.box = updateInfo;
setUpdateInfo(state);
}
2020-08-19 21:39:58 -07:00
2021-08-18 15:54:53 -07:00
async function checkForUpdates(options) {
2020-08-19 21:39:58 -07:00
assert.strictEqual(typeof options, 'object');
2021-08-18 15:54:53 -07:00
const [boxError] = await safe(checkBoxUpdates(options));
if (boxError) debug('checkForUpdates: error checking for box updates:', boxError);
2020-08-19 21:39:58 -07:00
2021-08-18 15:54:53 -07:00
const [appError] = await safe(checkAppUpdates(options));
if (appError) debug('checkForUpdates: error checking for app updates:', appError);
2020-08-19 21:39:58 -07:00
}