# All files / src/pluginTools index.js

19.78% Statements 18/91
0% Branches 0/74
0% Functions 0/8
16.87% Lines 14/83

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
    1564x
    4x
    4x
    4x
    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

    4x
     import chalk from 'chalk';
    import { mergeObjects, writeObjectSync } from '../systemTools/fileutils';
    import { logTask, logWarning, logError, getConfigProp } from '../common';
    import { askQuestion, finishQuestion } from '../systemTools/prompt';
    import { versionCheck } from '../configTools/configParser';

    const getMergedPlugin = (c, key, plugins, noMerge = false) => {
        const plugin = plugins[key];
        const origPlugin = c.files.rnv.pluginTemplates.config.plugins[key];
        if (typeof plugin === 'string' || plugin instanceof String) {
            if (plugin === 'source:rnv') {
                return origPlugin;
            }
            // NOT RECOGNIZED
            logWarning(`Plugin ${key} is not recognized RNV plugin`);
            return null;
        }

        if (origPlugin) {
            const mergedPlugin = mergeObjects(c, origPlugin, plugin);
            return mergedPlugin;
        }

        return plugin;
    };

    export const configurePlugins = c => new Promise((resolve, reject) => {
        logTask('configurePlugins');

        if (!c.files.project.package.dependencies) {
            c.files.project.package.dependencies = {};
        }

        let hasPackageChanged = false;

        for (const k in c.buildConfig.plugins) {
            const { dependencies } = c.files.project.package;
            const { devDependencies } = c.files.project.package;
            const plugin = getMergedPlugin(c, k, c.buildConfig.plugins);

            if (!plugin) {
                logWarning(`Plugin with name ${
                    chalk.white(k)} does not exists in ReNative source:rnv scope. you need to define it manually here: ${
                    chalk.white(c.paths.project.builds.config)}`);
            } else if (dependencies && dependencies[k]) {
                if (plugin['no-active'] !== true && plugin['no-npm'] !== true && dependencies[k] !== plugin.version) {
                    if (k === 'renative' && c.runtime.isWrapper) {
                        logWarning('You\'re in ReNative wrapper mode. plugin renative will stay as local dep!');
                    } else {
                        logWarning(
                            `Version mismatch of dependency ${chalk.white(k)} between:
      ${chalk.white(c.paths.project.package)}: v(${chalk.red(dependencies[k])}) and
      ${chalk.white(c.paths.project.builds.config)}: v(${chalk.green(plugin.version)}).
      package.json will be overriden`
                        );

                        hasPackageChanged = true;
                        dependencies[k] = plugin.version;
                    }
                }
            } else if (devDependencies && devDependencies[k]) {
                if (plugin['no-active'] !== true && plugin['no-npm'] !== true && devDependencies[k] !== plugin.version) {
                    logWarning(
                        `Version mismatch of devDependency ${chalk.white(k)} between package.json: v(${chalk.red(
                            devDependencies[k],
                        )}) and plugins.json: v(${chalk.red(plugin.version)}). package.json will be overriden`,
                    );
                    hasPackageChanged = true;
                    devDependencies[k] = plugin.version;
                }
            } else if (plugin['no-active'] !== true && plugin['no-npm'] !== true) {
                // Dependency does not exists
                logWarning(
                    `Missing dependency ${chalk.white(k)} v(${chalk.red(
                        plugin.version,
                    )}) in package.json. package.json will be overriden`,
                );

                hasPackageChanged = true;
                dependencies[k] = plugin.version;
            }

            if (plugin && plugin.npm) {
                for (const npmKey in plugin.npm) {
                    const npmDep = plugin.npm[npmKey];
                    if (dependencies[npmKey] !== npmDep) {
                        logWarning(`Plugin ${chalk.white(k)} requires npm dependency ${chalk.white(npmKey)} .Adding missing npm dependency to you package.json`);
                        dependencies[npmKey] = npmDep;
                        hasPackageChanged = true;
                    }
                }
            }
        }

        logTask(`configurePlugins:${hasPackageChanged}`, chalk.grey);
        versionCheck(c)
            .then(() => {
                if (hasPackageChanged) {
                    writeObjectSync(c.paths.project.package, c.files.project.package);
                    c._requiresNpmInstall = true;
                }
                resolve();
            }).catch(e => reject(e));
    });

    const parsePlugins = (c, platform, pluginCallback) => {
        logTask(`parsePlugins:${platform}`);

        if (c.buildConfig && c.buildConfig) {
            const includedPlugins = getConfigProp(c, platform, 'includedPlugins', []);
            const excludedPlugins = getConfigProp(c, platform, 'excludedPlugins', []);
            if (includedPlugins) {
                const { plugins } = c.buildConfig;
                if (plugins) {
                    Object.keys(plugins).forEach((key) => {
                        if ((includedPlugins.includes('*') || includedPlugins.includes(key)) && !excludedPlugins.includes(key)) {
                            const plugin = getMergedPlugin(c, key, plugins);
                            if (plugin) {
                                const pluginPlat = plugin[platform];
                                if (pluginPlat) {
                                    if (plugin['no-active'] !== true && plugin.enabled !== false && pluginPlat.enabled !== false) {
                                        if (pluginCallback) pluginCallback(plugin, pluginPlat, key);
                                    } else {
                                        logWarning(`Plugin ${key} is marked disabled. skipping.`);
                                    }
                                }
                            }
                        }
                    });
                } else {
                    logError(`You have no plugins defined in ${chalk.white(c.paths.project.builds.config)}`);
                }
            } else {
                logWarning(`You haven't included any ${chalk.white('{ common: { includedPlugins: [] }}')} in your ${chalk.white(c.paths.appConfig.config)}. Your app might not work correctly`);
            }
        }
    };

    const getLocalRenativePlugin = () => ({
        version: 'file:./packages/renative',
        webpack: {
            modulePaths: [],
            moduleAliases: {
                renative: {
                    projectPath: 'packages/renative'
                }
            }
        }
    });

    export { getMergedPlugin, parsePlugins, getLocalRenativePlugin };

    export default { getMergedPlugin, parsePlugins, getLocalRenativePlugin };