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

const { exec } = require('../utils/command');
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 + '"');

    const installerBin = resolve.sync('ied/lib/cmd.js');

    // 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 + ') with version', version);

        const npmID = plugin.getNpmID();
        const command = `${installerBin} install ${npmID}@${version}`;

        return exec(command, { cwd: installFolder });
    })
    .then(function() {
        logger.info.ok('plugin "' + name + '" installed with success');
    });
}

module.exports = installPlugin;