Ghost/core/frontend/services/apps/loader.js
Hannah Wolfe ac07703f17
Changed app/loader to use @tryghost/errors
- getting rid of instances of new Error as we should always use @tryghost/errors
- Whilst here, got rid of i18n but discovered the messages were missing!
- This is my fault, they disappeared when I removed external apps and clearly removed too much: 8c1a0b8d0c (diff-0f5cc40aa8906a1be1bad2002a35361bbf9e766e46b3b29be10f4f479265426a)
- Therefore, I have restored these messages in the places where they were used, except amp_content, where I have written a new message, as the message that was there was not relevant
2021-06-30 16:05:54 +01:00

53 lines
1.5 KiB
JavaScript

const path = require('path');
const _ = require('lodash');
const Promise = require('bluebird');
const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const config = require('../../../shared/config');
const Proxy = require('./proxy');
const messages = {
noActivateMethodLoadingAppError: 'Error loading app named {name}; no activate() method defined.'
};
// Get the full path to an app by name
function getAppAbsolutePath(name) {
return path.join(config.get('paths').internalAppPath, name);
}
function loadApp(name) {
return require(getAppAbsolutePath(name));
}
function getAppByName(name) {
// Grab the app class to instantiate
const AppClass = loadApp(name);
const proxy = Proxy.getInstance();
// Check for an actual class, otherwise just use whatever was returned
const app = _.isFunction(AppClass) ? new AppClass(proxy) : AppClass;
return {
app,
proxy
};
}
module.exports = {
// Activate a app and return it
activateAppByName: function (name) {
const {app, proxy} = getAppByName(name);
// Check for an activate() method on the app.
if (!_.isFunction(app.activate)) {
return Promise.reject(new errors.IncorrectUsageError(
tpl(messages.noActivateMethodLoadingAppError, {name: name})
));
}
// Wrapping the activate() with a when because it's possible
// to not return a promise from it.
return Promise.resolve(app.activate(proxy)).return(app);
}
};