# All files / src/cli plugin.js

51.61% Statements 64/124
35.56% Branches 16/45
40.91% Functions 9/22
53.85% Lines 56/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    2001x

    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x

    1x

    1x

    1x
    1x

    1x

    1x
    1x
    1x

    1x
    1x

    1x

    1x

    1x

    1x
    1x
    1x

    1x

    1x
    78x

    78x
    78x
    1170x

    78x
    78x
    78x
    78x

    78x
    78x
    78x
    78x

    78x

    78x
    14721x
    14721x
    14721x
    14721x
    8897x
    5824x
    5824x

    14721x

    1x

    1x

    1x

    1x

    1x
     /* eslint-disable import/no-cycle */
    // @todo fix cycle
    import chalk from 'chalk';
    import inquirer from 'inquirer';
    import ora from 'ora';
    import { SUPPORTED_PLATFORMS } from '../constants';
    import {
        logTask,
        logSuccess,
    } from '../common';
    import { executePipe } from '../projectTools/buildHooks';
    import { writeObjectSync } from '../systemTools/fileutils';

    const LIST = 'list';
    const ADD = 'add';
    const UPDATE = 'update';

    const PIPES = {
        PLUGIN_LIST_BEFORE: 'plugin:list:before',
        PLUGIN_LIST_AFTER: 'plugin:list:after',
        PLUGIN_ADD_BEFORE: 'plugin:add:before',
        PLUGIN_ADD_AFTER: 'plugin:add:after',
        PLUGIN_UPDATE_BEFORE: 'plugin:update:before',
        PLUGIN_UPDATE_AFTER: 'plugin:update:after',
    };

    // ##########################################
    // PUBLIC API
    // ##########################################

    const run = (c) => {
        logTask('run');

        switch (c.subCommand) {
        case LIST:
            return executePipe(c, PIPES.PLUGIN_LIST_BEFORE)
                .then(() => _runList(c))
                .then(() => executePipe(c, PIPES.PLUGIN_LIST_AFTER));
        case ADD:
            return executePipe(c, PIPES.PLUGIN_ADD_BEFORE)
                .then(() => _runAdd(c))
                .then(() => executePipe(c, PIPES.PLUGIN_ADD_AFTER));
        case UPDATE:
            return executePipe(c, PIPES.PLUGIN_UPDATE_BEFORE)
                .then(() => _runUpdate(c))
                .then(() => executePipe(c, PIPES.PLUGIN_UPDATE_AFTER));
        default:
            return Promise.reject(`cli:plugin: Sub-Command ${chalk.white.bold(c.subCommand)} not supported!`);
        }
    };

    // ##########################################
    // PRIVATE
    // ##########################################

    const _runList = c => new Promise((resolve) => {
        logTask('_runList');

        const o = _getPluginList(c);

        console.log(o.asString);

        resolve();
    });

    const _getPluginList = (c, isUpdate = false) => {
        const { plugins } = c.files.rnv.pluginTemplates.config;
        const output = {
            asString: '',
            asArray: [],
            plugins: [],
            json: plugins,
        };

        let i = 1;

        Object.keys(plugins).forEach((k) => {
            const p = plugins[k];

            let platforms = '';
            SUPPORTED_PLATFORMS.forEach((v) => {
                if (p[v]) platforms += `${v}, `;
            });
            if (platforms.length) platforms = platforms.slice(0, platforms.length - 2);
            const installedPlugin = c.buildConfig && c.buildConfig.plugins && c.buildConfig.plugins[k];
            const installedString = installedPlugin ? chalk.red('installed') : chalk.green('not installed');
        I    if (isUpdate && installedPlugin) {
                output.plugins.push(k);
                let versionString;
                if (installedPlugin.version !== p.version) {
                    versionString = `(${chalk.red(installedPlugin.version)}) => (${chalk.green(p.version)})`;
                } else {
                    versionString = `(${chalk.green(installedPlugin.version)})`;
                }
                output.asString += `-[${i}] ${chalk.white(k)} ${versionString}\n`;
                output.asArray.push({ name: `${k} ${versionString}`, value: k });
                i++;
            } eElse if (!isUpdate) {
                output.plugins.push(k);
                output.asString += `-[${i}] ${chalk.white(k)} (${chalk.blue(p.version)}) [${platforms}] - ${installedString}\n`;
                output.asArray.push({ name: `${k} (${chalk.blue(p.version)}) [${platforms}] - ${installedString}`, value: k });

                i++;
            }
            output.asArray.sort((a, b) => {
                const aStr = a.name.toLowerCase();
                const bStr = b.name.toLowerCase();
                let com = 0;
                if (aStr > bStr) {
                    com = 1;
                }E else if (aStr < bStr) {
                    com = -1;
                }
                return com;
            });
        });

        return output;
    };

    const _runAdd = async (c) => {
        logTask('_runAdd');

        const o = _getPluginList(c);

        const { plugins } = await inquirer.prompt({
            name: 'plugins',
            type: 'rawlist',
            message: 'Select the plugins you want to add',
            choices: o.asArray,
            pageSize: 100
        });

        const installMessage = [];

        if (plugins.length) {
            const selectedPlugins = {};
            plugins.forEach((plugin) => {
                selectedPlugins[plugin] = o.json[plugin];
                installMessage.push(`${chalk.white(plugin)} v(${chalk.green(o.json[plugin].version)})`);
            });

            const spinner = ora(`Installing: ${installMessage.join(', ')}`).start();

            Object.keys(selectedPlugins).forEach((key) => {
                // c.buildConfig.plugins[key] = 'source:rnv';
                c.files.project.config.plugins[key] = 'source:rnv';

                // c.buildConfig.plugins[key] = selectedPlugins[key];
                _checkAndAddDependantPlugins(c, selectedPlugins[key]);
            });

            writeObjectSync(c.paths.project.config, c.files.project.config);
            spinner.succeed('All plugins installed!');
            logSuccess('Plugins installed successfully!');
        }
    };

    const _checkAndAddDependantPlugins = (c, plugin) => {
        const templatePlugins = c.files.rnv.pluginTemplates.config.plugins;
        if (plugin.dependsOn) {
            plugin.dependsOn.forEach((v) => {
                if (templatePlugins[v]) {
                    console.log(`Added dependant plugin ${v}`);
                    c.buildConfig.plugins[v] = templatePlugins[v];
                }
            });
        }
    };

    const _runUpdate = async (c) => {
        logTask('_runUpdate');

        const o = _getPluginList(c, true);

        console.log(o.asString);

        const { confirm } = await inquirer.prompt({
            name: 'confirm',
            type: 'confirm',
            message: 'Above installed plugins will be updated with RNV',
        });

        if (confirm) {
            const { plugins } = c.buildConfig;
            Object.keys(plugins).forEach((key) => {
                // c.buildConfig.plugins[key] = o.json[key];
                c.files.project.config.plugins[key] = o.json[key];
            });

            writeObjectSync(c.paths.project.config, c.files.project.config);

            logSuccess('Plugins updated successfully!');
        }
    };

    export { PIPES };

    export default run;