var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.rnvCryptoUpdateProfiles=exports.rnvCryptoInstallCerts=exports.rnvCryptoInstallProfiles=exports.rnvCryptoDecrypt=exports.rnvCryptoEncrypt=exports.rnvCryptoUpdateProfile=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _tar=_interopRequireDefault(require("tar"));
var _chalk=_interopRequireDefault(require("chalk"));
var _fs=_interopRequireDefault(require("fs"));
var _common=require("../common");
var _configParser=require("../configTools/configParser");
var _constants=require("../constants");
var _fileutils=require("./fileutils");
var _exec=require("./exec");
var _fastlane=require("../platformTools/apple/fastlane");

var getEnvVar=function getEnvVar(c){
var p1=c.paths.workspace.dir.split('/').pop().replace('.','');
var p2=c.files.project.package.name.replace('@','').replace('/','_').replace(/-/g,'_');
var envVar=("CRYPTO_"+p1+"_"+p2).toUpperCase();
(0,_common.logDebug)('encrypt looking for env var:',envVar);
return envVar;
};

var rnvCryptoUpdateProfile=function rnvCryptoUpdateProfile(c){return _regenerator.default.async(function rnvCryptoUpdateProfile$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.next=2;return _regenerator.default.awrap(
(0,_fastlane.updateProfile)(c));case 2:case"end":return _context.stop();}}});};exports.rnvCryptoUpdateProfile=rnvCryptoUpdateProfile;


var rnvCryptoEncrypt=function rnvCryptoEncrypt(c){return new Promise(function(resolve,reject){var _c$files$project$conf,_c$files$project$conf2,_c$files$project$conf3;
(0,_common.logTask)('rnvCryptoEncrypt');

var source="./"+c.files.project.package.name;
var destRaw=(_c$files$project$conf=c.files.project.config)==null?void 0:(_c$files$project$conf2=_c$files$project$conf.crypto)==null?void 0:(_c$files$project$conf3=_c$files$project$conf2.encrypt)==null?void 0:_c$files$project$conf3.dest;

if(destRaw){
var dest=""+(0,_fileutils.getRealPath)(c,destRaw,'encrypt.dest');
var destTemp=_path.default.join(c.paths.workspace.dir,c.files.project.package.name.replace('/','-'))+".tgz";

var envVar=getEnvVar(c);
var key=c.program.key||c.process.env[envVar];
if(!key){
reject("encrypt: You must pass "+_chalk.default.white('--key')+" or have env var "+_chalk.default.white(envVar)+" defined");
return;
}
_tar.default.c(
{
gzip:true,
file:destTemp,
cwd:c.paths.workspace.dir},

[source]).

then(function(){return(0,_exec.executeAsync)(c,"openssl enc -aes-256-cbc -salt -in "+destTemp+" -out "+dest+" -k %s",{privateParams:[key]});}).
then(function(){
(0,_fileutils.removeFilesSync)([destTemp]);
_fs.default.writeFileSync(dest+".timestamp",new Date().getTime());
(0,_common.logSuccess)("Files succesfully encrypted into "+dest);
resolve();
}).catch(function(e){
reject(e);
});
}else{
(0,_common.logWarning)("You don't have {{ crypto.encrypt.dest }} specificed in "+_chalk.default.white(c.paths.projectConfig));
resolve();
}
});};exports.rnvCryptoEncrypt=rnvCryptoEncrypt;

var rnvCryptoDecrypt=function rnvCryptoDecrypt(c){return new Promise(function(resolve,reject){var _c$files$project$conf4,_c$files$project$conf5,_c$files$project$conf6;
(0,_common.logTask)('rnvCryptoDecrypt');

var sourceRaw=(_c$files$project$conf4=c.files.project.config)==null?void 0:(_c$files$project$conf5=_c$files$project$conf4.crypto)==null?void 0:(_c$files$project$conf6=_c$files$project$conf5.decrypt)==null?void 0:_c$files$project$conf6.source;

if(sourceRaw){
var source=""+(0,_fileutils.getRealPath)(c,sourceRaw,'decrypt.source');
var ts=source+".timestamp";
var destTemp=_path.default.join(c.paths.workspace.dir,c.files.project.package.name.replace('/','-'))+".tgz";
var envVar=getEnvVar(c);

var key=c.program.key||c.process.env[envVar];
if(!key){
reject("encrypt: You must pass "+_chalk.default.white('--key')+" or have env var "+_chalk.default.white(envVar)+" defined");
return;
}
(0,_exec.executeAsync)(c,"openssl enc -aes-256-cbc -d -in "+source+" -out "+destTemp+" -k %s",{privateParams:[key]}).
then(function(){
_tar.default.x(
{
file:destTemp,
cwd:c.paths.workspace.dir}).

then(function(){
(0,_fileutils.removeFilesSync)([destTemp]);
if(_fs.default.existsSync(ts)){
(0,_fileutils.copyFileSync)(ts,_path.default.join(c.paths.workspace.dir,c.files.project.package.name,'timestamp'));
}
(0,_common.logSuccess)("Files succesfully extracted into "+c.paths.workspace.dir);
resolve();
}).
catch(function(e){return reject(e);});
}).catch(function(e){
reject(e);
});
}else{
(0,_common.logWarning)("You don't have {{ crypto.encrypt.dest }} specificed in "+_chalk.default.white(c.paths.projectConfig));
resolve();
}
});};exports.rnvCryptoDecrypt=rnvCryptoDecrypt;

var rnvCryptoInstallProfiles=function rnvCryptoInstallProfiles(c){return new Promise(function(resolve,reject){
(0,_common.logTask)('rnvCryptoInstallProfiles');
if(c.platform!=='ios'){
(0,_common.logError)("rnvCryptoInstallProfiles: platform "+c.platform+" not supported");
resolve();
return;
}

var ppFolder=_path.default.join(c.paths.home.dir,'Library/MobileDevice/Provisioning Profiles');

if(!_fs.default.existsSync(ppFolder)){
(0,_common.logWarning)("folder "+ppFolder+" does not exist!");
(0,_fileutils.mkdirSync)(ppFolder);
}

var list=(0,_fileutils.getFileListSync)(c.paths.workspace.project.dir);
var mobileprovisionArr=list.filter(function(v){return v.endsWith('.mobileprovision');});

try{
mobileprovisionArr.forEach(function(v){
console.log("rnvCryptoInstallProfiles: Installing: "+v);
(0,_fileutils.copyFileSync)(v,ppFolder);
});
}catch(e){
(0,_common.logError)(e);
}

resolve();
});};exports.rnvCryptoInstallProfiles=rnvCryptoInstallProfiles;

var rnvCryptoInstallCerts=function rnvCryptoInstallCerts(c){return new Promise(function(resolve,reject){
(0,_common.logTask)('rnvCryptoInstallCerts');var
maxErrorLength=c.program.maxErrorLength;

if(c.platform!=='ios'){
(0,_common.logError)("_installTempCerts: platform "+c.platform+" not supported");
resolve();
return;
}
var kChain=c.program.keychain||'ios-build.keychain';
var kChainPath=_path.default.join(c.paths.home.dir,'Library/Keychains',kChain);
var list=(0,_fileutils.getFileListSync)(c.paths.workspace.project.dir);
var cerPromises=[];
var cerArr=list.filter(function(v){return v.endsWith('.cer');});

Promise.all(cerArr.map(function(v){return(0,_exec.executeAsync)(c,"security import "+v+" -k "+kChain+" -A");})).
then(function(){return resolve();}).
catch(function(e){
(0,_common.logWarning)(e);
resolve();
});
});};exports.rnvCryptoInstallCerts=rnvCryptoInstallCerts;


var rnvCryptoUpdateProfiles=function rnvCryptoUpdateProfiles(c){
(0,_common.logTask)('rnvCryptoUpdateProfiles');
switch(c.platform){
case _constants.IOS:
case _constants.TVOS:var
appId=c.runtime.appId;
return _updateProfiles(c).
then(function(){
(0,_configParser.setAppConfig)(c,appId);
});}

return Promise.reject("updateProfiles: Platform "+c.platform+" not supported");
};exports.rnvCryptoUpdateProfiles=rnvCryptoUpdateProfiles;

var _updateProfiles=function _updateProfiles(c){
(0,_common.logTask)('_updateProfiles',_chalk.default.grey);
var acList=(0,_configParser.listAppConfigsFoldersSync)(c,true);
var fullList=[];
var currentAppId=c.runtime.appId;

return acList.reduce(function(previousPromise,v){return previousPromise.then(function(){return _updateProfile(c,v);});},Promise.resolve());
};

var _updateProfile=function _updateProfile(c,v){return new Promise(function(resolve,reject){
(0,_common.logTask)("_updateProfile:"+v,_chalk.default.grey);
(0,_fastlane.updateProfile)(c,v).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};