summaryrefslogtreecommitdiffstats
path: root/lib/plugins
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-03-15 11:43:37 +0100
committerSamy Pessé <samypesse@gmail.com>2016-03-15 11:43:37 +0100
commit4d19a33af24d5ee68c473b78b81ae5e30ade6007 (patch)
treee772821a507173ea2f223419b941faf4cc7422c5 /lib/plugins
parent9d67a0e6560cf7441a267d98a412376586b65d1d (diff)
downloadgitbook-4d19a33af24d5ee68c473b78b81ae5e30ade6007.zip
gitbook-4d19a33af24d5ee68c473b78b81ae5e30ade6007.tar.gz
gitbook-4d19a33af24d5ee68c473b78b81ae5e30ade6007.tar.bz2
Add method registry.list to list all plugins for a book
Diffstat (limited to 'lib/plugins')
-rw-r--r--lib/plugins/registry.js50
1 files changed, 49 insertions, 1 deletions
diff --git a/lib/plugins/registry.js b/lib/plugins/registry.js
index abb8215..bc17a8c 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');
@@ -104,6 +106,50 @@ function installPlugin(book, plugin, version) {
});
}
+// List all packages installed inside a folder
+// Returns a map { pluginName -> folder }
+function listInstalled(folder) {
+ var options = {
+ dev: false,
+ log: function() {},
+ depth: 4
+ };
+ var result = {};
+
+ function onPackage(pkg, isRoot) {
+ if (!validateId(pkg.name)){
+ if (!isRoot) return;
+ } else {
+ result[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 result;
+ });
+}
+
+// List installed plugins for a book (defaults and installed)
+function listPlugins(book) {
+ return Promise.nfcall([
+ listInstalled(path.resolve(__dirname, '../..')),
+ listInstalled(book.root)
+ ])
+ .spread(function(defaultPlugins, plugins) {
+ return _.extend(defaultPlugins, plugins);
+ });
+}
+
module.exports = {
npmId: npmId,
pluginId: pluginId,
@@ -111,5 +157,7 @@ module.exports = {
resolve: resolveVersion,
link: linkPlugin,
- install: installPlugin
+ install: installPlugin,
+ list: listPlugins,
+ listInstalled: listInstalled
};