summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-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
};