mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
sortPlugins should not be async
This commit is contained in:
@@ -7,56 +7,54 @@ describe('sortPlugins', 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 sorted = sortPlugins(allPlugins, []);
|
||||
|
||||
var pluginsOk = plugins.every(function(plugin) {
|
||||
return !plugin.isTheme();
|
||||
});
|
||||
var plugins = sorted.slice(0, -2),
|
||||
themes = sorted.slice(-2);
|
||||
|
||||
var themesOk = themes.every(function(theme) {
|
||||
return theme.isTheme();
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
// theme-default should be last
|
||||
expect(themes.last().getName()).toBe('theme-default');
|
||||
var pluginsOk = plugins.every(function(plugin) {
|
||||
return !plugin.isTheme();
|
||||
});
|
||||
|
||||
var themesOk = themes.every(function(theme) {
|
||||
return theme.isTheme();
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
// theme-default should be last
|
||||
expect(themes.last().getName()).toBe('theme-default');
|
||||
});
|
||||
|
||||
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 sorted = sortPlugins(allPlugins, []);
|
||||
|
||||
var pluginsOk = plugins.every(function(plugin) {
|
||||
return !plugin.isTheme();
|
||||
});
|
||||
var plugins = sorted.slice(0, -2),
|
||||
themes = sorted.slice(-2);
|
||||
|
||||
var themesOk = themes.every(function(theme) {
|
||||
return theme.isTheme();
|
||||
});
|
||||
|
||||
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);
|
||||
var pluginsOk = plugins.every(function(plugin) {
|
||||
return !plugin.isTheme();
|
||||
});
|
||||
|
||||
var themesOk = themes.every(function(theme) {
|
||||
return theme.isTheme();
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -7,11 +7,10 @@ var sortPlugins = require('./sortPlugins');
|
||||
|
||||
|
||||
/**
|
||||
Load a list of plugins in a book
|
||||
|
||||
@param {Book}
|
||||
@return {Promise<Map<String:Plugin>}
|
||||
*/
|
||||
* Load a list of plugins in a book
|
||||
* @param {Book}
|
||||
* @return {Promise<Map<String:Plugin>}
|
||||
*/
|
||||
function loadForBook(book) {
|
||||
var logger = book.getLogger();
|
||||
var requirements = listForBook(book);
|
||||
@@ -28,9 +27,9 @@ function loadForBook(book) {
|
||||
);
|
||||
});
|
||||
|
||||
return sortPlugins(installed, requirementsKeys);
|
||||
})
|
||||
.then(function(installed) {
|
||||
// Sort plugins
|
||||
installed = sortPlugins(installed, requirementsKeys);
|
||||
|
||||
// Log state
|
||||
logger.info.ln(installed.size + ' plugins are installed');
|
||||
if (requirements.size != installed.size) {
|
||||
|
||||
+19
-28
@@ -1,44 +1,35 @@
|
||||
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}
|
||||
*/
|
||||
* 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>>}
|
||||
*/
|
||||
|
||||
* 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 {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());
|
||||
// Sort plugins to match list in book.json
|
||||
return 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);
|
||||
// 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;
|
||||
return loadingOrder || definitionOrder ? -1 : 1;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user