mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Refactored tpl to never be used in DI
- Ghost has a set of core packages that it is safe to require directly in any file - tpl is one of them! - This keeps the DI signature smaller and easier to reason about
This commit is contained in:
parent
1530cb28a5
commit
0bdaa216e5
9 changed files with 31 additions and 39 deletions
|
@ -1,8 +1,8 @@
|
|||
const config = require('../../../shared/config');
|
||||
const externalRequest = require('../../lib/request-external');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
|
||||
const OEmbed = require('../../services/oembed');
|
||||
const oembed = new OEmbed({config, externalRequest, tpl});
|
||||
const oembed = new OEmbed({config, externalRequest});
|
||||
|
||||
module.exports = {
|
||||
docName: 'oembed',
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
const config = require('../../../shared/config');
|
||||
const externalRequest = require('../../lib/request-external');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
|
||||
const OEmbed = require('../../services/oembed');
|
||||
const oembed = new OEmbed({config, externalRequest, tpl});
|
||||
const oembed = new OEmbed({config, externalRequest});
|
||||
|
||||
module.exports = {
|
||||
docName: 'oembed',
|
||||
|
|
|
@ -3,15 +3,15 @@ const Promise = require('bluebird');
|
|||
const _ = require('lodash');
|
||||
const path = require('path');
|
||||
const errors = require('@tryghost/errors');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
|
||||
const messages = {
|
||||
error: 'Could not fetch icon dimensions.'
|
||||
};
|
||||
|
||||
class BlogIcon {
|
||||
constructor({config, tpl, urlUtils, settingsCache, storageUtils}) {
|
||||
constructor({config, urlUtils, settingsCache, storageUtils}) {
|
||||
this.config = config;
|
||||
this.tpl = tpl;
|
||||
this.urlUtils = urlUtils;
|
||||
this.settingsCache = settingsCache;
|
||||
this.storageUtils = storageUtils;
|
||||
|
@ -27,10 +27,10 @@ class BlogIcon {
|
|||
getIconDimensions(storagePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let dimensions;
|
||||
|
||||
|
||||
try {
|
||||
dimensions = sizeOf(storagePath);
|
||||
|
||||
|
||||
if (dimensions.images) {
|
||||
dimensions.width = _.maxBy(dimensions.images, function (w) {
|
||||
return w.width;
|
||||
|
@ -39,14 +39,14 @@ class BlogIcon {
|
|||
return h.height;
|
||||
}).height;
|
||||
}
|
||||
|
||||
|
||||
return resolve({
|
||||
width: dimensions.width,
|
||||
height: dimensions.height
|
||||
});
|
||||
} catch (err) {
|
||||
return reject(new errors.ValidationError({
|
||||
message: this.tpl(messages.error, {
|
||||
message: tpl(messages.error, {
|
||||
file: storagePath,
|
||||
error: err.message
|
||||
})
|
||||
|
@ -64,7 +64,7 @@ class BlogIcon {
|
|||
*/
|
||||
isIcoImageType(icon) {
|
||||
const blogIcon = icon || this.settingsCache.get('icon');
|
||||
|
||||
|
||||
return blogIcon.match(/.ico$/i) ? true : false;
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,7 @@ class BlogIcon {
|
|||
*/
|
||||
getIconType(icon) {
|
||||
const blogIcon = icon || this.settingsCache.get('icon');
|
||||
|
||||
|
||||
return this.isIcoImageType(blogIcon) ? 'x-icon' : 'png';
|
||||
}
|
||||
|
||||
|
@ -90,7 +90,7 @@ class BlogIcon {
|
|||
*/
|
||||
getIconUrl(absolut) {
|
||||
const blogIcon = this.settingsCache.get('icon');
|
||||
|
||||
|
||||
if (absolut) {
|
||||
if (blogIcon) {
|
||||
return this.isIcoImageType(blogIcon) ? this.urlUtils.urlFor({relativeUrl: '/favicon.ico'}, true) : this.urlUtils.urlFor({relativeUrl: '/favicon.png'}, true);
|
||||
|
@ -115,7 +115,7 @@ class BlogIcon {
|
|||
*/
|
||||
getIconPath() {
|
||||
const blogIcon = this.settingsCache.get('icon');
|
||||
|
||||
|
||||
if (blogIcon) {
|
||||
return this.storageUtils.getLocalFileStoragePath(blogIcon);
|
||||
} else {
|
||||
|
|
|
@ -6,6 +6,7 @@ const path = require('path');
|
|||
const Promise = require('bluebird');
|
||||
const _ = require('lodash');
|
||||
const errors = require('@tryghost/errors');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
|
||||
const messages = {
|
||||
invalidDimensions: 'Could not fetch image dimensions.'
|
||||
|
@ -17,9 +18,8 @@ const FETCH_ONLY_FORMATS = [
|
|||
];
|
||||
|
||||
class ImageSize {
|
||||
constructor({config, tpl, storage, storageUtils, validator, urlUtils, request}) {
|
||||
constructor({config, storage, storageUtils, validator, urlUtils, request}) {
|
||||
this.config = config;
|
||||
this.tpl = tpl;
|
||||
this.storage = storage;
|
||||
this.storageUtils = storageUtils;
|
||||
this.validator = validator;
|
||||
|
@ -320,7 +320,7 @@ class ImageSize {
|
|||
});
|
||||
} catch (err) {
|
||||
return reject(new errors.ValidationError({
|
||||
message: this.tpl(messages.invalidDimensions, {
|
||||
message: tpl(messages.invalidDimensions, {
|
||||
file: imagePath,
|
||||
error: err.message
|
||||
})
|
||||
|
|
|
@ -4,12 +4,12 @@ const Gravatar = require('./gravatar');
|
|||
const ImageSize = require('./image-size');
|
||||
|
||||
class ImageUtils {
|
||||
constructor({config, logging, tpl, urlUtils, settingsCache, storageUtils, storage, validator, request}) {
|
||||
this.blogIcon = new BlogIcon({config, tpl, urlUtils, settingsCache, storageUtils});
|
||||
this.imageSize = new ImageSize({config, tpl, storage, storageUtils, validator, urlUtils, request});
|
||||
constructor({config, logging, urlUtils, settingsCache, storageUtils, storage, validator, request}) {
|
||||
this.blogIcon = new BlogIcon({config, urlUtils, settingsCache, storageUtils});
|
||||
this.imageSize = new ImageSize({config, storage, storageUtils, validator, urlUtils, request});
|
||||
this.cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging, imageSize: this.imageSize});
|
||||
this.gravatar = new Gravatar({config, request});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ImageUtils;
|
||||
module.exports = ImageUtils;
|
||||
|
|
|
@ -5,8 +5,7 @@ const storageUtils = require('../../adapters/storage/utils');
|
|||
const validator = require('@tryghost/validator');
|
||||
const config = require('../../../shared/config');
|
||||
const logging = require('@tryghost/logging');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
const settingsCache = require('../../../shared/settings-cache');
|
||||
const ImageUtils = require('./image-utils');
|
||||
|
||||
module.exports = new ImageUtils({config, logging, tpl, urlUtils, settingsCache, storageUtils, storage, validator, request});
|
||||
module.exports = new ImageUtils({config, logging, urlUtils, settingsCache, storageUtils, storage, validator, request});
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const settingsCache = require('../../../shared/settings-cache');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
const mailService = require('../../services/mail');
|
||||
const logging = require('@tryghost/logging');
|
||||
const urlUtils = require('../../../shared/url-utils');
|
||||
|
@ -7,7 +6,6 @@ const Invites = require('./invites');
|
|||
|
||||
module.exports = new Invites({
|
||||
settingsCache,
|
||||
tpl,
|
||||
logging,
|
||||
mailService,
|
||||
urlUtils
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const security = require('@tryghost/security');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
|
||||
const messages = {
|
||||
invitedByName: '{invitedByName} has invited you to join {blogName}',
|
||||
|
@ -9,9 +10,8 @@ const messages = {
|
|||
};
|
||||
|
||||
class Invites {
|
||||
constructor({settingsCache, tpl, logging, mailService, urlUtils}) {
|
||||
constructor({settingsCache, logging, mailService, urlUtils}) {
|
||||
this.settingsCache = settingsCache;
|
||||
this.tpl = tpl;
|
||||
this.logging = logging;
|
||||
this.mailService = mailService;
|
||||
this.urlUtils = urlUtils;
|
||||
|
@ -52,7 +52,7 @@ class Invites {
|
|||
mail: [{
|
||||
message: {
|
||||
to: invite.get('email'),
|
||||
subject: this.tpl(messages.invitedByName, {
|
||||
subject: tpl(messages.invitedByName, {
|
||||
invitedByName: emailData.invitedByName,
|
||||
blogName: emailData.blogName
|
||||
}),
|
||||
|
@ -75,10 +75,10 @@ class Invites {
|
|||
})
|
||||
.catch((err) => {
|
||||
if (err && err.errorType === 'EmailError') {
|
||||
const errorMessage = this.tpl(messages.errorSendingEmail.error, {
|
||||
const errorMessage = tpl(messages.errorSendingEmail.error, {
|
||||
message: err.message
|
||||
});
|
||||
const helpText = this.tpl(messages.errorSendingEmail.help);
|
||||
const helpText = tpl(messages.errorSendingEmail.help);
|
||||
err.message = `${errorMessage} ${helpText}`;
|
||||
this.logging.warn(err.message);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const Promise = require('bluebird');
|
||||
const errors = require('@tryghost/errors');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
const {extract, hasProvider} = require('oembed-parser');
|
||||
const cheerio = require('cheerio');
|
||||
const _ = require('lodash');
|
||||
|
@ -34,10 +35,6 @@ const findUrlWithProvider = (url) => {
|
|||
return {url, provider};
|
||||
};
|
||||
|
||||
/**
|
||||
* @typedef {(string: string) => string} ITpl
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} IConfig
|
||||
* @prop {(key: string) => string} get
|
||||
|
@ -51,19 +48,17 @@ class OEmbed {
|
|||
/**
|
||||
*
|
||||
* @param {Object} dependencies
|
||||
* @param {ITpl} dependencies.tpl
|
||||
* @param {IConfig} dependencies.config
|
||||
* @param {IExternalRequest} dependencies.externalRequest
|
||||
*/
|
||||
constructor({config, externalRequest, tpl}) {
|
||||
constructor({config, externalRequest}) {
|
||||
this.config = config;
|
||||
this.externalRequest = externalRequest;
|
||||
this.tpl = tpl;
|
||||
}
|
||||
|
||||
unknownProvider(url) {
|
||||
return Promise.reject(new errors.ValidationError({
|
||||
message: this.tpl(messages.unknownProvider),
|
||||
message: tpl(messages.unknownProvider),
|
||||
context: url
|
||||
}));
|
||||
}
|
||||
|
@ -133,7 +128,7 @@ class OEmbed {
|
|||
}
|
||||
|
||||
return Promise.reject(new errors.ValidationError({
|
||||
message: this.tpl(messages.insufficientMetadata),
|
||||
message: tpl(messages.insufficientMetadata),
|
||||
context: url
|
||||
}));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue