# All files / src/platformTools index.js

33.73% Statements 56/166
12.5% Branches 8/64
33.33% Functions 10/30
29.91% Lines 32/107

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
    1912x

    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x

    2x
    1x

    2x

    3x
    2x
    2x

    2x

    2x

    2x
    1x

    2x

    2x
    1x

    2x
    1x

    2x

    2x

    2x

    2x

    2x

    2x
     /* eslint-disable import/no-cycle */
    // @todo fix cycle dep
    import chalk from 'chalk';
    import path from 'path';
    import inquirer from 'inquirer';
    import {
        isPlatformSupportedSync, isPlatformSupported
    } from '../common';
    import { logToSummary, logTask, logSuccess } from '../systemTools/logger';
    import { generateOptions } from '../systemTools/prompt';
    import { cleanFolder, copyFolderContentsRecursiveSync, writeObjectSync, removeDirs } from '../systemTools/fileutils';
    import { executePipe } from '../projectTools/buildHooks';
    import { cleanPlaformAssets } from '../projectTools/projectParser';
    import { PLATFORMS } from '../constants';

    export const rnvPlatformList = c => new Promise((resolve, reject) => {
        const opts = _genPlatOptions(c);
        logToSummary(`Platforms:\n\n${opts.asString}`);
        resolve();
    });

    export const rnvPlatformConfigure = async (c) => {
        c.runtime.platform = c.program.platform || 'all';
        logTask(`rnvPlatformConfigure:${c.runtime.platform}`);

        await isPlatformSupported(c);
        await cleanPlatformBuild(c, c.runtime.platform);
        await cleanPlaformAssets(c, c.runtime.platform);
        await _runCopyPlatforms(c, c.runtime.platform);
    };

    const _generatePlatformChoices = c => c.buildConfig.defaults.supportedPlatforms.map((platform) => {
        const isConnected = c.paths.project.platformTemplatesDirs[platform].includes(c.paths.rnv.platformTemplates.dir);
        return { name: `${platform} - ${isConnected ? chalk.green('(connected)') : chalk.yellow('(ejected)')}`, value: platform, isConnected };
    });

    export const rnvPlatformEject = async (c) => {
        logTask('rnvPlatformEject');

        const { ejectedPlatforms } = await inquirer.prompt({
            name: 'ejectedPlatforms',
            message: 'This will copy platformTemplates folders from ReNative managed directly to your project Select platforms you would like to connect',
            type: 'checkbox',
            choices: _generatePlatformChoices(c).map(choice => ({ ...choice, disabled: !choice.isConnected }))
        });

        if (ejectedPlatforms.length) {
            const ptfn = 'platformTemplates';
            const rptf = c.paths.rnv.platformTemplates.dir;
            const prf = c.paths.project.dir;

            let copyShared = false;

            ejectedPlatforms.forEach((platform) => {
                if (PLATFORMS[platform].requiresSharedConfig) {
                    copyShared = true;
                }

                copyFolderContentsRecursiveSync(path.join(rptf, platform), path.join(prf, ptfn, platform));

                if (copyShared) {
                    copyFolderContentsRecursiveSync(path.join(rptf, '_shared'), path.join(prf, ptfn, '_shared'));
                }

                c.files.project.config.platformTemplatesDirs = c.files.project.config.platformTemplatesDirs || {};
                c.files.project.config.platformTemplatesDirs[platform] = `./${ptfn}`;

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

        logSuccess(
            `${chalk.white(ejectedPlatforms.join(','))} platform templates are located in ${chalk.white(
                c.files.project.config.platformTemplatesDirs[ejectedPlatforms[0]]
            )} now. You can edit them directly!`
        );
    };

    const _genPlatOptions = (c) => {
        const opts = generateOptions(c.buildConfig.defaults.supportedPlatforms, true, null, (i, obj, mapping, defaultVal) => {
            const isEjected = c.paths.project.platformTemplatesDirs[obj].includes(c.paths.rnv.platformTemplates.dir) ? chalk.green('(connected)') : chalk.yellow('(ejected)');
            return ` [${chalk.white(i + 1)}]> ${chalk.bold(defaultVal)} - ${isEjected} \n`;
        });
        return opts;
    };

    export const rnvPlatformConnect = async (c) => {
        logTask('rnvPlatformConnect');

        const { connectedPlatforms } = await inquirer.prompt({
            name: 'connectedPlatforms',
            message: 'This will point platformTemplates folders from your local project to ReNative managed one. Select platforms you would like to connect',
            type: 'checkbox',
            choices: _generatePlatformChoices(c).map(choice => ({ ...choice, disabled: choice.isConnected }))
        });

        if (connectedPlatforms.length) {
            connectedPlatforms.forEach((platform) => {
                if (c.files.project.config.platformTemplatesDirs?.[platform]) {
                    delete c.files.project.config.platformTemplatesDirs[platform];
                }

                if (!Object.keys(c.files.project.config.platformTemplatesDirs).length) {
                    delete c.files.project.config.platformTemplatesDirs; // also cleanup the empty object
                }

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

        const { deletePlatformFolder } = await inquirer.prompt({
            name: 'deletePlatformFolder',
            type: 'confirm',
            message: 'Would you also like to delete the previously used platform folder?'
        });

        if (deletePlatformFolder) {
            const pathsToRemove = [];
            connectedPlatforms.forEach((platform) => {
                pathsToRemove.push(path.join(c.paths.project.platformTemplatesDirs[platform], platform));
            });

            // TODO: Remove shared folders as well

            await removeDirs(pathsToRemove);
        }

        logSuccess(
            `${chalk.white(connectedPlatforms.join(','))} now using ReNative platformTemplates located in ${chalk.white(c.paths.rnv.platformTemplates.dir)} now!`
        );
    };

    const _runCopyPlatforms = (c, platform) => new Promise((resolve, reject) => {
        logTask(`_runCopyPlatforms:${platform}`);
        const copyPlatformTasks = [];
        if (platform === 'all') {
            for (const k in c.buildConfig.platforms) {
                if (isPlatformSupportedSync(k)) {
                    const ptPath = path.join(c.paths.project.platformTemplatesDirs[k], `${k}`);
                    const pPath = path.join(c.paths.project.builds.dir, `${c.runtime.appId}_${k}`);
                    copyPlatformTasks.push(copyFolderContentsRecursiveSync(ptPath, pPath));
                }
            }
        } else if (isPlatformSupportedSync(platform)) {
            const ptPath = path.join(c.paths.project.platformTemplatesDirs[platform], `${platform}`);
            const pPath = path.join(c.paths.project.builds.dir, `${c.runtime.appId}_${platform}`);
            copyPlatformTasks.push(copyFolderContentsRecursiveSync(ptPath, pPath));
        } else {
            logWarning(`Your platform ${chalk.white(platform)} config is not present. Check ${chalk.white(c.paths.appConfig.config)}`);
        }

        Promise.all(copyPlatformTasks).then((values) => {
            resolve();
        });
    });

    export const cleanPlatformBuild = (c, platform) => new Promise((resolve, reject) => {
        logTask(`cleanPlatformBuild:${platform}`);

        const cleanTasks = [];

        if (platform === 'all') {
            for (const k in c.buildConfig.platforms) {
                if (isPlatformSupportedSync(k)) {
                    const pPath = path.join(c.paths.project.builds.dir, `${c.runtime.appId}_${k}`);
                    cleanTasks.push(cleanFolder(pPath));
                }
            }
        } else if (isPlatformSupportedSync(platform)) {
            const pPath = path.join(c.paths.project.builds.dir, `${c.runtime.appId}_${platform}`);
            cleanTasks.push(cleanFolder(pPath));
        }

        Promise.all(cleanTasks).then((values) => {
            resolve();
        });
    });

    export const createPlatformBuild = (c, platform) => new Promise((resolve, reject) => {
        logTask(`createPlatformBuild:${platform}`);

        if (!isPlatformSupportedSync(platform, null, reject)) return;

        const pPath = path.join(c.paths.project.builds.dir, `${c.runtime.appId}_${platform}`);
        const ptPath = path.join(c.paths.project.platformTemplatesDirs[platform], `${platform}`);
        copyFolderContentsRecursiveSync(ptPath, pPath, false, [path.join(ptPath, '_privateConfig')]);

        resolve();
    });