diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-02-02 21:45:14 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-02-02 21:45:14 +0100 |
commit | c0dcb886ef5399cefc3b11195e608c59eb7dea98 (patch) | |
tree | 35c4624e764c89f52b3ddc4ae582003da64ff182 /lib | |
parent | df391288949a703727f531333c228072d901bd55 (diff) | |
download | gitbook-c0dcb886ef5399cefc3b11195e608c59eb7dea98.zip gitbook-c0dcb886ef5399cefc3b11195e608c59eb7dea98.tar.gz gitbook-c0dcb886ef5399cefc3b11195e608c59eb7dea98.tar.bz2 |
Improve install command to resolve best plugin version
Diffstat (limited to 'lib')
-rw-r--r-- | lib/configuration.js | 35 | ||||
-rw-r--r-- | lib/index.js | 4 | ||||
-rw-r--r-- | lib/pluginslist.js | 84 |
3 files changed, 85 insertions, 38 deletions
diff --git a/lib/configuration.js b/lib/configuration.js index af4d3e2..a7b4f1f 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -1,7 +1,6 @@ var _ = require("lodash"); var Q = require("q"); var path = require("path"); -var npmi = require('npmi'); var semver = require('semver'); var pkg = require('../package.json'); @@ -126,40 +125,6 @@ Configuration.prototype.normalizeLanguage = function() { return _.first(this.options.language.split("-")).toLowerCase(); }; -// Install plugins -Configuration.prototype.installPlugins = function(options) { - var that = this; - options = _.defaults(options || { - log: true - }); - - // Remov defaults (no need to install) - var plugins = _.filter(that.options.plugins, function(plugin) { - return !_.contains(defaultsPlugins, plugin.name); - }); - - // Install plugins one by one - if (options.log) that.book.log.info.ln(plugins.length+" plugins to install"); - return _.reduce(plugins, function(prev, plugin) { - return prev.then(function() { - var fullname = "gitbook-plugin-"+plugin.name; - if (options.log) that.book.log.info.ln("install plugin", plugin.name, "from npm ("+fullname+") with version", (plugin.version || "*")); - return Q.nfcall(npmi, { - 'name': fullname, - 'version': plugin.version, - 'path': that.book.root, - 'npmLoad': { - 'loglevel': 'silent', - 'loaded': false, - 'prefix': that.book.root - } - }) - .then(function() { - that.book.log.info.ok("plugin", plugin.name, "installed with success"); - }); - }); - }, Q()); -} // Default configuration Configuration.DEFAULT = { diff --git a/lib/index.js b/lib/index.js index e91211f..31af646 100644 --- a/lib/index.js +++ b/lib/index.js @@ -186,9 +186,7 @@ module.exports = { return book.config.load() .then(function() { - return book.config.installPlugins({ - log: true - }); + return book.plugins.install(); }) .then(function(){ console.log(""); diff --git a/lib/pluginslist.js b/lib/pluginslist.js index 10e21ac..b4b601e 100644 --- a/lib/pluginslist.js +++ b/lib/pluginslist.js @@ -1,7 +1,16 @@ var _ = require("lodash"); var Q = require("q"); +var npmi = require('npmi'); +var npm = require('npm'); +var semver = require('semver'); var Plugin = require("./plugin"); +var pkg = require("../package.json"); + +var initNPM = _.memoize(function() { + return Q.nfcall(npm.load, { silent: true, loglevel: 'silent' }); +}); + var PluginsList = function(book, plugins) { this.book = book; @@ -138,4 +147,79 @@ PluginsList.prototype.resources = function(namespace) { return this.namespaces[namespace].resources; }; +// Install plugins from a book +PluginsList.prototype.install = function() { + var that = this; + var defaultsPlugins = _.pluck(that.book.options.defaultsPlugins) + + // Remove defaults (no need to install) + var plugins = _.filter(that.book.options.plugins, function(plugin) { + return !_.contains(defaultsPlugins, plugin.name); + }); + + // Install plugins one by one + that.book.log.info.ln(plugins.length+" plugins to install"); + return _.reduce(plugins, function(prev, plugin) { + return prev.then(function() { + var fullname = "gitbook-plugin-"+plugin.name; + + return Q() + + // Resolve version if needed + .then(function() { + if (plugin.version) return plugin.version; + + that.book.log.info.ln("No version specified, resolve plugin", plugin.name); + return initNPM() + .then(function() { + return Q.nfcall(npm.commands.view, [fullname+"@>=0.0.0", "engines"], true); + }) + .then(function(versions) { + return _.chain(versions) + .pairs() + .map(function(v) { + return { + version: v[0], + gitbook: (v[1].engines || {})["gitbook"] + } + }) + .filter(function(v) { + return v.gitbook && semver.satisfies(pkg.version, v.gitbook); + }) + .sort(function(v1, v2) { + return semver.lt(v1.version, v2.version)? 1 : -1; + }) + .pluck("version") + .first() + .value(); + }); + }) + + // Install the plugin with the resolved version + .then(function(version) { + if (!version) { + throw "Found no satisfactory version for plugin "+plugin.name; + } + + that.book.log.info.ln("install plugin", plugin.name, "from npm ("+fullname+") with version", version); + return Q.nfcall(npmi, { + 'name': fullname, + 'version': version, + 'path': that.book.root, + 'npmLoad': { + 'loglevel': 'silent', + 'loaded': true, + 'prefix': that.book.root + } + }); + }) + .then(function() { + that.book.log.info.ok("plugin", plugin.name, "installed with success"); + }); + }); + }, Q()); +}; + + + module.exports = PluginsList; |