forked from GitbookIO/gitbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadForBook.js
73 lines (59 loc) · 2.05 KB
/
loadForBook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var Immutable = require('immutable');
var Promise = require('../utils/promise');
var listDepsForBook = require('./listDepsForBook');
var findForBook = require('./findForBook');
var loadPlugin = require('./loadPlugin');
/**
* Load all plugins in a book
*
* @param {Book}
* @return {Promise<Map<String:Plugin>}
*/
function loadForBook(book) {
var logger = book.getLogger();
// List the dependencies
var requirements = listDepsForBook(book);
// 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);
}
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(installedMap.size + ' plugins are installed');
if (requirements.size != installedMap.size) {
logger.info.ln(requirements.size + ' explicitly listed');
}
// Verify that all plugins are present
if (missing.length > 0) {
throw new Error('Couldn\'t locate plugins "' + missing.join(', ') + '", Run \'gitbook install\' to install plugins from registry.');
}
return Promise.map(plugins, function(plugin) {
return loadPlugin(book, plugin);
});
});
}
module.exports = loadForBook;