# All files / src/deployTools ftp.js

15.69% Statements 16/102
0% Branches 0/56
0% Functions 0/18
16.42% Lines 11/67

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
    1712x
    2x
    2x

    2x

    2x
    2x

    2x

    2x

    2x

    2x

    2x

     import path from 'path';
    import fs from 'fs';
    import inquirer from 'inquirer';

    import {
        logInfo,
        logTask,
    } from '../systemTools/logger';
    import { writeObjectSync } from '../systemTools/fileutils';
    import { DEPLOY_TARGET_FTP } from './webTools';

    const _deployToFtp = (c, platform) => new Promise((resolve, reject) => {
        logTask(`_deployToFtp:${platform}`);
        let promise;
        const envPath = path.resolve(c.paths.project.dir, '.env');
        if (!fs.existsSync(envPath)) {
            logInfo('.env file does not exist. Creating one for you');
            promise = _createEnvFtpConfig(envPath);
        } else {
            promise = new Promise((resolve, reject) => {
                fs.readFile(envPath, (err, data) => {
                    if (err) return reject(err);
                    resolve(data.toString());
                });
            });
        }
        promise.then((envContent) => {
            let matches = 0;
            const targetMatches = 2;
            envContent.split('\n').map(line => line.split('=')).forEach(([key, val]) => {
                if (['RNV_DEPLOY_WEB_FTP_SERVER', 'RNV_DEPLOY_WEB_FTP_USER'].indexOf(key) > -1) {
                    matches++;
                }
            });
            let envPromise;
            if (matches >= targetMatches) {
                envPromise = Promise.resolve();
            } else {
                logInfo('.env file does not contain all needed FTP config, helping you to set it up');
                envPromise = _createEnvFtpConfig(envPath, `${envContent}\n`);
            }
            return envPromise;
        })
            .then(() => {
                require('dotenv').config();
                const config = {
                    user: process.env.RNV_DEPLOY_WEB_FTP_USER,
                    password: process.env.RNV_DEPLOY_WEB_FTP_PASSWORD, // optional, prompted if none given
                    host: process.env.RNV_DEPLOY_WEB_FTP_SERVER,
                    port: process.env.RNV_DEPLOY_WEB_FTP_PORT || 21,
                    localRoot: c.buildConfig.platforms[platform].deploy[DEPLOY_TARGET_FTP].localRoot,
                    remoteRoot: c.buildConfig.platforms[platform].deploy[DEPLOY_TARGET_FTP].remoteRoot || '/',
                    include: c.buildConfig.platforms[platform].deploy[DEPLOY_TARGET_FTP].include || ['*', '**/*'], // this would upload everything except dot files
                    exclude: c.buildConfig.platforms[platform].deploy[DEPLOY_TARGET_FTP].exclude || [], // e.g. exclude sourcemaps - ** exclude: [] if nothing to exclude **
                    deleteRemote: c.buildConfig.platforms[platform].deploy[DEPLOY_TARGET_FTP].exclude.deleteRemote || false, // delete ALL existing files at destination before uploading, if true
                    forcePasv: true // Passive mode is forced (EPSV command is not sent)
                };
                return config;
            })
            .then((config) => {
                const FtpDeploy = require('ftp-deploy');
                const ftpDeploy = new FtpDeploy();
                return ftpDeploy.deploy(config);
            }).catch(reject);
    });

    const _createEnvFtpConfig = async (configFilePath, previousContent = '') => {
        let envContent = previousContent || '';

        const { host, user, password, port } = await inquirer.prompt([
            {
                name: 'host',
                type: 'input',
                message: 'Type your FTP host',
                validate: i => !!i || 'No FTP server provided'
            },
            {
                name: 'port',
                type: 'number',
                message: 'Type your FTP port',
                default: 21,
                validate: i => !!i || 'No FTP server provided'
            },
            {
                name: 'user',
                message: 'Type your FTP user',
                type: 'input',
                validate: i => !!i || 'No FTP user provided'
            },
            {
                name: 'password',
                message: 'Type your FTP password (or press ENTER for prompting every time)',
                type: 'password',
            },
        ]);

        envContent += `RNV_DEPLOY_WEB_FTP_SERVER=${host}\n`;
        envContent += `RNV_DEPLOY_WEB_FTP_USER=${user}\n`;
        envContent += `RNV_DEPLOY_WEB_FTP_PASSWORD=${password}\n`;
        envContent += `RNV_DEPLOY_WEB_FTP_PORT=${port}`;

        fs.writeFileSync(configFilePath, envContent);
        logInfo(`Writing .env config to ${configFilePath}`);
    };

    const _createDeployConfig = async (c, platform) => {
        logTask(`_createDeployConfig:${platform}`);

        const deploy = c.buildConfig.platforms[platform].deploy || {};

        deploy[DEPLOY_TARGET_FTP] = {};
        deploy[DEPLOY_TARGET_FTP].type = DEPLOY_TARGET_FTP;

        deploy[DEPLOY_TARGET_FTP].localRoot = path.resolve(c.paths.project.builds.dir, `${c.runtime.appId}_${platform}`);
        const { remoteRoot, deleteRemote, include, exclude, excludeSourcemaps } = await inquirer.prompt([
            {
                name: 'remoteRoot',
                type: 'input',
                message: 'Folder on the ftp to upload the project',
                default: '/',
            },
            {
                name: 'deleteRemote',
                type: 'confirm',
                message: 'Delete all contents of that folder when deploying versions?',
            },
            {
                name: 'include',
                type: 'input',
                message: 'Included files pattern, comma separated',
                default: '\'*\',\'**/*\''
            },
            {
                name: 'exclude',
                type: 'input',
                message: 'Excluded files pattern, comma separated',
                default: '[]'
            },
            {
                name: 'excludeSourcemaps',
                type: 'confirm',
                message: 'Exclude sourcemaps?',
            },
        ]);

        deploy[DEPLOY_TARGET_FTP].remoteRoot = remoteRoot || '/';
        deploy[DEPLOY_TARGET_FTP].deleteRemote = deleteRemote;
        deploy[DEPLOY_TARGET_FTP].include = include ? include.split(',') : ['*', '**/*'];
        deploy[DEPLOY_TARGET_FTP].exclude = exclude ? exclude.split(',') : [];
        deploy[DEPLOY_TARGET_FTP].exclude = deploy[DEPLOY_TARGET_FTP].exclude.concat(excludeSourcemaps ? ['**/*.map'] : []);

        logInfo(`Setting your appconfig for ${platform} to include deploy type: ${DEPLOY_TARGET_FTP}
                        on ${c.paths.appConfig.config}
                    `);

        // TODO: Review this (where to put what props renative.*.json)
        c.files.appConfig.config.platforms[platform].deploy = deploy;
        writeObjectSync(c.paths.appConfig.config, c.files.appConfig.config);
    };

    const deployToFtp = (c, platform) => {
        logTask(`checkDeployConfigTarget:${platform}`);
        const targetConfig = c.buildConfig.platforms[platform];
        if (targetConfig?.deploy?.[DEPLOY_TARGET_FTP]?.type) {
            return _deployToFtp(c, platform);
        }
        return _createDeployConfig(c, platform).then(() => _deployToFtp(c, platform));
    };

    export { deployToFtp };