summaryrefslogtreecommitdiffstats
path: root/lib/plugins/sortPlugins.js
diff options
context:
space:
mode:
authorJohan Preynat <johan.preynat@gmail.com>2016-05-28 11:56:34 +0200
committerJohan Preynat <johan.preynat@gmail.com>2016-05-28 11:56:34 +0200
commit33406c308208210cfc94a25d1d5975aad6e3f557 (patch)
treef1bb28c1c7c765ea0653c5c2c56443a7eb4d6c2d /lib/plugins/sortPlugins.js
parentebe845d46c9e3378a504f9b05c71ea361e36b599 (diff)
parentc3851889b0eba506c3138daaf4f1ef583dd608db (diff)
downloadgitbook-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/sortPlugins.js')
-rw-r--r--lib/plugins/sortPlugins.js45
1 files changed, 45 insertions, 0 deletions
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