summaryrefslogtreecommitdiffstats
path: root/packages/gitbook/src/plugins/installPlugin.js
blob: c1f1f2270a04c0b6a8a257443b19aa09c0bfa2f4 (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
const npmi = require('npmi');

const Promise = require('../utils/promise');
const resolveVersion = require('./resolveVersion');

/**
 * Install a plugin for a book
 *
 * @param {Book} book
 * @param {PluginDependency} plugin
 * @return {Promise}
 */
function installPlugin(book, plugin) {
    const logger = book.getLogger();

    const installFolder = book.getRoot();
    const name = plugin.getName();
    const requirement = plugin.getVersion();

    logger.info.ln('');
    logger.info.ln('installing plugin "' + name + '"');

    // Find a version to install
    return resolveVersion(plugin)
    .then(function(version) {
        if (!version) {
            throw new Error('Found no satisfactory version for plugin "' + name + '" with requirement "' + requirement + '"');
        }

        logger.info.ln('install plugin "' + name + '" (' + requirement + ') from NPM with version', version);
        return Promise.nfcall(npmi, {
            'name': plugin.getNpmID(),
            version,
            'path': installFolder,
            'npmLoad': {
                'loglevel': 'silent',
                'loaded': true,
                'prefix': installFolder
            }
        });
    })
    .then(function() {
        logger.info.ok('plugin "' + name + '" installed with success');
    });
}

module.exports = installPlugin;