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