var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.taskRnvCryptoEncrypt=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _tar=_interopRequireDefault(require("tar"));
var _util=require("util");
var _logger=require("../core/systemManager/logger");






var _fileutils=require("../core/systemManager/fileutils");









var _prompt=require("../cli/prompt");
var _crypto=require("../core/systemManager/crypto");
var _engineManager=require("../core/engineManager");
var _constants=require("../core/constants");


var iocane=require('iocane');

var readdirAsync=(0,_util.promisify)(_fileutils.fsReaddir);

var generateRandomKey=function generateRandomKey(length){return Array(length).
fill(
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%^&*').

map(function(x){return x[Math.floor(Math.random()*x.length)];}).
join('');};

var _checkAndConfigureCrypto=function _checkAndConfigureCrypto(c){var source,_await$inquirerPrompt,location,sourceFolder,appConfigsDirs,envVar,key,keyGenerated,_await$inquirerPrompt2,confirm;return _regenerator.default.async(function _checkAndConfigureCrypto$(_context){while(1){switch(_context.prev=_context.next){case 0:

source="./"+c.files.project.package.name;if(!(

c.files.project.config&&!c.files.project.config.crypto)){_context.next=8;break;}_context.next=4;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'input',
name:'location',
message:
'Where would you like your secrets to be residing? (path relative to root, without leading or trailing slash. Ex. `myPrivateConfig/encrypt`)',
default:'secrets'}));case 4:_await$inquirerPrompt=_context.sent;location=_await$inquirerPrompt.location;

c.files.project.config.crypto={
encrypt:{
dest:"PROJECT_HOME/"+location+"/privateConfigs.enc"},

decrypt:{
source:"PROJECT_HOME/"+location+"/privateConfigs.enc"}};


(0,_fileutils.writeFileSync)(c.paths.project.config,c.files.project.config);case 8:



sourceFolder=_path.default.join(c.paths.workspace.dir,source);if(
(0,_fileutils.fsExistsSync)(sourceFolder)){_context.next=20;break;}
(0,_logger.logInfo)("It seems you are running encrypt for the first time. Directory "+
(0,_logger.chalk)().white(
sourceFolder)+" does not exist yet.\nRNV will create it for you, make sure you add whatever you want encrypted in it and then run the command again");




(0,_fileutils.mkdirSync)(sourceFolder);
(0,_fileutils.mkdirSync)(_path.default.join(sourceFolder,'certs'));
(0,_fileutils.writeFileSync)(_path.default.join(sourceFolder,'renative.private.json'),{});_context.next=16;return _regenerator.default.awrap(

readdirAsync(c.paths.project.appConfigsDir));case 16:appConfigsDirs=_context.sent;

appConfigsDirs.forEach(function(item){
var appConfigDir=_path.default.join(sourceFolder,item);
(0,_fileutils.mkdirSync)(appConfigDir);
(0,_fileutils.mkdirSync)(_path.default.join(appConfigDir,'certs'));
(0,_fileutils.writeFileSync)(_path.default.join(appConfigDir,'renative.private.json'),{});
});_context.next=20;return _regenerator.default.awrap(


(0,_prompt.inquirerPrompt)({
type:'confirm',
message:'Once ready, Continue?'}));case 20:





envVar=(0,_crypto.getEnvVar)(c);
key=c.program.key||c.process.env[envVar];
keyGenerated=false;if(
key){_context.next=35;break;}_context.next=26;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'confirm',
message:"You haven't passed a key with --key or set an env variable named "+(0,_logger.chalk)().yellow(
envVar)+" for the encryption key. Would you like to generate one?"}));case 26:_await$inquirerPrompt2=_context.sent;confirm=_await$inquirerPrompt2.confirm;if(!


confirm){_context.next=33;break;}
key=generateRandomKey(20);
keyGenerated=true;_context.next=34;break;case 33:return _context.abrupt("return",

Promise.reject("encrypt: You must pass "+(0,_logger.chalk)().white(
'--key')+" or have env var defined:\n\n"+


(0,_crypto.getEnvExportCmd)(envVar,'REPLACE_WITH_ENV_VARIABLE')+"\n\n"));case 34:



if(keyGenerated){
(0,_logger.logSuccess)("The files were encrypted with key "+(0,_logger.chalk)().red(
key)+". Make sure you keep it safe! Pass it with --key on decryption or set it as following env variable:\n\n"+


(0,_crypto.getEnvExportCmd)(envVar,key)+"\n\n");


c.process.env[envVar]=key;
}case 35:case"end":return _context.stop();}}},null,null,null,Promise);};



var taskRnvCryptoEncrypt=function taskRnvCryptoEncrypt(c,parentTask,originTask){var _c$files$project$conf,_c$files$project$conf2,_c$files$project$conf3;var source,destRaw,tsWorkspacePath,envVar,key,dest,destTemp,timestamp,destFolder,data;return _regenerator.default.async(function taskRnvCryptoEncrypt$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('taskRnvCryptoEncrypt');_context2.next=3;return _regenerator.default.awrap(

(0,_engineManager.executeTask)(c,_constants.TASK_PROJECT_CONFIGURE,_constants.TASK_CRYPTO_ENCRYPT,originTask));case 3:

source="./"+c.files.project.package.name;_context2.next=6;return _regenerator.default.awrap(

_checkAndConfigureCrypto(c));case 6:

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;
tsWorkspacePath=_path.default.join(
c.paths.workspace.dir,
c.files.project.package.name,
'timestamp');

envVar=(0,_crypto.getEnvVar)(c);
key=c.program.key||c.process.env[envVar];if(!

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

timestamp=new Date().getTime();


destFolder=_path.default.join(dest,'../core/');
!(0,_fileutils.fsExistsSync)(destFolder)&&(0,_fileutils.mkdirSync)(destFolder);_context2.next=18;return _regenerator.default.awrap(

_tar.default.c(
{
gzip:true,
file:destTemp,
cwd:c.paths.workspace.dir},

[source]));case 18:_context2.next=20;return _regenerator.default.awrap(



iocane.createSession().
use('cbc').
encrypt((0,_fileutils.fsReadFileSync)(destTemp),key));case 20:data=_context2.sent;

(0,_fileutils.fsWriteFileSync)(dest,data);








(0,_fileutils.removeFilesSync)([destTemp]);
(0,_fileutils.fsWriteFileSync)(dest+".timestamp",timestamp);
(0,_fileutils.fsWriteFileSync)(""+tsWorkspacePath,timestamp);
(0,_logger.logSuccess)("Files succesfully encrypted into "+dest);_context2.next=29;break;case 28:

(0,_logger.logWarning)("You don't have {{ crypto.encrypt.dest }} specificed in "+
(0,_logger.chalk)().white(
c.paths.appConfigBase));case 29:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.taskRnvCryptoEncrypt=taskRnvCryptoEncrypt;var _default=





{
description:'Encrypts secure files from ~/<wokspace>/<project>/.. to project',
fn:taskRnvCryptoEncrypt,
task:_constants.TASK_CRYPTO_ENCRYPT,
params:_constants.PARAMS.withBase(),
platforms:[],
skipPlatforms:true};exports.default=_default;
//# sourceMappingURL=task.rnv.crypto.encrypt.js.map