ES6 migration: server/lib/common (#9779)

refs #9589
This commit is contained in:
Anonymous 2018-09-10 13:39:50 +01:00 committed by Katharina Irrgang
parent 03af23cddf
commit 8f568bc556
4 changed files with 40 additions and 38 deletions

View File

@ -1,4 +1,5 @@
var _ = require('lodash'),
const merge = require('lodash/merge'),
each = require('lodash/each'),
util = require('util'),
errors = require('ghost-ignition').errors;
@ -9,59 +10,59 @@ function GhostError(options) {
errors.IgnitionError.call(this, options);
}
var ghostErrors = {
const ghostErrors = {
DataExportError: function DataExportError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 500,
errorType: 'DataExportError'
}, options));
},
DataImportError: function DataImportError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 500,
errorType: 'DataImportError'
}, options));
},
DatabaseVersionError: function DatabaseVersionError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
hideStack: true,
statusCode: 500,
errorType: 'DatabaseVersionError'
}, options));
},
DatabaseNotPopulatedError: function DatabaseNotPopulatedError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 500,
errorType: 'DatabaseNotPopulatedError'
}, options));
},
DatabaseNotSeededError: function DatabaseNotSeededError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 500,
errorType: 'DatabaseNotSeededError'
}, options));
},
EmailError: function EmailError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 500,
errorType: 'EmailError'
}, options));
},
ThemeValidationError: function ThemeValidationError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 422,
errorType: 'ThemeValidationError',
errorDetails: {}
}, options));
},
DisabledFeatureError: function DisabledFeatureError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 409,
errorType: 'DisabledFeatureError'
}, options));
},
UpdateCollisionError: function UpdateCollisionError(options) {
GhostError.call(this, _.merge({
GhostError.call(this, merge({
statusCode: 409,
errorType: 'UpdateCollisionError'
}, options));
@ -69,12 +70,12 @@ var ghostErrors = {
};
util.inherits(GhostError, errors.IgnitionError);
_.each(ghostErrors, function (error) {
each(ghostErrors, function (error) {
util.inherits(error, GhostError);
});
// we need to inherit all general errors from GhostError, otherwise we have to check instanceof IgnitionError
_.each(errors, function (error) {
each(errors, function (error) {
if (error.name === 'IgnitionError' || typeof error === 'object') {
return;
}
@ -82,5 +83,5 @@ _.each(errors, function (error) {
util.inherits(error, GhostError);
});
module.exports = _.merge(ghostErrors, errors);
module.exports = merge(ghostErrors, errors);
module.exports.GhostError = GhostError;

View File

@ -1,6 +1,6 @@
var events = require('events'),
util = require('util'),
EventRegistry,
const events = require('events'),
util = require('util');
let EventRegistry,
EventRegistryInstance;
EventRegistry = function () {
@ -10,10 +10,8 @@ EventRegistry = function () {
util.inherits(EventRegistry, events.EventEmitter);
EventRegistry.prototype.onMany = function (arr, onEvent) {
var self = this;
arr.forEach(function (eventName) {
self.on(eventName, onEvent);
arr.forEach((eventName) => {
this.on(eventName, onEvent);
});
};

View File

@ -1,18 +1,21 @@
/* global Intl */
var supportedLocales = ['en'],
const supportedLocales = ['en'],
chalk = require('chalk'),
fs = require('fs-extra'),
MessageFormat = require('intl-messageformat'),
jp = require('jsonpath'),
_ = require('lodash'),
isString = require('lodash/isString'),
isObject = require('lodash/isObject'),
isEqual = require('lodash/isEqual'),
merge = require('lodash/merge'),
path = require('path'),
config = require('../../config'),
errors = require('./errors'),
events = require('./events'),
logging = require('./logging'),
settingsCache = require('../../services/settings/cache'),
_private = {},
_private = {};
// currentLocale, dynamically based on overall settings (key = "default_locale") in the settings db table
// (during Ghost's initialization, settings available inside i18n functions below; see core/server/index.js)
@ -23,7 +26,7 @@ var supportedLocales = ['en'],
// https://www.w3.org/International/articles/language-tags/
//
// The corresponding translation files should be at content/themes/mytheme/locales/es.json, etc.
currentLocale,
let currentLocale,
activeTheme,
coreStrings,
themeStrings,
@ -56,7 +59,7 @@ I18n = {
* @returns {string}
*/
t: function t(path, bindings) {
var string, isTheme, msg;
let string, isTheme, msg;
currentLocale = I18n.locale();
if (bindings !== undefined) {
@ -68,10 +71,10 @@ I18n = {
// If the path returns an array (as in the case with anything that has multiple paragraphs such as emails), then
// loop through them and return an array of translated/formatted strings. Otherwise, just return the normal
// translated/formatted string.
if (_.isArray(string)) {
if (Array.isArray(string)) {
msg = [];
string.forEach(function (s) {
var m = new MessageFormat(s, currentLocale);
let m = new MessageFormat(s, currentLocale);
try {
m.format(bindings);
@ -109,11 +112,11 @@ I18n = {
* @returns {string}
*/
findString: function findString(msgPath, opts) {
var options = _.merge({log: true}, opts || {}),
candidateString, matchingString, path;
const options = merge({log: true}, opts || {});
let candidateString, matchingString, path;
// no path? no string
if (_.isEmpty(msgPath) || !_.isString(msgPath)) {
if (msgPath.length === 0 || !isString(msgPath)) {
chalk.yellow('i18n.t() - received an empty path.');
return '';
}
@ -141,13 +144,13 @@ I18n = {
// While bracket-notation allows any Unicode characters in keys for themes,
// dot-notation allows only word characters in keys for backend messages
// (that is \w or [A-Za-z0-9_] in RegExp)
path = '$.' + msgPath;
path = `$.${msgPath}`;
candidateString = jp.value(coreStrings, path);
}
matchingString = candidateString || {};
if (_.isObject(matchingString) || _.isEqual(matchingString, {})) {
if (isObject(matchingString) || isEqual(matchingString, {})) {
if (options.log) {
logging.error(new errors.IncorrectUsageError({
message: `i18n error: path "${msgPath}" was not found`
@ -161,7 +164,7 @@ I18n = {
},
doesTranslationKeyExist: function doesTranslationKeyExist(msgPath) {
var translation = I18n.findString(msgPath, {log: false});
const translation = I18n.findString(msgPath, {log: false});
return translation !== coreStrings.errors.errors.anErrorOccurred;
},
@ -207,7 +210,7 @@ I18n = {
} catch (err) {
themeStrings = undefined;
if (err.code === 'ENOENT') {
logging.warn('Theme\'s file locales/' + currentLocale + '.json not found.');
logging.warn(`Theme\'s file locales/${currentLocale}.json not found.`);
} else {
throw err;
}
@ -258,7 +261,7 @@ I18n = {
* - Polyfill node.js if it does not have Intl support or support for a particular locale
*/
_private.initializeIntl = function initializeIntl() {
var hasBuiltInLocaleData, IntlPolyfill;
let hasBuiltInLocaleData, IntlPolyfill;
if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.

View File

@ -1,5 +1,5 @@
var config = require('../../config'),
logging = require('ghost-ignition').logging;
const config = require('../../config'),
{logging} = require('ghost-ignition');
module.exports = logging({
env: config.get('env'),