var Promise = require('../utils/promise'); var listForBook = require('./listForBook'); var listInstalledForBook = require('./listInstalledForBook'); var loadPlugin = require('./loadPlugin'); /** Load a list of plugins in a book @param {Book} @return {Promise} */ function loadForBook(book) { var logger = book.getLogger(); var requirements = listForBook(book); var requirementsKeys = requirements.keys().toList(); return listInstalledForBook(book) .then(function(installed) { // Filter out plugins not listed of first level // (aka pre-installed plugins) installed = installed.filter(function(plugin) { return ( plugin.getDepth() > 1 || requirements.has(plugin.getName()) ); }); // 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; }); // Log state logger.info.ln(installed.size + ' are installed'); if (requirements.size != installed.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.'); } return Promise.map(installed, function(plugin) { return loadPlugin(plugin); }); }); } module.exports = loadForBook;