summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/plugins/__tests__/listDependencies.js (renamed from lib/plugins/__tests__/listAll.js)10
-rw-r--r--lib/plugins/__tests__/sortDependencies.js42
-rw-r--r--lib/plugins/__tests__/sortPlugins.js45
-rw-r--r--lib/plugins/listAll.js44
-rw-r--r--lib/plugins/listDependencies.js33
-rw-r--r--lib/plugins/listDepsForBook.js (renamed from lib/plugins/listForBook.js)10
-rw-r--r--lib/plugins/loadForBook.js83
-rw-r--r--lib/plugins/sortDependencies.js (renamed from lib/plugins/sortPlugins.js)16
8 files changed, 132 insertions, 151 deletions
diff --git a/lib/plugins/__tests__/listAll.js b/lib/plugins/__tests__/listDependencies.js
index 6a08c84..940faba 100644
--- a/lib/plugins/__tests__/listAll.js
+++ b/lib/plugins/__tests__/listDependencies.js
@@ -1,11 +1,11 @@
var PluginDependency = require('../../models/pluginDependency');
-var listAll = require('../listAll');
+var listDependencies = require('../listDependencies');
var toNames = require('../toNames');
-describe('listAll', function() {
+describe('listDependencies', function() {
it('must list default', function() {
var deps = PluginDependency.listFromString('ga,great');
- var plugins = listAll(deps);
+ var plugins = listDependencies(deps);
var names = toNames(plugins);
expect(names).toEqual([
@@ -16,7 +16,7 @@ describe('listAll', function() {
it('must list from array with -', function() {
var deps = PluginDependency.listFromString('ga,-great');
- var plugins = listAll(deps);
+ var plugins = listDependencies(deps);
var names = toNames(plugins);
expect(names).toEqual([
@@ -27,7 +27,7 @@ describe('listAll', function() {
it('must remove default plugins using -', function() {
var deps = PluginDependency.listFromString('ga,-search');
- var plugins = listAll(deps);
+ var plugins = listDependencies(deps);
var names = toNames(plugins);
expect(names).toEqual([
diff --git a/lib/plugins/__tests__/sortDependencies.js b/lib/plugins/__tests__/sortDependencies.js
new file mode 100644
index 0000000..87df477
--- /dev/null
+++ b/lib/plugins/__tests__/sortDependencies.js
@@ -0,0 +1,42 @@
+var PluginDependency = require('../../models/pluginDependency');
+var sortDependencies = require('../sortDependencies');
+var toNames = require('../toNames');
+
+describe('sortDependencies', function() {
+ it('must load themes after plugins', function() {
+ var allPlugins = PluginDependency.listFromArray([
+ 'hello',
+ 'theme-test',
+ 'world'
+ ]);
+
+ var sorted = sortDependencies(allPlugins);
+ var names = toNames(sorted);
+
+ expect(names).toEqual([
+ 'hello',
+ 'world',
+ 'theme-test'
+ ]);
+ });
+
+ it('must keep order of themes', function() {
+ var allPlugins = PluginDependency.listFromArray([
+ 'theme-test',
+ 'theme-test1',
+ 'hello',
+ 'theme-test2',
+ 'world'
+ ]);
+ var sorted = sortDependencies(allPlugins);
+ var names = toNames(sorted);
+
+ expect(names).toEqual([
+ 'hello',
+ 'world',
+ 'theme-test',
+ 'theme-test1',
+ 'theme-test2'
+ ]);
+ });
+}); \ No newline at end of file
diff --git a/lib/plugins/__tests__/sortPlugins.js b/lib/plugins/__tests__/sortPlugins.js
deleted file mode 100644
index 4aa26a3..0000000
--- a/lib/plugins/__tests__/sortPlugins.js
+++ /dev/null
@@ -1,45 +0,0 @@
-var Immutable = require('immutable');
-
-var Plugin = require('../../models/plugin');
-var sortPlugins = require('../sortPlugins');
-var toNames = require('../toNames');
-
-describe('sortPlugins', function() {
- it('must load themes after plugins', function() {
- var allPlugins = Immutable.OrderedMap([
- ['hello', Plugin.createFromString('hello')],
- ['theme-test', Plugin.createFromString('theme-test')],
- ['world', Plugin.createFromString('world')]
- ]);
-
- var sorted = sortPlugins(allPlugins);
- var names = toNames(sorted);
-
- expect(names).toEqual([
- 'hello',
- 'world',
- 'theme-test'
- ]);
- });
-
- it('must keep order of themes', function() {
- var allPlugins = Immutable.OrderedMap([
- ['theme-test', Plugin.createFromString('theme-test')],
- ['theme-test1', Plugin.createFromString('theme-test1')],
- ['hello', Plugin.createFromString('hello')],
- ['theme-test2', Plugin.createFromString('theme-test2')],
- ['world', Plugin.createFromString('world')]
- ]);
-
- var sorted = sortPlugins(allPlugins);
- var names = toNames(sorted);
-
- expect(names).toEqual([
- 'hello',
- 'world',
- 'theme-test',
- 'theme-test1',
- 'theme-test2'
- ]);
- });
-}); \ No newline at end of file
diff --git a/lib/plugins/listAll.js b/lib/plugins/listAll.js
deleted file mode 100644
index e8c3008..0000000
--- a/lib/plugins/listAll.js
+++ /dev/null
@@ -1,44 +0,0 @@
-var Immutable = require('immutable');
-var Plugin = require('../models/plugin');
-
-var DEFAULT_PLUGINS = require('../constants/defaultPlugins');
-var sortPlugins = require('./sortPlugins');
-
-/**
- * List all plugins for a book
- *
- * @param {List<PluginDependency>} deps
- * @return {OrderedMap<Plugin>}
- */
-function listAll(deps) {
- // Extract list of plugins to disable (starting with -)
- var toRemove = deps
- .filter(function(plugin) {
- return !plugin.isEnabled();
- })
- .map(function(plugin) {
- return plugin.getName();
- });
-
- // Concat with default plugins
- deps = deps.concat(DEFAULT_PLUGINS);
-
- // Convert to an ordered map of Plugin
- var plugins = deps
- .map(function(dep) {
- var plugin = Plugin.createFromDep(dep);
-
- return [dep.getName(), plugin];
- });
- plugins = Immutable.OrderedMap(plugins);
-
- // Remove plugins
- plugins = plugins.filterNot(function(plugin, name) {
- return toRemove.includes(name);
- });
-
- // Sort plugins
- return sortPlugins(plugins);
-}
-
-module.exports = listAll;
diff --git a/lib/plugins/listDependencies.js b/lib/plugins/listDependencies.js
new file mode 100644
index 0000000..d52eaa9
--- /dev/null
+++ b/lib/plugins/listDependencies.js
@@ -0,0 +1,33 @@
+var DEFAULT_PLUGINS = require('../constants/defaultPlugins');
+var sortDependencies = require('./sortDependencies');
+
+/**
+ * List all dependencies for a book, including default plugins.
+ * It returns a concat with default plugins and remove disabled ones.
+ *
+ * @param {List<PluginDependency>} deps
+ * @return {List<PluginDependency>}
+ */
+function listDependencies(deps) {
+ // Extract list of plugins to disable (starting with -)
+ var toRemove = deps
+ .filter(function(plugin) {
+ return !plugin.isEnabled();
+ })
+ .map(function(plugin) {
+ return plugin.getName();
+ });
+
+ // Concat with default plugins
+ deps = deps.concat(DEFAULT_PLUGINS);
+
+ // Remove plugins
+ deps = deps.filterNot(function(plugin) {
+ return toRemove.includes(plugin.getName());
+ });
+
+ // Sort
+ return sortDependencies(deps);
+}
+
+module.exports = listDependencies;
diff --git a/lib/plugins/listForBook.js b/lib/plugins/listDepsForBook.js
index 6764403..196e3aa 100644
--- a/lib/plugins/listForBook.js
+++ b/lib/plugins/listDepsForBook.js
@@ -1,4 +1,4 @@
-var listAll = require('./listAll');
+var listDependencies = require('./listDependencies');
/**
* List all plugin requirements for a book.
@@ -6,13 +6,13 @@ var listAll = require('./listAll');
* since plugins can have their own dependencies
*
* @param {Book}
- * @return {OrderedMap<Plugin>}
+ * @return {List<PluginDependency>}
*/
-function listForBook(book) {
+function listDepsForBook(book) {
var config = book.getConfig();
var plugins = config.getPluginDependencies();
- return listAll(plugins);
+ return listDependencies(plugins);
}
-module.exports = listForBook;
+module.exports = listDepsForBook;
diff --git a/lib/plugins/loadForBook.js b/lib/plugins/loadForBook.js
index a3f9dd0..757677e 100644
--- a/lib/plugins/loadForBook.js
+++ b/lib/plugins/loadForBook.js
@@ -1,6 +1,7 @@
-var Promise = require('../utils/promise');
+var Immutable = require('immutable');
-var listForBook = require('./listForBook');
+var Promise = require('../utils/promise');
+var listDepsForBook = require('./listDepsForBook');
var findForBook = require('./findForBook');
var loadPlugin = require('./loadPlugin');
@@ -13,60 +14,56 @@ var loadPlugin = require('./loadPlugin');
*/
function loadForBook(book) {
var logger = book.getLogger();
- var requirements = listForBook(book);
- var requirementsKeys = requirements.keySeq().toList();
-
- return findForBook(book)
- .then(function(installed) {
- // Filter out plugins not listed of first level
- // (aka pre-installed plugins)
- /*installed = installed.filter(function(plugin) {
- return (
- // Plugin is a dependency of another one
- plugin.getDepth() > 0 ||
-
- // Plugin is specified in "book.json"
- requirements.has(plugin.getName())
- );
- });*/
- // Insert installed plugins not listed in required
- var pluginsToLoad = installed.reduce(function(pluginSeq, plugin) {
- var name = plugin.getName();
+ // List the dependencies
+ var requirements = listDepsForBook(book);
- if (requirements.has(name)) {
- return pluginSeq;
+ // List all plugins installed in the book
+ return findForBook(book)
+ .then(function(installedMap) {
+ var missing = [];
+ var plugins = requirements.reduce(function(result, dep) {
+ var name = dep.getName();
+ var installed = installedMap.get(name);
+
+ if (installed) {
+ var deps = installedMap
+ .filter(function(plugin) {
+ return plugin.getParent() === name;
+ })
+ .toArray();
+
+ result = result.concat(deps);
+ result.push(installed);
+ } else {
+ missing.push(name);
}
- var parentName = plugin.getParent();
-
-
- pluginSeq = pluginSeq.push([
- name,
- plugin
- ]);
-
- return pluginSeq;
- }, requirements.entrySeq());
-
+ return result;
+ }, []);
+ // Convert plugins list to a map
+ plugins = Immutable.List(plugins)
+ .map(function(plugin) {
+ return [
+ plugin.getName(),
+ plugin
+ ];
+ });
+ plugins = Immutable.OrderedMap(plugins);
// Log state
- logger.info.ln(installed.size + ' plugins are installed');
- if (requirements.size != installed.size) {
+ logger.info.ln(installedMap.size + ' plugins are installed');
+ if (requirements.size != installedMap.size) {
logger.info.ln(requirements.size + ' explicitly listed');
}
// Verify that all plugins are present
- var notInstalled = requirementsKeys.filter(function(name) {
- return !installed.has(name);
- });
-
- if (notInstalled.size > 0) {
- throw new Error('Couldn\'t locate plugins "' + notInstalled.join(', ') + '", Run \'gitbook install\' to install plugins from registry.');
+ if (missing.length > 0) {
+ throw new Error('Couldn\'t locate plugins "' + missing.join(', ') + '", Run \'gitbook install\' to install plugins from registry.');
}
- return Promise.map(installed, function(plugin) {
+ return Promise.map(plugins, function(plugin) {
return loadPlugin(book, plugin);
});
});
diff --git a/lib/plugins/sortPlugins.js b/lib/plugins/sortDependencies.js
index 155b691..7f10095 100644
--- a/lib/plugins/sortPlugins.js
+++ b/lib/plugins/sortDependencies.js
@@ -18,19 +18,17 @@ function pluginType(plugin) {
/**
- * Sort the list of installed plugins to match list in book.json
+ * Sort the list of dependencies to match list in book.json
* The themes should always be loaded after the plugins
*
- * @param {<OrderedMap<String:Plugin>>} plugins
- * @return {OrderedMap<String:Plugin>}
+ * @param {List<PluginDependency>} deps
+ * @return {List<PluginDependency>}
*/
-function sortPlugins(plugins) {
+function sortDependencies(plugins) {
var byTypes = plugins.groupBy(pluginType);
- return byTypes.get(TYPE_PLUGIN, Immutable.OrderedMap())
- .merge(
- byTypes.get(TYPE_THEME, Immutable.OrderedMap())
- );
+ return byTypes.get(TYPE_PLUGIN, Immutable.List())
+ .concat(byTypes.get(TYPE_THEME, Immutable.List()));
}
-module.exports = sortPlugins; \ No newline at end of file
+module.exports = sortDependencies; \ No newline at end of file