diff options
author | Johan Preynat <johan.preynat@gmail.com> | 2016-05-28 11:56:34 +0200 |
---|---|---|
committer | Johan Preynat <johan.preynat@gmail.com> | 2016-05-28 11:56:34 +0200 |
commit | 33406c308208210cfc94a25d1d5975aad6e3f557 (patch) | |
tree | f1bb28c1c7c765ea0653c5c2c56443a7eb4d6c2d /lib/plugins | |
parent | ebe845d46c9e3378a504f9b05c71ea361e36b599 (diff) | |
parent | c3851889b0eba506c3138daaf4f1ef583dd608db (diff) | |
download | gitbook-33406c308208210cfc94a25d1d5975aad6e3f557.zip gitbook-33406c308208210cfc94a25d1d5975aad6e3f557.tar.gz gitbook-33406c308208210cfc94a25d1d5975aad6e3f557.tar.bz2 |
Merge pull request #1336 from GitbookIO/loading/themes
Load plugins before themes
Diffstat (limited to 'lib/plugins')
-rw-r--r-- | lib/plugins/__tests__/sortPlugins.js | 70 | ||||
-rw-r--r-- | lib/plugins/loadForBook.js | 9 | ||||
-rw-r--r-- | lib/plugins/sortPlugins.js | 45 |
3 files changed, 119 insertions, 5 deletions
diff --git a/lib/plugins/__tests__/sortPlugins.js b/lib/plugins/__tests__/sortPlugins.js new file mode 100644 index 0000000..2e1b594 --- /dev/null +++ b/lib/plugins/__tests__/sortPlugins.js @@ -0,0 +1,70 @@ +var PluginDependency = require('../../models/pluginDependency'); +var sortPlugins = require('../sortPlugins'); +var listAll = require('../listAll'); +var THEME_PREFIX = require('../../constants/themePrefix'); + + +/** + Check if a plugin is a theme given its name + + @return {Boolean} +*/ +function isTheme(name) { + return (name && name.indexOf(THEME_PREFIX) === 0); +} + +describe('sortPlugins', function() { + it('must load themes after plugins', function() { + var deps = PluginDependency.listFromString('theme-faq'), + allPlugins = listAll(deps); + + return sortPlugins(allPlugins, []) + .then(function(sorted) { + var plugins = sorted.slice(0, -2), + themes = sorted.slice(-2); + + var pluginsOk = plugins.every(function(plugin) { + return !isTheme(plugin.getName()); + }); + + var themesOk = themes.every(function(theme) { + return isTheme(theme.getName()); + }); + + expect(pluginsOk).toBe(true); + expect(plugins.has('search')).toBe(true); + + expect(themesOk).toBe(true); + expect(themes.size).toBe(2); + expect(themes.has('theme-faq')).toBe(true); + expect(themes.has('theme-default')).toBe(true); + }); + }); + + it('must load themes after plugins with a complex dependencies list', function() { + var deps = PluginDependency.listFromString('comment,theme-faq,-search,ga'), + allPlugins = listAll(deps); + + return sortPlugins(allPlugins, []) + .then(function(sorted) { + var plugins = sorted.slice(0, -2), + themes = sorted.slice(-2); + + var pluginsOk = plugins.every(function(plugin) { + return !isTheme(plugin.getName()); + }); + + var themesOk = themes.every(function(theme) { + return isTheme(theme.getName()); + }); + + expect(pluginsOk).toBe(true); + expect(plugins.has('search')).toBe(false); + + expect(themesOk).toBe(true); + expect(themes.size).toBe(2); + expect(themes.has('theme-faq')).toBe(true); + expect(themes.has('theme-default')).toBe(true); + }); + }); +});
\ No newline at end of file diff --git a/lib/plugins/loadForBook.js b/lib/plugins/loadForBook.js index c4acb5f..fd45a77 100644 --- a/lib/plugins/loadForBook.js +++ b/lib/plugins/loadForBook.js @@ -3,6 +3,7 @@ var Promise = require('../utils/promise'); var listForBook = require('./listForBook'); var findForBook = require('./findForBook'); var loadPlugin = require('./loadPlugin'); +var sortPlugins = require('./sortPlugins'); /** @@ -27,11 +28,9 @@ function loadForBook(book) { ); }); - // Sort plugins to match list in book.json - installed = installed.sort(function(a, b){ - return requirementsKeys.indexOf(a.getName()) < requirementsKeys.indexOf(b.getName()) ? -1 : 1; - }); - + return sortPlugins(installed, requirementsKeys); + }) + .then(function(installed) { // Log state logger.info.ln(installed.size + ' plugins are installed'); if (requirements.size != installed.size) { diff --git a/lib/plugins/sortPlugins.js b/lib/plugins/sortPlugins.js new file mode 100644 index 0000000..6626936 --- /dev/null +++ b/lib/plugins/sortPlugins.js @@ -0,0 +1,45 @@ +var Promise = require('../utils/promise'); + +var THEME_PREFIX = require('../constants/themePrefix'); +var LOADING_ORDER = ['plugin', 'theme']; + +/** + Returns the type of a plugin given its name + + @return {String} +*/ +function pluginType(name) { + return (name && name.indexOf(THEME_PREFIX) === 0) ? 'theme' : 'plugin'; +} + + +/** + Sort the list of installed plugins to match list in book.json + The themes should always be loaded after the plugins + + @param {<OrderedMap<String:Plugin>>} plugins + @param {<List<String>>} requirementsKeys + @return {Promise<OrderedMap<String:Plugin>>} +*/ + +function sortPlugins(plugins, requirementsKeys) { + return Promise() + .then(function() { + // Sort plugins to match list in book.json + plugins = plugins.sort(function(a, b) { + // Get order from book.json + var definitionOrder = requirementsKeys.indexOf(a.getName()) < requirementsKeys.indexOf(b.getName()); + + // Get order from plugins a and b type + var aType = pluginType(a.getName()), + bType = pluginType(b.getName()), + loadingOrder = LOADING_ORDER.indexOf(aType) < LOADING_ORDER.indexOf(bType); + + return loadingOrder || definitionOrder ? -1 : 1; + }); + + return plugins; + }); +} + +module.exports = sortPlugins;
\ No newline at end of file |