2019-04-15 11:57:52 +02:00
|
|
|
const path = require('path');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const config = require('../../config');
|
|
|
|
const common = require('../../lib/common');
|
2019-04-15 12:42:31 +02:00
|
|
|
const Proxy = require('./proxy');
|
2019-04-15 12:13:50 +02:00
|
|
|
const Sandbox = require('./sandbox');
|
2016-04-11 08:58:41 -05:00
|
|
|
|
2014-02-02 00:07:39 -06:00
|
|
|
// Get the full path to an app by name
|
|
|
|
function getAppAbsolutePath(name) {
|
2019-04-15 11:57:52 +02:00
|
|
|
return path.join(config.get('paths').internalAppPath, name);
|
2014-02-02 00:07:39 -06:00
|
|
|
}
|
|
|
|
|
2014-01-21 15:45:27 +07:00
|
|
|
// Get a relative path to the given apps root, defaults
|
|
|
|
// to be relative to __dirname
|
2019-04-15 11:57:52 +02:00
|
|
|
function getAppRelativePath(name, relativeTo = __dirname) {
|
|
|
|
const relativePath = path.relative(relativeTo, getAppAbsolutePath(name));
|
2016-04-11 08:58:41 -05:00
|
|
|
|
|
|
|
if (relativePath.charAt(0) !== '.') {
|
2019-04-15 11:57:52 +02:00
|
|
|
return './' + relativePath;
|
2016-04-11 08:58:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return relativePath;
|
2014-01-21 15:45:27 +07:00
|
|
|
}
|
|
|
|
|
2019-04-15 12:26:51 +02:00
|
|
|
function getAppByName(name) {
|
2014-01-21 15:45:27 +07:00
|
|
|
// Grab the app class to instantiate
|
2019-04-15 12:42:31 +02:00
|
|
|
const AppClass = Sandbox.loadApp(getAppRelativePath(name));
|
|
|
|
const proxy = Proxy.getInstance(name);
|
2014-01-21 15:45:27 +07:00
|
|
|
|
|
|
|
// Check for an actual class, otherwise just use whatever was returned
|
2019-04-15 11:57:52 +02:00
|
|
|
const app = _.isFunction(AppClass) ? new AppClass(proxy) : AppClass;
|
2014-01-21 15:45:27 +07:00
|
|
|
|
2014-04-21 19:22:13 -05:00
|
|
|
return {
|
2019-04-15 11:57:52 +02:00
|
|
|
app,
|
|
|
|
proxy
|
2014-04-21 19:22:13 -05:00
|
|
|
};
|
2014-01-21 15:45:27 +07:00
|
|
|
}
|
|
|
|
|
2019-04-15 11:57:52 +02:00
|
|
|
module.exports = {
|
2014-01-21 15:45:27 +07:00
|
|
|
// Activate a app and return it
|
|
|
|
activateAppByName: function (name) {
|
2019-04-15 12:26:51 +02:00
|
|
|
const {app, proxy} = getAppByName(name);
|
2014-01-21 15:45:27 +07:00
|
|
|
|
2019-04-15 12:26:51 +02: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
|
|
|
|
2019-04-15 12:26:51 +02: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
|
|
|
}
|
|
|
|
};
|