summaryrefslogtreecommitdiffstats
path: root/lib/plugins/listAll.js
blob: 9b2a96b0905fb26aa25812033c304b3786d645f4 (plain)
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
var Immutable = require('immutable');
var Plugin = require('../models/plugin');

var pkg = require('../../package.json');
var DEFAULT_PLUGINS = require('../constants/defaultPlugins');

/**
    List all plugins for a book

    @param {List<PluginDependency>} deps
    @return {OrderedMap<Plugin>}
*/
function listAll(deps) {
    // Extract list of plugins to disable (starting with -)
    var toRemove = deps
        .filter(function(plugin) {
            return !plugin.isEnabled();
        })
        .map(function(plugin) {
            return plugin.getName();
        });

    // Convert to an ordered map of Plugin
    var plugins = deps
        .map(function(dep) {
            var plugin = Plugin.createFromDep(dep);

            return [dep.getName(), plugin];
        });
    plugins = Immutable.OrderedMap(plugins);

    // Append default plugins
    DEFAULT_PLUGINS.forEach(function(pluginName) {
        if (plugins.has(pluginName)) return;

        plugins = plugins.set(pluginName, new Plugin({
            name: pluginName,
            version: pkg.dependencies[Plugin.nameToNpmID(pluginName)]
        }));
    });

    // Remove plugins
    plugins = plugins.filterNot(function(plugin, name) {
        return toRemove.includes(name);
    });

    return plugins;
}

module.exports = listAll;