var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.deployToFtp=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _common=require("../common");





var _prompt=require("../systemTools/prompt");
var _constants=require("../constants");
var _webTools=require("./webTools");

var _deployToFtp=function _deployToFtp(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("_deployToFtp:"+platform);
var promise;
var envPath=_path.default.resolve(c.paths.project.dir,'.env');
if(!_fs.default.existsSync(envPath)){
(0,_common.logInfo)('.env file does not exist. Creating one for you');
promise=_createEnvFtpConfig(envPath);
}else{
promise=new Promise(function(resolve,reject){
_fs.default.readFile(envPath,function(err,data){
if(err)return reject(err);
resolve(data.toString());
});
});
}
promise.then(function(envContent){
var envObj={};
var matches=0;
var targetMatches=2;
envContent.split('\n').map(function(line){return line.split('=');}).forEach(function(_ref){var _ref2=(0,_slicedToArray2.default)(_ref,2),key=_ref2[0],val=_ref2[1];
if(['RNV_DEPLOY_WEB_FTP_SERVER','RNV_DEPLOY_WEB_FTP_USER'].indexOf(key)>-1){
matches++;
}
});
var envPromise;
if(matches>=targetMatches){
envPromise=Promise.resolve();
}else{
(0,_common.logInfo)('.env file does not contain all needed FTP config, helping you to set it up');
envPromise=_createEnvFtpConfig(envPath,envContent+"\n");
}
return envPromise;
}).
then(function(){
require('dotenv').config();
var config={
user:process.env.RNV_DEPLOY_WEB_FTP_USER,
password:process.env.RNV_DEPLOY_WEB_FTP_PASSWORD,
host:process.env.RNV_DEPLOY_WEB_FTP_SERVER,
port:process.env.RNV_DEPLOY_WEB_FTP_PORT||21,
localRoot:c.buildConfig.platforms[platform].deploy[_webTools.DEPLOY_TARGET_FTP].localRoot,
remoteRoot:c.buildConfig.platforms[platform].deploy[_webTools.DEPLOY_TARGET_FTP].remoteRoot||'/',
include:c.buildConfig.platforms[platform].deploy[_webTools.DEPLOY_TARGET_FTP].include||['*','**/*'],
exclude:c.buildConfig.platforms[platform].deploy[_webTools.DEPLOY_TARGET_FTP].exclude||[],
deleteRemote:c.buildConfig.platforms[platform].deploy[_webTools.DEPLOY_TARGET_FTP].exclude.deleteRemote||false,
forcePasv:true};

return config;
}).
then(function(config){
var FtpDeploy=require('ftp-deploy');
var ftpDeploy=new FtpDeploy();
return ftpDeploy.deploy(config);
}).catch(reject);
});};

var _createEnvFtpConfig=function _createEnvFtpConfig(configFilePath){var previousContent=arguments.length>1&&arguments[1]!==undefined?arguments[1]:'';return new Promise(function(resolve,reject){
var envContent=previousContent||'';
(0,_prompt.askQuestion)('Type your FTP host').then(function(v){
(0,_prompt.finishQuestion)();
if(v){
envContent+="RNV_DEPLOY_WEB_FTP_SERVER="+v+"\n";
}else{
return reject(new Error('NO FTP SERVER PROVIDED'));
}
(0,_prompt.askQuestion)('Type your FTP user').then(function(v){
(0,_prompt.finishQuestion)();
if(v){
envContent+="RNV_DEPLOY_WEB_FTP_USER="+v+"\n";
}else{
return reject(new Error('NO FTP USER PROVIDED'));
}
(0,_prompt.askQuestion)('Type your FTP password (or press ENTER for prompting every time)').then(function(v){
(0,_prompt.finishQuestion)();
if(v){
envContent+="RNV_DEPLOY_WEB_FTP_PASSWORD="+v+"\n";
}
(0,_prompt.askQuestion)('Type your FTP port (or enter for 21)').then(function(v){
(0,_prompt.finishQuestion)();
var port=!v||isNaN(v)?21:v;
envContent+="RNV_DEPLOY_WEB_FTP_PORT="+port;
_fs.default.writeFileSync(configFilePath,envContent);
(0,_common.logInfo)("Writing .env config to "+configFilePath);
resolve();
});
});
});
});
});};

var _createDeployConfig=function _createDeployConfig(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("_createDeployConfig:"+platform);

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

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

var targetConfigPromise=new Promise(function(resolve,reject){
deploy[_webTools.DEPLOY_TARGET_FTP].localRoot=_path.default.resolve(c.paths.project.builds.dir,c.runtime.appId+"_"+platform);
(0,_prompt.askQuestion)('Folder on the ftp to upload the project (default is \'/\')').
then(function(v){
(0,_prompt.finishQuestion)();
deploy[_webTools.DEPLOY_TARGET_FTP].remoteRoot=v||'/';
}).
then(function(){return(0,_prompt.askQuestion)('Delete all contents of that folder when deploying versions y/N (default is \'N\')?').
then(function(v){
(0,_prompt.finishQuestion)();
deploy[_webTools.DEPLOY_TARGET_FTP].deleteRemote=['yes','Y','y'].indexOf(v)>-1;
});}).
then(function(){return(0,_prompt.askQuestion)('Included files pattern, comma separated (default \'*\',\'**/*\' = all except dot files)').
then(function(v){
(0,_prompt.finishQuestion)();
deploy[_webTools.DEPLOY_TARGET_FTP].include=v?v.split(','):['*','**/*'];
});}).
then(function(){
deploy[_webTools.DEPLOY_TARGET_FTP].exclude=[];
return(0,_prompt.askQuestion)('Excluded files pattern, comma separated (default [])').
then(function(v){
(0,_prompt.finishQuestion)();
deploy[_webTools.DEPLOY_TARGET_FTP].exclude=v?v.split(','):[];
});
}).
then(function(){return(0,_prompt.askQuestion)('Exclude sourcemaps? y/N (default = N)').
then(function(v){
(0,_prompt.finishQuestion)();
deploy[_webTools.DEPLOY_TARGET_FTP].exclude=deploy[_webTools.DEPLOY_TARGET_FTP].exclude.concat(['yes','Y','y'].indexOf(v)>-1?['**/*.map']:[]);
});}).
then(resolve);
});

targetConfigPromise.
then(function(){
(0,_common.logInfo)("Setting your appconfig for "+platform+" to include deploy type: "+_webTools.DEPLOY_TARGET_FTP+"\n                    on "+
c.paths.appConfig.config+"\n                ");



c.files.appConfig.config.platforms[platform].deploy=deploy;
writeObjectSync(c.paths.appConfig.config,c.files.appConfig.config);
resolve();
}).
catch(reject);
});};

var deployToFtp=function deployToFtp(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("checkDeployConfigTarget:"+platform);
var targetConfig=c.buildConfig.platforms[platform];
if(targetConfig&&targetConfig.deploy[_webTools.DEPLOY_TARGET_FTP]&&targetConfig.deploy[_webTools.DEPLOY_TARGET_FTP].type){
_deployToFtp(c,platform).then(resolve).catch(reject);
}else{
_createDeployConfig(c,platform).
then(function(){
_deployToFtp(c,platform).then(resolve).catch(reject);
}).
catch(reject);
}
});};exports.deployToFtp=deployToFtp;