0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Updated UpdateCheckService methods to async/await

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

- In additions to easier tracking of "this" context in the unit tests it gets rid of unnecessary Bluebird's "reflect" method which was making unit test dependent on Bluebird's specific Promise implementation
This commit is contained in:
Naz 2021-05-31 20:57:30 +04:00
parent 63c7d5aad1
commit 3173603d96

View file

@ -64,7 +64,7 @@ class UpdateCheckService {
* @description Collect stats from your blog.
* @returns {Promise}
*/
updateCheckData() {
async updateCheckData() {
let data = {};
let mailConfig = this.config.get('mail');
@ -77,18 +77,13 @@ class UpdateCheckService {
mailConfig.options.service :
mailConfig.transport);
return Promise.props({
hash: this.api.settings.read(_.extend({key: 'db_hash'}, internal)).reflect(),
theme: this.api.settings.read(_.extend({key: 'active_theme'}, internal)).reflect(),
posts: this.api.posts.browse().reflect(),
users: this.api.users.browse(internal).reflect(),
npm: Promise.promisify(exec)('npm -v').reflect()
}).then(function (descriptors) {
const hash = descriptors.hash.value().settings[0];
const theme = descriptors.theme.value().settings[0];
const posts = descriptors.posts.value();
const users = descriptors.users.value();
const npm = descriptors.npm.value();
try {
const hash = (await this.api.settings.read(_.extend({key: 'db_hash'}, internal))).settings[0];
const theme = (await this.api.settings.read(_.extend({key: 'active_theme'}, internal))).settings[0];
const posts = await this.api.posts.browse();
const users = await this.api.users.browse(internal);
const npm = await Promise.promisify(exec)('npm -v');
const blogUrl = this.urlUtils.urlFor('home', true);
const parsedBlogUrl = url.parse(blogUrl);
const blogId = parsedBlogUrl.hostname + parsedBlogUrl.pathname.replace(/\//, '') + hash.value;
@ -102,7 +97,9 @@ class UpdateCheckService {
data.npm_version = npm.trim();
return data;
}).catch(this.updateCheckError);
} catch (err) {
this.updateCheckError(err);
}
}
/**
@ -115,8 +112,8 @@ class UpdateCheckService {
* @returns {Promise}
*/
async updateCheckRequest() {
return this.updateCheckData()
.then(function then(reqData) {
const reqData = await this.updateCheckData();
let reqObj = {
timeout: 1000,
headers: {}
@ -140,11 +137,10 @@ class UpdateCheckService {
debug('Request Update Check Service', checkEndpoint);
return this.request(checkEndpoint, reqObj)
.then(function (response) {
try {
const response = await this.request(checkEndpoint, reqObj);
return response.body;
})
.catch(function (err) {
} catch (err) {
// CASE: no notifications available, ignore
if (err.statusCode === 404) {
return {
@ -155,12 +151,11 @@ class UpdateCheckService {
// CASE: service returns JSON error, deserialize into JS error
if (err.response && err.response.body && typeof err.response.body === 'object') {
err = errors.utils.deserialize(err.response.body);
}
throw errors.utils.deserialize(err.response.body);
} else {
throw err;
});
});
}
}
}
/**
@ -331,7 +326,7 @@ class UpdateCheckService {
try {
const response = await this.updateCheckRequest();
await this.updateCheckResponse(response);
return await this.updateCheckResponse(response);
} catch (err) {
this.updateCheckError(err);
}