summaryrefslogtreecommitdiffstats
path: root/lib/plugins/index.js
blob: ed3aa0ac45c0703e4aa8cc87d2b200ef9dd06b00 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var _ = require('lodash');
var path = require('path');

var Promise = require('../utils/promise');
var fs = require('../utils/fs');
var BookPlugin = require('./plugin');
var registry = require('./registry');

/*
PluginsManager is an interface to work with multiple plugins at once:
- Extract assets from plugins
- Call hooks for all plugins, etc
*/

function PluginsManager(book) {
    this.book = book;
    this.log = this.book.log;
    this.plugins = [];

    _.bindAll(this);
}

// Return count of plugins loaded
PluginsManager.prototype.count = function() {
    return _.size(this.plugins);
};

// Returns a plugin by its name
PluginsManager.prototype.get = function(name) {
    return _.find(this.plugins, {
        id: name
    });
};

// Load a plugin, or a list of plugins
PluginsManager.prototype.load = function(name) {
    var that = this;

    if (_.isArray(name)) {
        return Promise.serie(name, function(_name) {
            return that.load(_name);
        });
    }

    return Promise()

    // Initiate and load the plugin
    .then(function() {
        var plugin;

        if (!_.isString(name)) plugin = name;
        else plugin = new BookPlugin(that.book, name);

        if (that.get(plugin.id)) {
            throw new Error('Plugin "'+plugin.id+'" is already loaded');
        }


        if (plugin.isLoaded()) return plugin;
        else return plugin.load()
            .thenResolve(plugin);
    })

    // Setup the plugin
    .then(this._setup);
};

// Load all plugins from the book's configuration
PluginsManager.prototype.loadAll = function() {
    var plugins = _.pluck(this.book.config.get('plugins'), 'name');

    this.log.info.ln('installing', plugins.length, 'plugins');
    return this.load(plugins);
};

// Setup a plugin
// Register its filter, blocks, etc
PluginsManager.prototype._setup = function(plugin) {
    this.plugins.push(plugin);
};

// Install all plugins for the book
PluginsManager.prototype.install = function() {
    var that = this;
    var plugins = _.filter(this.book.config.get('plugins'), {
        isDefault: false
    });

    if (plugins.length == 0) {
        this.log.info.ln('nothing to install!');
        return Promise(0);
    }

    this.log.info.ln('installing', plugins.length, 'plugins');

    return Promise.serie(plugins, function(plugin) {
        return registry.install(that.book, plugin.name, plugin.version);
    })
    .thenResolve(plugins.length);
};

// Call a hook on all plugins to transform an input
PluginsManager.prototype.hook = function(name, input) {
    return Promise.reduce(this.plugins, function(current, plugin) {
        return plugin.hook(name, current);
    }, input);
};

// Extract all resources for a namespace
PluginsManager.prototype.resources = function(namespace) {

};

// Copy all resources for a plugin
PluginsManager.prototype.copyResources = function(namespace, outputRoot) {
    return Promise.serie(this.plugins, function(plugin) {
        return plugin.getResources(namespace)
        .then(function(resources) {
            if (!resources.assets) return;

            var input = path.resolve(plugin.root, resources.assets);
            var output = path.resolve(outputRoot, plugin.npmId);

            return fs.copyDir(input, output);
        });
    });
};

module.exports = PluginsManager;