diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-03-15 12:37:52 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-03-15 12:37:52 +0100 |
commit | 0b4db49f5752fd2a6f72c3ba57cdb5c1b5b8ae4e (patch) | |
tree | 2b2dede4d212d40992a829cd33151d35a7a7d91d /lib/plugins/registry.js | |
parent | c499a8a13a3059e3953727866beb7c986e46dd78 (diff) | |
parent | fbffd54aa244d8a969200b0efbed3d7dc9eb73d0 (diff) | |
download | gitbook-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/registry.js')
-rw-r--r-- | lib/plugins/registry.js | 54 |
1 files changed, 52 insertions, 2 deletions
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 }; |