summaryrefslogtreecommitdiffstats
path: root/lib/output/website/prepareResources.js
blob: 4e6835d9a3df60173d06063c510a6cb40e400dbb (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
var is        = require('is');
var Immutable = require('immutable');
var Promise   = require('../../utils/promise');

var Api              = require('../../api');

/**
    Prepare plugins resources, add all output corresponding type resources

    @param {Output}
    @return {Promise<Output>}
*/
function prepareResources(output) {
    var plugins = output.getPlugins();
    var options = output.getOptions();
    var type    = options.get('prefix');
    var state   = output.getState();
    var context = Api.encodeGlobal(output);

    var result = Immutable.Map();

    return Promise.forEach(plugins, function(plugin) {
        var pluginResources = plugin.getResources(type);

        return Promise()
        .then(function() {
            // Apply resources if is a function
            if (is.fn(pluginResources)) {
                return Promise()
                .then(pluginResources.bind(context));
            }
            else {
                return pluginResources;
            }
        })
        .then(function(resources) {
            result = result.set(plugin.getName(), Immutable.Map(resources));
        });
    })
    .then(function() {
        // Set output resources
        state = state.merge({
            resources: result
        });

        output = output.merge({
            state: state
        });

        return output;
    });
}

module.exports = prepareResources;