2019-04-15 12:57:52 +03:00
|
|
|
const path = require('path');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const config = require('../../config');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const AppProxy = require('./proxy');
|
2019-04-15 13:13:50 +03:00
|
|
|
const Sandbox = require('./sandbox');
|
2019-04-15 12:57:52 +03:00
|
|
|
const AppPermissions = require('./permissions');
|
2016-04-11 16:58:41 +03:00
|
|
|
|
2014-02-02 10:07:39 +04:00
|
|
|
// Get the full path to an app by name
|
|
|
|
function getAppAbsolutePath(name) {
|
2019-04-15 12:57:52 +03:00
|
|
|
return path.join(config.get('paths').internalAppPath, name);
|
2014-02-02 10:07:39 +04:00
|
|
|
}
|
|
|
|
|
2014-01-21 12:45:27 +04:00
|
|
|
// Get a relative path to the given apps root, defaults
|
|
|
|
// to be relative to __dirname
|
2019-04-15 12:57:52 +03:00
|
|
|
function getAppRelativePath(name, relativeTo = __dirname) {
|
|
|
|
const relativePath = path.relative(relativeTo, getAppAbsolutePath(name));
|
2016-04-11 16:58:41 +03:00
|
|
|
|
|
|
|
if (relativePath.charAt(0) !== '.') {
|
2019-04-15 12:57:52 +03:00
|
|
|
return './' + relativePath;
|
2016-04-11 16:58:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return relativePath;
|
2014-01-21 12:45:27 +04:00
|
|
|
}
|
|
|
|
|
2015-03-15 00:50:00 +03:00
|
|
|
// Load apps through a pseudo sandbox
|
2019-04-15 12:57:52 +03:00
|
|
|
function loadApp(appPath) {
|
2019-04-15 13:13:50 +03:00
|
|
|
return Sandbox.loadApp(appPath);
|
2014-02-01 02:53:27 +04:00
|
|
|
}
|
2014-01-21 12:45:27 +04:00
|
|
|
|
2014-04-22 04:22:13 +04:00
|
|
|
function getAppByName(name, permissions) {
|
2014-01-21 12:45:27 +04:00
|
|
|
// Grab the app class to instantiate
|
2019-04-15 12:57:52 +03:00
|
|
|
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
|
2019-04-15 12:57:52 +03:00
|
|
|
const app = _.isFunction(AppClass) ? new AppClass(proxy) : AppClass;
|
2014-01-21 12:45:27 +04:00
|
|
|
|
2014-04-22 04:22:13 +04:00
|
|
|
return {
|
2019-04-15 12:57:52 +03:00
|
|
|
app,
|
|
|
|
proxy
|
2014-04-22 04:22:13 +04:00
|
|
|
};
|
2014-01-21 12:45:27 +04:00
|
|
|
}
|
|
|
|
|
2019-04-15 12:57:52 +03:00
|
|
|
module.exports = {
|
2014-01-21 12:45:27 +04:00
|
|
|
// Activate a app and return it
|
|
|
|
activateAppByName: function (name) {
|
2019-04-15 12:57:52 +03:00
|
|
|
const perms = new AppPermissions(getAppAbsolutePath(name));
|
2014-01-21 12:45:27 +04:00
|
|
|
|
2014-02-12 05:04:13 +04:00
|
|
|
return perms.read().then(function (appPerms) {
|
2019-04-15 12:57:52 +03:00
|
|
|
const {app, proxy} = getAppByName(name, appPerms);
|
2014-01-21 12:45:27 +04:00
|
|
|
|
2014-02-12 05:04:13 +04:00
|
|
|
// Check for an activate() method on the app.
|
|
|
|
if (!_.isFunction(app.activate)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new Error(common.i18n.t('errors.apps.noActivateMethodLoadingApp.error', {name: name})));
|
2014-02-12 05:04:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapping the activate() with a when because it's possible
|
|
|
|
// to not return a promise from it.
|
2019-04-15 12:57:52 +03:00
|
|
|
return Promise.resolve(app.activate(proxy)).return(app);
|
2014-01-21 12:45:27 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|