2014-01-21 12:45:27 +04:00
|
|
|
|
|
|
|
var path = require('path'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-04-22 04:22:13 +04:00
|
|
|
AppProxy = require('./proxy'),
|
2014-01-21 12:45:27 +04:00
|
|
|
config = require('../config'),
|
2014-02-01 02:53:27 +04:00
|
|
|
AppSandbox = require('./sandbox'),
|
2014-02-02 10:07:39 +04:00
|
|
|
AppDependencies = require('./dependencies'),
|
2014-02-12 05:04:13 +04:00
|
|
|
AppPermissions = require('./permissions'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-01-21 12:45:27 +04:00
|
|
|
loader;
|
|
|
|
|
2016-04-11 16:58:41 +03:00
|
|
|
function isInternalApp(name) {
|
2016-09-13 18:41:14 +03:00
|
|
|
return _.includes(config.get('internalApps'), name);
|
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) {
|
2016-04-11 16:58:41 +03:00
|
|
|
if (isInternalApp(name)) {
|
2016-09-13 18:41:14 +03:00
|
|
|
return path.join(config.get('paths').internalAppPath, name);
|
2016-04-11 16:58:41 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 23:24:57 +03:00
|
|
|
return path.join(config.getContentPath('apps'), 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
|
|
|
|
function getAppRelativePath(name, relativeTo) {
|
|
|
|
relativeTo = relativeTo || __dirname;
|
|
|
|
|
2016-04-11 16:58:41 +03:00
|
|
|
var relativePath = path.relative(relativeTo, getAppAbsolutePath(name));
|
|
|
|
|
|
|
|
if (relativePath.charAt(0) !== '.') {
|
|
|
|
relativePath = './' + 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
|
2016-04-11 16:58:41 +03:00
|
|
|
function loadApp(appPath, isInternal) {
|
|
|
|
var sandbox = new AppSandbox({internal: isInternal});
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
return sandbox.loadApp(appPath);
|
|
|
|
}
|
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
|
2016-04-11 16:58:41 +03:00
|
|
|
var AppClass = loadApp(getAppRelativePath(name), isInternalApp(name)),
|
2014-04-22 04:22:13 +04:00
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: name,
|
2016-04-14 20:32:43 +03:00
|
|
|
permissions: permissions,
|
|
|
|
internal: isInternalApp(name)
|
2014-04-22 04:22:13 +04:00
|
|
|
}),
|
2014-01-21 12:45:27 +04:00
|
|
|
app;
|
|
|
|
|
|
|
|
// Check for an actual class, otherwise just use whatever was returned
|
|
|
|
if (_.isFunction(AppClass)) {
|
|
|
|
app = new AppClass(appProxy);
|
|
|
|
} else {
|
|
|
|
app = AppClass;
|
|
|
|
}
|
|
|
|
|
2014-04-22 04:22:13 +04:00
|
|
|
return {
|
|
|
|
app: app,
|
|
|
|
proxy: appProxy
|
|
|
|
};
|
2014-01-21 12:45:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The loader is responsible for loading apps
|
|
|
|
loader = {
|
|
|
|
// Load a app and return the instantiated app
|
|
|
|
installAppByName: function (name) {
|
2015-03-15 00:50:00 +03:00
|
|
|
// Install the apps dependencies first
|
2014-02-12 05:04:13 +04:00
|
|
|
var appPath = getAppAbsolutePath(name),
|
|
|
|
deps = new AppDependencies(appPath);
|
|
|
|
|
|
|
|
return deps.install()
|
|
|
|
.then(function () {
|
|
|
|
// Load app permissions
|
|
|
|
var perms = new AppPermissions(appPath);
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return perms.read().catch(function (err) {
|
2014-02-12 05:04:13 +04:00
|
|
|
// Provide a helpful error about which app
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new Error(i18n.t('errors.apps.permissionsErrorLoadingApp.error', {name: name, message: err.message})));
|
2014-02-12 05:04:13 +04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function (appPerms) {
|
2014-04-22 04:22:13 +04:00
|
|
|
var appInfo = getAppByName(name, appPerms),
|
|
|
|
app = appInfo.app,
|
|
|
|
appProxy = appInfo.proxy;
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
// Check for an install() method on the app.
|
|
|
|
if (!_.isFunction(app.install)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new Error(i18n.t('errors.apps.noInstallMethodLoadingApp.error', {name: name})));
|
2014-02-12 05:04:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the app.install() method
|
|
|
|
// Wrapping the install() with a when because it's possible
|
|
|
|
// to not return a promise from it.
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(app.install(appProxy)).return(app);
|
2014-02-02 10:07:39 +04:00
|
|
|
});
|
2014-01-21 12:45:27 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Activate a app and return it
|
|
|
|
activateAppByName: function (name) {
|
2014-02-12 05:04:13 +04:00
|
|
|
var 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) {
|
2014-04-22 04:22:13 +04:00
|
|
|
var appInfo = getAppByName(name, appPerms),
|
|
|
|
app = appInfo.app,
|
|
|
|
appProxy = appInfo.proxy;
|
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)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new Error(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.
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(app.activate(appProxy)).return(app);
|
2014-01-21 12:45:27 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
module.exports = loader;
|