# All files / src/cli index.js

70.5% Statements 141/200
64.89% Branches 61/94
60.87% Functions 14/23
82.24% Lines 88/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
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    4142x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x

    2x

    2x

    2x

    282x
    70x

    70x
    1x
    1x
    1x

    70x
    70x
    70x
    70x

    70x
    55x
    55x
    31x
    31x
    31x

    24x

    23x
    2x

    18x

    65x

    202x
    48x

    48x
    48x
    17x
    17x
    17x

    48x

    48x
    3x
    49x

    30x
    15x
    15x

    15x
    7x
    7x
    7x

    15x
    1x
    3x

    15x

    15x

    15x

    2x
    1x
    1x

    1x

    2x

    12x
    6x

    6x

    2x

    2x
    1x

    1x
    11x

    11x

    1x

    1x
    3x
    1x

    2x

    1x
    2x

    2x
     import chalk from 'chalk';
    import inquirer from 'inquirer';
    import { logWarning, logTask, rnvStatus, logEnd, logToSummary } from '../systemTools/logger';
    import { rnvWorkspaceList, rnvWorkspaceAdd, rnvWorkspaceConnect, rnvWorkspaceUpdate } from '../projectTools/workspace';
    import { createNewProject } from '../projectTools/projectGenerator';
    import { rnvTemplateAdd, rnvTemplateApply, rnvTemplateList } from '../templateTools';
    import { targetCreate, rnvTargetLaunch, rnvTargetList } from '../platformTools/target';
    import { rnvPluginAdd, rnvPluginList, rnvPluginUpdate, rnvLink } from '../pluginTools';
    import { rnvPlatformEject, rnvPlatformList, rnvPlatformConnect, rnvPlatformConfigure } from '../platformTools';
    import { executePipe, rnvHooksList, rnvHooksRun, rnvHooksPipes } from '../projectTools/buildHooks';
    import { rnvConfigure, rnvSwitch } from '../projectTools';
    import { rnvCryptoDecrypt, rnvCryptoEncrypt, rnvCryptoInstallCerts, rnvCryptoUpdateProfile, rnvCryptoUpdateProfiles, rnvCryptoInstallProfiles } from '../systemTools/crypto';
    import { rnvFastlane } from '../deployTools/fastlane';
    import { rnvClean } from '../systemTools/cleaner';
    import { inquirerPrompt } from '../systemTools/prompt';
    import { rnvRun, rnvBuild, rnvPackage, rnvExport, rnvLog, rnvDeploy, rnvStart } from '../platformTools/runner';
    import { SUPPORTED_PLATFORMS, IOS, ANDROID, ANDROID_TV, ANDROID_WEAR, WEB, TIZEN, TIZEN_MOBILE, TVOS,
        WEBOS, MACOS, WINDOWS, TIZEN_WATCH, KAIOS, FIREFOX_OS, FIREFOX_TV } from '../constants';
    // import { getBinaryPath } from '../common';
    import Config from '../config';

    export const rnvHelp = () => {
        let cmdsString = '';
        for (const key in COMMANDS) {
            cmdsString += `${key}, `;
        }

        logToSummary(`
    ${chalk.bold.white('COMMANDS:')}

    ${cmdsString}

    ${chalk.bold.white('OPTIONS:')}

    '-i, --info', 'Show full debug info'
    '-u, --update', 'Force update dependencies (iOS only)'
    '-p, --platform <value>', 'Select specific platform' // <ios|android|web|...>
    '-c, --appConfigID <value>', 'Select specific appConfigID' // <ios|android|web|...>
    '-t, --target <value>', 'Select specific simulator' // <.....>
    '-d, --device [value]', 'Select connected device'
    '-s, --scheme <value>', 'Select build scheme' // <Debug | Release>
    '-f, --filter <value>', 'Filter Value'
    '-l, --list', 'Return list of items related to command' // <alpha|beta|prod>
    '-r, --reset', 'Also perform reset'
    '-b, --blueprint', 'Blueprint for targets'
    '-h, --host <value>', 'Custom Host IP'
    '-x, --exeMethod <value>', 'Executable method in buildHooks'
    '-P, --port <value>', 'Custom Port'
    '-H, --help', 'Help'
    '-D, --debug', 'enable remote debugger'
    '--hosted', 'Run in a hosted environment (skip bundleAssets)'
    '--debugIp <value>', '(optional) overwrite the ip to which the remote debugger will connect'
    `);
    };

    const COMMANDS = {
        start: {
            fn: rnvStart
        },
        run: {
            desc: 'Run your app on target device or emulator',
            fn: rnvRun
        },
        package: {
            desc: 'Package JS Code',
            platforms: [IOS, ANDROID, ANDROID_TV, ANDROID_WEAR, TVOS],
            fn: rnvPackage
        },
        deploy: {
            desc: 'Deploy whole app via preconfigured or custom integration',
            fn: rnvDeploy
        },
        build: {
            desc: 'Build your app',
            fn: rnvBuild
        },
        export: {
            desc: 'Export your app (ios only)',
            platforms: [IOS, TVOS, MACOS, WINDOWS],
            fn: rnvExport
        },
        log: {
            desc: 'Attach logger to device or emulator and print out logs',
            platforms: [IOS, ANDROID, ANDROID_TV, ANDROID_WEAR, TVOS],
            fn: rnvLog
        },
        new: {
            fn: createNewProject,
            desc: 'Creates new project',
            params: ['mono', 'ci']
        },
        help: {
            desc: 'Displays help',
            fn: rnvHelp
        },
        configure: {
            desc: 'Configures app config',
            fn: rnvConfigure,
            params: ['appConfigID', 'mono', 'ci']
        },
        switch: {
            desc: 'Switches to app confing without rebuilding',
            fn: rnvSwitch,
            params: ['appConfigID', 'mono', 'ci']
        },
        link: {
            desc: 'Local dependency linking of your project',
            fn: rnvLink
        },
        platform: {
            desc: 'Manages native platform projects',
            subCommands: {
                eject: {
                    fn: rnvPlatformEject
                },
                list: {
                    fn: rnvPlatformList
                },
                connect: {
                    fn: rnvPlatformConnect
                },
                configure: {
                    fn: rnvPlatformConfigure
                }
            }
        },
        target: {
            desc: 'Manages simulators and emulators',
            platforms: [IOS, ANDROID, ANDROID_TV, ANDROID_WEAR, TIZEN, TIZEN_MOBILE, TVOS, WEBOS, TIZEN_WATCH],
            subCommands: {
                launch: {
                    fn: rnvTargetLaunch
                },
                list: {
                    fn: rnvTargetList
                }
            }
        },
        plugin: {
            desc: 'Manages all plugins',
            subCommands: {
                add: {
                    fn: rnvPluginAdd
                },
                list: {
                    fn: rnvPluginList
                },
                update: {
                    fn: rnvPluginUpdate
                }
            }
        },
        hooks: {
            desc: 'Manages project based build hooks. This allows you to extend functionality of RNV CLI',
            subCommands: {
                run: {
                    fn: rnvHooksRun
                },
                list: {
                    fn: rnvHooksList
                },
                pipes: {
                    fn: rnvHooksPipes
                }
            }
        },
        status: {
            desc: 'Prints out summary of your project',
            fn: rnvStatus
        },
        clean: {
            desc: 'Automatically removes all node_modules and lock in your project and its dependencies',
            fn: rnvClean
        },
        template: {
            desc: 'Manages rnv and project templates',
            subCommands: {
                add: {
                    fn: rnvTemplateAdd
                },
                list: {
                    fn: rnvTemplateList
                },
                apply: {
                    fn: rnvTemplateApply
                }
            }
        },
        crypto: {
            desc: 'Utility to manage encrytped files in your project, provisioning profiles, kestores and other sensitive information',
            platforms: [IOS, TVOS],
            subCommands: {
                encrypt: {
                    fn: rnvCryptoEncrypt
                },
                decrypt: {
                    fn: rnvCryptoDecrypt
                },
                installCerts: {
                    fn: rnvCryptoInstallCerts
                },
                updateProfile: {
                    requiredParams: ['scheme', 'platform'],
                    fn: rnvCryptoUpdateProfile
                },
                updateProfiles: {
                    fn: rnvCryptoUpdateProfiles
                },
                installProfiles: {
                    fn: rnvCryptoInstallProfiles
                }
            }
        },
        workspace: {
            desc: 'Manages global workspaces for ReNative projects',
            subCommands: {
                add: {
                    fn: rnvWorkspaceAdd
                },
                connect: {
                    fn: rnvWorkspaceConnect
                },
                list: {
                    fn: rnvWorkspaceList
                },
                update: {
                    fn: rnvWorkspaceUpdate
                }
            }
        },
        fastlane: {
            desc: 'Run fastlane commands on currectly active app/platform directly via rnv command',
            platforms: [IOS, ANDROID, ANDROID_TV, ANDROID_WEAR, TVOS],
            fn: rnvFastlane
        }
    };

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

    const run = async (c, spawnC) => {
        logTask('cli');

        let oldC;
        if (spawnC) {
            oldC = c;
            c = _spawnCommand(c, spawnC);
            Config.initializeConfig(c);
        }

        const cmd = COMMANDS[c.command];
        const cmdFn = cmd?.fn;
        const subCmd = cmd?.subCommands?.[c.subCommand];
        const subCmdFn = subCmd?.fn;I

        if (cmd) {
            if (c.subCommand === 'help') {
                await _execCommandHep(c, cmd);
            } else if (cmdFn) {E
                if (subCmdFn) {
                    await _execute(c, subCmdFn, cmd, c.command, c.subCommand);
                } else {
                    await _execute(c, cmdFn, cmd, c.command, c.subCommand);
                }
            } else if (subCmdFn) {
                await _execute(c, subCmdFn, cmd, c.command, c.subCommand);
            } else {
                await _handleUnknownSubCommand(c);
            }
        } else {
            await _handleUnknownCommand(c);
        }
        if (spIawnC) Config.initializeConfig(oldC);
    };

    const _execute = async (c, cmdFn, cmd, command, subCommand) => {
        if (cmd.platforms && !cmd.platforms.includes(c.platform)) {
            await _handleUnknownPlatform(c, cmd.platforms);
            return;
        }
        let subCmd = '';
        if (subCommand) {
            subCmd = `:${c.subCommand}`;
            const requiredParams = cmd.subCommands[c.subCommand]?.requiredParams;
            if (IrequiredParams) {
                for (let i = 0; i < requiredParams.length; i++) {
                    const requiredParam = requiredParams[i];
                    // TODO
                }
            }
        }

        await executePipe(c, `${c.command}${subCmd}:before`);
        await cmdFn(c);
        await executePipe(c, `${c.command}${subCmd}:after`);
    };

    // ##########################################
    // PRIVATE API
    // ##########################################

    const _execCommandHep = async (c, cmd) => {
        let opts = '';
        let subCommands = '';

        if (cmd.subCommands) {
            subCommands = '\nSub Commands: \n';
            subCommands += Object.keys(cmd.subCommands).join(', ');
            subCommands += '\n';
        }

        if (cmd.params) {
            opts = 'Options:\n';
            opts += (cmd.params || []).reduce((t, v) => `${t}--${v}\n`, '');
        }

        logToSummary(`
    Command: ${c.command}

    Description: ${cmd.desc}.
    ${subCommands}
    ${opts}
    More info at ${chalk.grey(`https://renative.org/docs/rnv-${c.command}`)}
    `);
        return Promise.resolve();
    };

    const _handleUnknownSubCommand = async (c) => {
        logTask('_handleUnknownSubCommand');
        const cmds = COMMANDS[c.command]?.subCommands;

        const { subCommand } = await inquirerPrompt({
            type: 'list',
            name: 'subCommand',
            message: 'Pick a subCommand',
            choices: Object.keys(cmds),
            logMessage: `cli: Command ${chalk.bold(c.command)} does not support method ${chalk.bold(c.subCommand)}!`
        });

        c.subCommand = subCommand;
        return run(c);
    };

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

        c.program.scheme = '?';
        c.program.appConfigID = '?';
        c.runtime.appId = '?';

        const { command } = await inquirerPrompt({
            type: 'list',
            name: 'command',
            message: 'Pick a command',
            choices: Object.keys(COMMANDS),
            logMessage: `cli: Command ${chalk.bold(c.command)} not supported!`
        });
        c.command = command;
        return run(c);
    };

    const _handleUnknownPlatform = async (c, platforms) => {
        logTask('_handleUnknownPlatform');
        const { platform } = await inquirerPrompt({
            type: 'list',
            name: 'platform',
            message: 'pick one of the following',
            choices: platforms,
            logMessage: `cli: Command ${chalk.grey(c.command)} does not support platform ${chalk.grey(c.platform)}. `
        });

        c.platform = platform;
        return run(c);
    };

    const _arrayMergeOverride = (destinationArray, sourceArray, mergeOptions) => sourceArray;

    export const _spawnCommand = (c, overrideParams) => {
        const newCommand = {};

        Object.keys(c).forEach((k) => {
        I    if (typeof newCommand[k] === 'object' && !(newCommand[k] instanceof 'String')) {
                newCommand[k] = { ...c[k] };
            } else {
                newCommand[k] = c[k];
            }
        });

        const merge = require('deepmerge');

        Object.keys(overrideParams).forEach((k) => {
            if (newCommand[k] && typeof overrideParams[k] === 'object') {
                newCommand[k] = merge(newCommand[k], overrideParams[k], { arrayMerge: _arrayMergeOverride });
            } else {
                newCommand[k] = overrideParams[k];
            }
        });

        // This causes stack overflow on Linux
        // const merge = require('deepmerge');
        // const newCommand = merge(c, overrideParams, { arrayMerge: _arrayMergeOverride });
        return newCommand;
    };

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

    export default run;