0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/services/apps/loader.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

const path = require('path');
const _ = require('lodash');
const Promise = require('bluebird');
const common = require('../../lib/common');
const config = require('../../config');
const Proxy = require('./proxy');
// 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));
2014-01-21 15:45:27 +07:00
}
function getAppByName(name) {
2014-01-21 15:45:27 +07:00
// Grab the app class to instantiate
const AppClass = loadApp(name);
const proxy = Proxy.getInstance();
2014-01-21 15:45:27 +07:00
// Check for an actual class, otherwise just use whatever was returned
const app = _.isFunction(AppClass) ? new AppClass(proxy) : AppClass;
2014-01-21 15:45:27 +07:00
return {
app,
proxy
};
2014-01-21 15:45:27 +07:00
}
module.exports = {
2014-01-21 15:45:27 +07:00
// Activate a app and return it
activateAppByName: function (name) {
const {app, proxy} = getAppByName(name);
2014-01-21 15:45:27 +07:00
// Check for an activate() method on the app.
if (!_.isFunction(app.activate)) {
return Promise.reject(new Error(common.i18n.t('errors.apps.noActivateMethodLoadingApp.error', {name: name})));
}
2014-01-21 15:45:27 +07:00
// 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);
2014-01-21 15:45:27 +07:00
}
};