diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-01-29 12:21:57 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-01-29 12:21:57 +0100 |
commit | f32d03eb88d66b37874c461c6327b4576327b4db (patch) | |
tree | 98f3b91bb216731e2cb4f583f9f6d38d126377b2 /lib/plugins/manager.js | |
parent | e83d63c2aa5e30c26ada888990b263e6b786d3f6 (diff) | |
download | gitbook-f32d03eb88d66b37874c461c6327b4576327b4db.zip gitbook-f32d03eb88d66b37874c461c6327b4576327b4db.tar.gz gitbook-f32d03eb88d66b37874c461c6327b4576327b4db.tar.bz2 |
Add base for loading plugins
Diffstat (limited to 'lib/plugins/manager.js')
-rw-r--r-- | lib/plugins/manager.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/plugins/manager.js b/lib/plugins/manager.js index 8617044..b6549d6 100644 --- a/lib/plugins/manager.js +++ b/lib/plugins/manager.js @@ -1,10 +1,61 @@ +var _ = require('lodash'); +var Promise = require('../utils/promise'); var BookPlugin = require('./plugin'); + +/* +PluginsManager is an interface to work with multiple plugins at once: +- Extract assets from plugins +- Call hooks for all plugins, etc +*/ + function PluginsManager(book) { this.book = book; this.plugins = []; } +// Returns a plugin by its name +PluginsManager.prototype.get = function(name) { + return _.find(this.plugins, { + id: name + }); +}; + +// Load a plugin, or a list of plugins +PluginsManager.prototype.load = function(name) { + var that = this; + + if (!_.isArray(name)) { + return Promise.serie(name, function(_name) { + return that.load(_name); + }); + } + + return Promise() + + // Initiate and load the plugin + .then(function() { + var plugin; + + if (!_.isString(name)) plugin = name; + else plugin = new BookPlugin(that.book, name); + + if (that.get(plugin.id)) { + throw new Error('Plugin "'+plugin.id+'" is already loaded'); + } + + + if (plugin.isLoaded()) return plugin; + else return plugin.load() + .thenResolve(plugin); + }) + + .then(function(plugin) { + that.plugins.push(plugin); + }); +}; + + module.exports = PluginsManager; |