2013-08-05 19:11:32 +04:00
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
var path = require('path'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
when = require('when'),
|
2013-11-15 06:17:33 +04:00
|
|
|
createProxy = require('./proxy'),
|
2013-08-05 19:11:32 +04:00
|
|
|
ghostInstance,
|
|
|
|
loader;
|
|
|
|
|
|
|
|
function getGhostInstance() {
|
|
|
|
if (ghostInstance) {
|
|
|
|
return ghostInstance;
|
|
|
|
}
|
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
var Ghost = require('../../ghost');
|
2013-08-05 19:11:32 +04:00
|
|
|
|
|
|
|
ghostInstance = new Ghost();
|
|
|
|
|
|
|
|
return ghostInstance;
|
|
|
|
}
|
|
|
|
|
2013-08-12 18:13:15 +04:00
|
|
|
// Get a relative path to the given plugins root, defaults
|
|
|
|
// to be relative to __dirname
|
|
|
|
function getPluginRelativePath(name, relativeTo, ghost) {
|
|
|
|
ghost = ghost || getGhostInstance();
|
|
|
|
relativeTo = relativeTo || __dirname;
|
|
|
|
|
|
|
|
return path.relative(relativeTo, path.join(ghost.paths().pluginPath, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 19:11:32 +04:00
|
|
|
function getPluginByName(name, ghost) {
|
|
|
|
ghost = ghost || getGhostInstance();
|
|
|
|
|
|
|
|
// Grab the plugin class to instantiate
|
2013-08-12 18:13:15 +04:00
|
|
|
var PluginClass = require(getPluginRelativePath(name)),
|
2013-08-05 19:11:32 +04:00
|
|
|
plugin;
|
|
|
|
|
|
|
|
// Check for an actual class, otherwise just use whatever was returned
|
|
|
|
if (_.isFunction(PluginClass)) {
|
2013-11-15 06:17:33 +04:00
|
|
|
plugin = new PluginClass(createProxy(ghost));
|
2013-08-05 19:11:32 +04:00
|
|
|
} else {
|
|
|
|
plugin = PluginClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The loader is responsible for loading plugins
|
|
|
|
loader = {
|
|
|
|
// Load a plugin and return the instantiated plugin
|
|
|
|
installPluginByName: function (name, ghost) {
|
|
|
|
var plugin = getPluginByName(name, ghost);
|
|
|
|
|
|
|
|
// Check for an install() method on the plugin.
|
|
|
|
if (!_.isFunction(plugin.install)) {
|
|
|
|
return when.reject(new Error("Error loading plugin named " + name + "; no install() method defined."));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapping the install() with a when because it's possible
|
|
|
|
// to not return a promise from it.
|
2013-11-15 06:17:33 +04:00
|
|
|
return when(plugin.install(createProxy(ghost))).then(function () {
|
2013-08-05 19:11:32 +04:00
|
|
|
return when.resolve(plugin);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Activate a plugin and return it
|
|
|
|
activatePluginByName: function (name, ghost) {
|
|
|
|
var plugin = getPluginByName(name, ghost);
|
|
|
|
|
|
|
|
// Check for an activate() method on the plugin.
|
|
|
|
if (!_.isFunction(plugin.activate)) {
|
|
|
|
return when.reject(new Error("Error loading plugin named " + name + "; no activate() method defined."));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapping the activate() with a when because it's possible
|
|
|
|
// to not return a promise from it.
|
2013-11-15 06:17:33 +04:00
|
|
|
return when(plugin.activate(createProxy(ghost))).then(function () {
|
2013-08-05 19:11:32 +04:00
|
|
|
return when.resolve(plugin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = loader;
|