0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Refactored update check to use async/await syntax

refs https://github.com/TryGhost/Team/issues/726

- These are minimal changes that I've done while reviewing the code inside the update-check module. There's more to come, only picked up the low-hanging fruit!
This commit is contained in:
Naz 2021-05-26 16:06:06 +04:00
parent 88564751f9
commit 1b26692430

View file

@ -226,15 +226,20 @@ function updateCheckRequest() {
* @param {Object} response
* @return {Promise}
*/
function updateCheckResponse(response) {
async function updateCheckResponse(response) {
let notifications = [];
let notificationGroups = (config.get('notificationGroups') || []).concat(['all']);
debug('Notification Groups', notificationGroups);
debug('Response Update Check Service', response);
return api.settings.edit({settings: [{key: 'next_update_check', value: response.next_check}]}, internal)
.then(function () {
await api.settings.edit({
settings: [{
key: 'next_update_check',
value: response.next_check
}]
}, internal);
/**
* @NOTE:
*
@ -273,7 +278,6 @@ function updateCheckResponse(response) {
}
return Promise.each(notifications, createCustomNotification);
});
}
/**
@ -284,27 +288,29 @@ function updateCheckResponse(response) {
*
* @returns {Promise}
*/
function updateCheck() {
async function updateCheck() {
// CASE: The check will not happen if your NODE_ENV is not in the allowed defined environments.
if (_.indexOf(allowedCheckEnvironments, process.env.NODE_ENV) === -1) {
return Promise.resolve();
return;
}
return api.settings.read(_.extend({key: 'next_update_check'}, internal))
.then(function then(result) {
const result = await api.settings.read(_.extend({key: 'next_update_check'}, internal));
const nextUpdateCheck = result.settings[0];
// CASE: Next update check should happen now?
// @NOTE: You can skip this check by adding a config value. This is helpful for developing.
if (!config.get('updateCheck:forceUpdate') && nextUpdateCheck && nextUpdateCheck.value && nextUpdateCheck.value > moment().unix()) {
return Promise.resolve();
return;
}
return updateCheckRequest()
.then(updateCheckResponse)
.catch(updateCheckError);
})
.catch(updateCheckError);
try {
const response = await updateCheckRequest();
await updateCheckResponse(response);
} catch (err) {
updateCheckError(err);
}
}
module.exports = updateCheck;