Ghost/core/server/services/apps/loader.js

106 lines
3.5 KiB
JavaScript
Raw Normal View History

const path = require('path');
const _ = require('lodash');
const Promise = require('bluebird');
const config = require('../../config');
const common = require('../../lib/common');
const AppProxy = require('./proxy');
const AppSandbox = require('./sandbox');
const AppDependencies = require('./dependencies');
const AppPermissions = require('./permissions');
// Get the full path to an app by name
function getAppAbsolutePath(name) {
return path.join(config.get('paths').internalAppPath, name);
}
2014-01-21 12:45:27 +04:00
// Get a relative path to the given apps root, defaults
// to be relative to __dirname
function getAppRelativePath(name, relativeTo = __dirname) {
const relativePath = path.relative(relativeTo, getAppAbsolutePath(name));
if (relativePath.charAt(0) !== '.') {
return './' + relativePath;
}
return relativePath;
2014-01-21 12:45:27 +04:00
}
2015-03-15 00:50:00 +03:00
// Load apps through a pseudo sandbox
function loadApp(appPath) {
const sandbox = new AppSandbox();
return sandbox.loadApp(appPath);
}
2014-01-21 12:45:27 +04:00
function getAppByName(name, permissions) {
2014-01-21 12:45:27 +04:00
// Grab the app class to instantiate
const AppClass = loadApp(getAppRelativePath(name));
const proxy = new AppProxy({
name,
permissions,
internal: true
});
2014-01-21 12:45:27 +04:00
// Check for an actual class, otherwise just use whatever was returned
const app = _.isFunction(AppClass) ? new AppClass(proxy) : AppClass;
2014-01-21 12:45:27 +04:00
return {
app,
proxy
};
2014-01-21 12:45:27 +04:00
}
module.exports = {
2014-01-21 12:45:27 +04:00
// Load a app and return the instantiated app
installAppByName: function (name) {
2015-03-15 00:50:00 +03:00
// Install the apps dependencies first
const appPath = getAppAbsolutePath(name);
const deps = new AppDependencies(appPath);
return deps.install()
.then(function () {
// Load app permissions
const perms = new AppPermissions(appPath);
return perms.read().catch(function (err) {
// Provide a helpful error about which app
return Promise.reject(new Error(common.i18n.t('errors.apps.permissionsErrorLoadingApp.error', {
name: name,
message: err.message
})));
});
})
.then(function (appPerms) {
const {app, proxy} = getAppByName(name, appPerms);
// Check for an install() method on the app.
if (!_.isFunction(app.install)) {
return Promise.reject(new Error(common.i18n.t('errors.apps.noInstallMethodLoadingApp.error', {name: name})));
}
// Run the app.install() method
// Wrapping the install() with a when because it's possible
// to not return a promise from it.
return Promise.resolve(app.install(proxy)).return(app);
});
2014-01-21 12:45:27 +04:00
},
// Activate a app and return it
activateAppByName: function (name) {
const perms = new AppPermissions(getAppAbsolutePath(name));
2014-01-21 12:45:27 +04:00
return perms.read().then(function (appPerms) {
const {app, proxy} = getAppByName(name, appPerms);
2014-01-21 12:45:27 +04: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})));
}
// 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 12:45:27 +04:00
});
}
};