All files / src/deployTools webTools.js

36.62% Statements 26/71
0% Branches 0/41
0% Functions 0/7
45.24% Lines 19/42

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 1002x 2x 2x 2x   2x 2x 2x             2x   2x 2x 2x 2x   2x                                   2x                       2x                                               2x         2x     2x           2x                    
import chalk from 'chalk';
import minimist from 'minimist';
import inquirer from 'inquirer';
import path from 'path';
 
import { deployToNow } from './now';
import { deployToFtp } from './ftp';
import {
    logTask,
    logComplete,
    logError,
    logInfo,
    importPackageFromProject
} from '../common';
import { configureDeploymentIfRequired, configureExportIfRequired } from './configure';
 
const DEPLOY_TARGET_DOCKER = 'docker';
const DEPLOY_TARGET_FTP = 'ftp';
const DEPLOY_TARGET_NOW = 'now';
const DEPLOY_TARGET_NONE = 'none';
 
const _runDeployment = async (c, platform, deployType) => {
    switch (deployType) {
    case DEPLOY_TARGET_FTP:
        return deployToFtp(c, platform);
    case DEPLOY_TARGET_NOW:
        return deployToNow(c, platform);
    case DEPLOY_TARGET_NONE:
        return Promise.resolve();
    case DEPLOY_TARGET_DOCKER:
        const rnvPath = process.mainModule.filename.split('/bin/index.js')[0];
        const deployToDocker = importPackageFromProject('@rnv/deploy-docker');
        deployToDocker.setRNVPath(rnvPath);
        return deployToDocker.doDeploy();
    default:
        return Promise.reject(new Error(`Deploy Type not supported ${deployType}`));
    }
};
 
const _runExport = (c, platform, deployType) => {
    switch (deployType) {
    case DEPLOY_TARGET_DOCKER:
        const rnvPath = process.mainModule.filename.split('/bin/index.js')[0];
        const deployToDocker = importPackageFromProject('@rnv/deploy-docker');
        deployToDocker.setRNVPath(rnvPath);
        return deployToDocker.doExport();
    default:
        return Promise.reject(new Error(`Deploy Type not supported ${deployType}`));
    }
};
 
const selectToolAndExecute = async ({
    c, platform, choices, configFunction, executeFunction, isDeploy = true
}) => {
    const argv = minimist(c.process.argv.slice(2));
    const type = argv.t;
    const targetConfig = c.buildConfig.platforms[platform];
 
    if (type || (targetConfig && targetConfig.deploy && targetConfig.deploy.type)) {
        await configFunction(type || targetConfig.deploy.type);
        return executeFunction(c, platform, type || targetConfig.deploy.type);
    }
    const { selectedTarget } = await inquirer.prompt({
        name: 'selectedTarget',
        type: 'list',
        choices,
        message: `Which type of ${isDeploy ? 'deploy' : 'export'} option would you like to use for ${chalk.white(platform)}?`
    });
 
    await configFunction(selectedTarget);
 
    logInfo(`Setting your appconfig for ${chalk.white(platform)} to include ${isDeploy ? 'deploy' : 'export'} type: ${chalk.white(selectedTarget)} at ${chalk.white(c.paths.appConfig.config)}`);
    return executeFunction(c, platform, selectedTarget);
};
 
const selectWebToolAndDeploy = (c, platform) => selectToolAndExecute({
    c,
    platform,
    choices: [DEPLOY_TARGET_DOCKER, DEPLOY_TARGET_FTP, DEPLOY_TARGET_NOW, DEPLOY_TARGET_NONE],
    configFunction: configureDeploymentIfRequired,
    executeFunction: _runDeployment
});
 
const selectWebToolAndExport = (c, platform) => selectToolAndExecute({
    c,
    platform,
    choices: [DEPLOY_TARGET_DOCKER],
    configFunction: configureExportIfRequired,
    executeFunction: _runExport,
    isDeploy: false
});
 
export {
    selectWebToolAndDeploy,
    selectWebToolAndExport,
    DEPLOY_TARGET_FTP,
    DEPLOY_TARGET_NOW,
    DEPLOY_TARGET_NONE
};