mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-14 09:52:09 +03:00
13d2d04c72
Progress on #2095 - Add new AppPermissions class with read() method - has default permissions to read and browse posts - uses default permissions if no package.json - uses default permissions if no ghost object in package.json - errors when reading malformed package.json - uses ghost.permissions if found in package.json
103 lines
3.3 KiB
JavaScript
103 lines
3.3 KiB
JavaScript
|
|
var path = require('path'),
|
|
_ = require('lodash'),
|
|
when = require('when'),
|
|
appProxy = require('./proxy'),
|
|
config = require('../config'),
|
|
AppSandbox = require('./sandbox'),
|
|
AppDependencies = require('./dependencies'),
|
|
AppPermissions = require('./permissions'),
|
|
loader;
|
|
|
|
// Get the full path to an app by name
|
|
function getAppAbsolutePath(name) {
|
|
return path.join(config().paths.appPath, name);
|
|
}
|
|
|
|
// Get a relative path to the given apps root, defaults
|
|
// to be relative to __dirname
|
|
function getAppRelativePath(name, relativeTo) {
|
|
relativeTo = relativeTo || __dirname;
|
|
|
|
return path.relative(relativeTo, getAppAbsolutePath(name));
|
|
}
|
|
|
|
// Load apps through a psuedo sandbox
|
|
function loadApp(appPath) {
|
|
var sandbox = new AppSandbox();
|
|
|
|
return sandbox.loadApp(appPath);
|
|
}
|
|
|
|
function getAppByName(name) {
|
|
// Grab the app class to instantiate
|
|
var AppClass = loadApp(getAppRelativePath(name)),
|
|
app;
|
|
|
|
// Check for an actual class, otherwise just use whatever was returned
|
|
if (_.isFunction(AppClass)) {
|
|
app = new AppClass(appProxy);
|
|
} else {
|
|
app = AppClass;
|
|
}
|
|
|
|
return app;
|
|
}
|
|
|
|
// The loader is responsible for loading apps
|
|
loader = {
|
|
// Load a app and return the instantiated app
|
|
installAppByName: function (name) {
|
|
// Install the apps dependendencies first
|
|
var appPath = getAppAbsolutePath(name),
|
|
deps = new AppDependencies(appPath);
|
|
|
|
return deps.install()
|
|
.then(function () {
|
|
// Load app permissions
|
|
var perms = new AppPermissions(appPath);
|
|
|
|
return perms.read().otherwise(function (err) {
|
|
// Provide a helpful error about which app
|
|
return when.reject(new Error("Error loading app named " + name + "; problem reading permissions: " + err.message));
|
|
});
|
|
})
|
|
.then(function (appPerms) {
|
|
var app = getAppByName(name, appPerms);
|
|
|
|
// Check for an install() method on the app.
|
|
if (!_.isFunction(app.install)) {
|
|
return when.reject(new Error("Error loading app named " + name + "; no install() method defined."));
|
|
}
|
|
|
|
// Run the app.install() method
|
|
// Wrapping the install() with a when because it's possible
|
|
// to not return a promise from it.
|
|
return when(app.install(appProxy)).then(function () {
|
|
return when.resolve(app);
|
|
});
|
|
});
|
|
},
|
|
|
|
// Activate a app and return it
|
|
activateAppByName: function (name) {
|
|
var perms = new AppPermissions(getAppAbsolutePath(name));
|
|
|
|
return perms.read().then(function (appPerms) {
|
|
var app = getAppByName(name, appPerms);
|
|
|
|
// Check for an activate() method on the app.
|
|
if (!_.isFunction(app.activate)) {
|
|
return when.reject(new Error("Error loading app named " + name + "; no activate() method defined."));
|
|
}
|
|
|
|
// Wrapping the activate() with a when because it's possible
|
|
// to not return a promise from it.
|
|
return when(app.activate(appProxy)).then(function () {
|
|
return when.resolve(app);
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = loader; |