summaryrefslogtreecommitdiffstats
path: root/lib/plugins
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-03-15 12:37:52 +0100
committerSamy Pessé <samypesse@gmail.com>2016-03-15 12:37:52 +0100
commit0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e (patch)
tree2b2dede4d212d40992a829cd33151d35a7a7d91d /lib/plugins
parentc499a8a13a3059e3953727866beb7c986e46dd78 (diff)
parentfbffd54aa244d8a969200b0efbed3d7dc9eb73d0 (diff)
downloadgitbook-0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e.zip
gitbook-0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e.tar.gz
gitbook-0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e.tar.bz2
Merge pull request #1181 from GitbookIO/feature/allplugins_tpl
All plugins can extend templates/theme
Diffstat (limited to 'lib/plugins')
-rw-r--r--lib/plugins/index.js54
-rw-r--r--lib/plugins/plugin.js33
-rw-r--r--lib/plugins/registry.js54
3 files changed, 104 insertions, 37 deletions
diff --git a/lib/plugins/index.js b/lib/plugins/index.js
index 8280542..f897d9c 100644
--- a/lib/plugins/index.js
+++ b/lib/plugins/index.js
@@ -21,6 +21,11 @@ function PluginsManager(book) {
_.bindAll(this);
}
+// Returns the list of plugins
+PluginsManager.prototype.list = function() {
+ return this.plugins;
+};
+
// Return count of plugins loaded
PluginsManager.prototype.count = function() {
return _.size(this.plugins);
@@ -33,24 +38,21 @@ PluginsManager.prototype.get = function(name) {
});
};
-// Load a plugin, or a list of plugins
-PluginsManager.prototype.load = function(name) {
+// Load a plugin (could be a BookPlugin or {name,path})
+PluginsManager.prototype.load = function(plugin) {
var that = this;
- if (_.isArray(name)) {
- return Promise.serie(name, function(_name) {
- return that.load(_name);
- });
+ if (_.isArray(plugin)) {
+ return Promise.serie(plugin, that.load);
}
return Promise()
// Initiate and load the plugin
.then(function() {
- var plugin;
-
- if (!_.isString(name)) plugin = name;
- else plugin = new BookPlugin(that.book, name);
+ if (!(plugin instanceof BookPlugin)) {
+ plugin = new BookPlugin(that.book, plugin.name, plugin.path);
+ }
if (that.get(plugin.id)) {
throw new Error('Plugin "'+plugin.id+'" is already loaded');
@@ -68,10 +70,36 @@ PluginsManager.prototype.load = function(name) {
// Load all plugins from the book's configuration
PluginsManager.prototype.loadAll = function() {
- var plugins = _.pluck(this.book.config.get('plugins'), 'name');
+ var that = this;
+ var pluginNames = _.pluck(this.book.config.get('plugins'), 'name');
+
+ return registry.list(this.book)
+ .then(function(plugins) {
+ // Filter out plugins not listed of first level
+ // (aka pre-installed plugins)
+ plugins = _.filter(plugins, function(plugin) {
+ return (
+ plugin.depth > 1 ||
+ _.contains(pluginNames, plugin.name)
+ );
+ });
- this.log.info.ln('loading', plugins.length, 'plugins');
- return this.load(plugins);
+ // Log state
+ that.log.info.ln(_.size(plugins) + ' are installed');
+ if (_.size(pluginNames) != _.size(plugins)) that.log.info.ln(_.size(pluginNames) + ' explicitly listed');
+
+ // Verify that all plugins are present
+ var notInstalled = _.filter(pluginNames, function(name) {
+ return !_.find(plugins, { name: name });
+ });
+
+ if (_.size(notInstalled) > 0) {
+ throw new Error('Couldn\'t locate plugins "' + notInstalled.join(', ') + '", Run \'gitbook install\' to install plugins from registry.');
+ }
+
+ // Load plugins
+ return that.load(plugins);
+ });
};
// Setup a plugin
diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js
index d707e5c..d1c00d8 100644
--- a/lib/plugins/plugin.js
+++ b/lib/plugins/plugin.js
@@ -24,13 +24,14 @@ function isModuleNotFound(err) {
return err.message.indexOf('Cannot find module') >= 0;
}
-function BookPlugin(book, pluginId) {
+function BookPlugin(book, pluginId, pluginFolder) {
this.book = book;
this.log = this.book.log.prefix(pluginId);
+
this.id = pluginId;
this.npmId = registry.npmId(pluginId);
- this.root;
+ this.root = pluginFolder;
this.packageInfos = undefined;
this.content = undefined;
@@ -51,8 +52,7 @@ BookPlugin.prototype.bind = function(fn) {
return fn.bind(compatibility.pluginCtx(this));
};
-// Load this plugin
-// An optional folder to search in can be passed
+// Load this plugin from its root folder
BookPlugin.prototype.load = function(folder) {
var that = this;
@@ -60,18 +60,12 @@ BookPlugin.prototype.load = function(folder) {
return Promise.reject(new Error('Plugin "' + this.id + '" is already loaded'));
}
- // Fodlers to search plugins in
- var searchPaths = _.compact([
- folder,
- this.book.resolve('node_modules'),
- __dirname
- ]);
-
// Try loading plugins from different location
- var p = Promise.some(searchPaths, function(baseDir) {
+ var p = Promise()
+ .then(function() {
// Locate plugin and load pacjage.json
try {
- var res = resolve.sync(that.npmId + '/package.json', { basedir: baseDir });
+ var res = resolve.sync('./package.json', { basedir: that.root });
that.root = path.dirname(res);
that.packageInfos = require(res);
@@ -81,12 +75,12 @@ BookPlugin.prototype.load = function(folder) {
that.packageInfos = undefined;
that.content = undefined;
- return false;
+ return;
}
// Load plugin JS content
try {
- that.content = require(resolve.sync(that.npmId, { basedir: baseDir }));
+ that.content = require(that.root);
} catch(err) {
// It's no big deal if the plugin doesn't have an "index.js"
// (For example: themes)
@@ -98,8 +92,6 @@ BookPlugin.prototype.load = function(folder) {
});
}
}
-
- return true;
})
.then(that.validate)
@@ -122,18 +114,15 @@ BookPlugin.prototype.load = function(folder) {
// This method throws erros if plugin is invalid
BookPlugin.prototype.validate = function() {
var isValid = (
+ this.isLoaded() &&
this.packageInfos &&
this.packageInfos.name &&
this.packageInfos.engines &&
this.packageInfos.engines.gitbook
);
- if (!this.isLoaded()) {
- throw new Error('Couldn\'t locate plugin "' + this.id + '", Run \'gitbook install\' to install plugins from registry.');
- }
-
if (!isValid) {
- throw new Error('Invalid plugin "' + this.id + '"');
+ throw new Error('Error loading plugin "' + this.id + '" at "' + this.root + '"');
}
if (!gitbook.satisfies(this.packageInfos.engines.gitbook)) {
diff --git a/lib/plugins/registry.js b/lib/plugins/registry.js
index abb8215..ea172c4 100644
--- a/lib/plugins/registry.js
+++ b/lib/plugins/registry.js
@@ -1,7 +1,9 @@
var npm = require('npm');
var npmi = require('npmi');
+var path = require('path');
var semver = require('semver');
var _ = require('lodash');
+var readInstalled = require('read-installed');
var Promise = require('../utils/promise');
var gitbook = require('../gitbook');
@@ -21,7 +23,7 @@ function pluginId(name) {
// Validate an NPM plugin ID
function validateId(name) {
- return name.indexOf(PLUGIN_PREFIX) === 0;
+ return name && name.indexOf(PLUGIN_PREFIX) === 0;
}
// Initialize NPM for operations
@@ -104,6 +106,52 @@ function installPlugin(book, plugin, version) {
});
}
+// List all packages installed inside a folder
+// Returns an ordered list of plugins
+function listInstalled(folder) {
+ var options = {
+ dev: false,
+ log: function() {},
+ depth: 4
+ };
+ var results = [];
+
+ function onPackage(pkg, isRoot) {
+ if (!validateId(pkg.name)){
+ if (!isRoot) return;
+ } else {
+ results.push({
+ name: pluginId(pkg.name),
+ version: pkg.version,
+ path: pkg.realPath,
+ depth: pkg.depth
+ });
+ }
+
+ _.each(pkg.dependencies, function(dep) {
+ onPackage(dep);
+ });
+ }
+
+ return Promise.nfcall(readInstalled, folder, options)
+ .then(function(data) {
+ onPackage(data, true);
+ return _.uniq(results, 'name');
+ });
+}
+
+// List installed plugins for a book (defaults and installed)
+function listPlugins(book) {
+ return Promise.all([
+ listInstalled(path.resolve(__dirname, '../..')),
+ listInstalled(book.root)
+ ])
+ .spread(function(defaultPlugins, plugins) {
+ var results = plugins.concat(defaultPlugins);
+ return _.uniq(results, 'name');
+ });
+}
+
module.exports = {
npmId: npmId,
pluginId: pluginId,
@@ -111,5 +159,7 @@ module.exports = {
resolve: resolveVersion,
link: linkPlugin,
- install: installPlugin
+ install: installPlugin,
+ list: listPlugins,
+ listInstalled: listInstalled
};