2019-04-15 11:57:52 +02:00
|
|
|
const path = require('path');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const common = require('../../lib/common');
|
2019-04-15 12:54:29 +02:00
|
|
|
const config = require('../../config');
|
2019-04-15 12:42:31 +02:00
|
|
|
const Proxy = require('./proxy');
|
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
|
|
|
}
|
|
|
|
|
2019-04-15 12:54:29 +02:00
|
|
|
function loadApp(name) {
|
|
|
|
return require(getAppAbsolutePath(name));
|
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:54:29 +02:00
|
|
|
const AppClass = loadApp(name);
|
2019-04-15 15:11:00 +02:00
|
|
|
const proxy = Proxy.getInstance();
|
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
|
|
|
}
|
|
|
|
};
|