var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.executeEngineTask=exports.shouldSkipTask=exports.executeOrSkipTask=exports.executeTask=exports.findSuitableTask=exports.initializeTask=exports.registerCustomTask=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _logger=require("../systemManager/logger");
var _analytics=_interopRequireDefault(require("../systemManager/analytics"));
var _buildHooks=require("../projectManager/buildHooks");
var _prompt=require("../../cli/prompt");
var _npmUtils=require("../systemManager/npmUtils");
var _engineManager=require("../engineManager");
var _constants=require("../constants");


var executedTasks={};

var CUSTOM_TASKS={};

var registerCustomTask=function registerCustomTask(c,task){return _regenerator.default.async(function registerCustomTask$(_context){while(1){switch(_context.prev=_context.next){case 0:
if(task.task){
CUSTOM_TASKS[task.task]=task;
}case 1:case"end":return _context.stop();}}},null,null,null,Promise);};exports.registerCustomTask=registerCustomTask;


var initializeTask=function initializeTask(c,task){return _regenerator.default.async(function initializeTask$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('initializeTask',task);
c.runtime.task=task;
executedTasks={};

_analytics.default.captureEvent({
type:task+"Project",
platform:c.platform});_context2.next=6;return _regenerator.default.awrap(


executeTask(c,task,null,task,true));case 6:return _context2.abrupt("return",
true);case 7:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.initializeTask=initializeTask;


var _getTaskOption=function _getTaskOption(_ref){var taskInstance=_ref.taskInstance,hasMultipleSubTasks=_ref.hasMultipleSubTasks;
if(hasMultipleSubTasks){
return taskInstance.task.split(' ')[0]+"...";
}
if(taskInstance.description&&taskInstance.description!==''){
return taskInstance.task.split(' ')[0]+" "+(0,_logger.chalk)().grey("("+taskInstance.description+")");
}
return""+taskInstance.task.split(' ')[0];
};

var _getTaskObj=function _getTaskObj(taskInstance){
var key=taskInstance.task.split(' ')[0];
var hasMultipleSubTasks=false;
if(taskInstance.task.includes(' '))hasMultipleSubTasks=true;
return{
key:key,
taskInstance:taskInstance,
hasMultipleSubTasks:hasMultipleSubTasks};

};

var findSuitableTask=function findSuitableTask(c,specificTask){var REGISTERED_ENGINES,task,suitableTaskInstances,taskInstances,tasks,defaultCmd,tasksCommands,filteredTasks,addendum,_await$inquirerPrompt,command,suitableEngines,autocompleteEngines,isAutoComplete,message,supportedSubtasksArr,supportedSubtasks,supportedSubtasksFilter,subTasks,_await$inquirerPrompt2,subCommand,customTask;return _regenerator.default.async(function findSuitableTask$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('findSuitableTask');
REGISTERED_ENGINES=(0,_engineManager.getRegisteredEngines)(c);if(

specificTask){_context3.next=65;break;}if(
c.command){_context3.next=16;break;}
suitableTaskInstances={};
REGISTERED_ENGINES.forEach(function(engine){
Object.values(engine.tasks).forEach(function(taskInstance){
var taskObj=_getTaskObj(taskInstance);
suitableTaskInstances[taskObj.key]=taskObj;
});
});
Object.values(CUSTOM_TASKS).forEach(function(taskInstance){
var taskObj=_getTaskObj(taskInstance);
suitableTaskInstances[taskObj.key]=taskObj;
});

taskInstances=Object.values(suitableTaskInstances);

defaultCmd='new';


addendum='';
if(!c.paths.project.configExists){
filteredTasks=taskInstances.filter(function(v){return v.taskInstance.isGlobalScope;});
tasks=filteredTasks.map(function(v){return _getTaskOption(v);}).sort();
tasksCommands=filteredTasks.map(function(v){return v.taskInstance.task.split(' ')[0];}).sort();
addendum=' (Not a ReNative project. options will be limited)';
}else{
tasks=taskInstances.map(function(v){return _getTaskOption(v);}).sort();
tasksCommands=taskInstances.map(function(v){return v.taskInstance.task.split(' ')[0];}).sort();
defaultCmd=tasks.find(function(v){return v.startsWith('run');});
}_context3.next=13;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
type:'list',
default:defaultCmd,
name:'command',
message:"Pick a command"+addendum,
choices:tasks,
pageSize:15,
logMessage:'Welcome to the brave new world...'}));case 13:_await$inquirerPrompt=_context3.sent;command=_await$inquirerPrompt.command;

c.command=tasksCommands[tasks.indexOf(command)];case 16:

task=c.command;
if(c.subCommand)task+=" "+c.subCommand;

suitableEngines=REGISTERED_ENGINES.
filter(function(engine){return(0,_engineManager.hasEngineTask)(task,engine.tasks,c.paths.project.configExists);});

autocompleteEngines=REGISTERED_ENGINES.
filter(function(engine){return(0,_engineManager.getEngineSubTasks)(task,engine.tasks,true).length;});

isAutoComplete=!suitableEngines.length&&!!c.command&&!autocompleteEngines.length;
message=isAutoComplete?"Autocomplete action for \""+
c.command+"\"":"Pick a subCommand for "+c.command;if(

suitableEngines.length){_context3.next=38;break;}

supportedSubtasksArr=[];
REGISTERED_ENGINES.forEach(function(engine){
(0,_engineManager.getEngineSubTasks)(task,engine.tasks).forEach(function(taskInstance){
var isNotViable=!c.paths.project.configExists&&!taskInstance.isGlobalScope;
if(!isNotViable){var _taskInstance$descrip;
var taskKey=isAutoComplete?taskInstance.task:taskInstance.task.split(' ')[1];

supportedSubtasksArr.push({
desc:(_taskInstance$descrip=taskInstance.description)==null?void 0:_taskInstance$descrip.toLowerCase==null?void 0:_taskInstance$descrip.toLowerCase(),
taskKey:taskKey});

}
});
});
Object.values(CUSTOM_TASKS).forEach(function(taskInstance){
var tskArr=taskInstance.task.split(' ');
if(task===tskArr[0]){var _taskInstance$descrip2;
var taskKey=isAutoComplete?taskInstance.task:taskInstance.task.split(' ')[1];

supportedSubtasksArr.push({
desc:(_taskInstance$descrip2=taskInstance.description)==null?void 0:_taskInstance$descrip2.toLowerCase==null?void 0:_taskInstance$descrip2.toLowerCase(),
taskKey:taskKey});

}
});
supportedSubtasks={};

supportedSubtasksFilter={};
supportedSubtasksArr.forEach(function(tsk){
var mergedTask=supportedSubtasksFilter[tsk.taskKey];
if(!mergedTask){
supportedSubtasksFilter[tsk.taskKey]=tsk;
}else if(!mergedTask.desc.includes(tsk.desc)){
mergedTask.desc+=", "+tsk.desc;
}
});


Object.values(supportedSubtasksFilter).forEach(function(v){
var desc=v.desc?"("+v.desc+")":'';
var key=v.taskKey+" "+(0,_logger.chalk)().grey(desc);
supportedSubtasks[key]={
taskKey:v.taskKey};

});

subTasks=Object.keys(supportedSubtasks);if(!
subTasks.length){_context3.next=38;break;}_context3.next=34;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'list',
name:'subCommand',
message:message,
choices:subTasks}));case 34:_await$inquirerPrompt2=_context3.sent;subCommand=_await$inquirerPrompt2.subCommand;

if(isAutoComplete){
task=supportedSubtasks[subCommand].taskKey;
c.command=task.split(' ')[0];
c.subCommand=task.split(' ')[1];
if(c.subCommand){
task=c.command+" "+c.subCommand;
}else{
task=""+c.command;
}
}else{
c.subCommand=supportedSubtasks[subCommand].taskKey;
task=c.command+" "+c.subCommand;
}


suitableEngines=REGISTERED_ENGINES.
filter(function(engine){return(0,_engineManager.hasEngineTask)(task,engine.tasks,c.paths.project.configExists);});case 38:if(!



CUSTOM_TASKS[task]){_context3.next=42;break;}

(0,_logger.logInfo)("Running custom task "+task);_context3.next=51;break;case 42:if(
suitableEngines.length){_context3.next=51;break;}if(
c.runtime.hasAllEnginesRegistered){_context3.next=47;break;}_context3.next=46;return _regenerator.default.awrap(

(0,_engineManager.registerAllPlatformEngines)(c));case 46:return _context3.abrupt("return",
findSuitableTask(c));case 47:


(0,_logger.logError)("could not find suitable task for "+(0,_logger.chalk)().white(c.command));
c.command=null;
c.subCommand=null;return _context3.abrupt("return",
findSuitableTask(c));case 51:if(!(

!c.platform||c.platform===true)){_context3.next=54;break;}_context3.next=54;return _regenerator.default.awrap(
_selectPlatform(c,suitableEngines,task));case 54:

c.runtime.engine=(0,_engineManager.getEngineRunner)(c,task,CUSTOM_TASKS,false);if(

c.runtime.engine){_context3.next=59;break;}_context3.next=58;return _regenerator.default.awrap(
_selectPlatform(c,suitableEngines,task));case 58:
c.runtime.engine=(0,_engineManager.getEngineRunner)(c,task,CUSTOM_TASKS);case 59:


(0,_logger.logInfo)("Current Engine: "+(0,_logger.chalk)().bold.white(
c.runtime.engine.config.id));

customTask=CUSTOM_TASKS[task];if(!
customTask){_context3.next=63;break;}return _context3.abrupt("return",customTask);case 63:_context3.next=67;break;case 65:

task=specificTask;
c.runtime.engine=(0,_engineManager.getEngineRunner)(c,task);case 67:

c.runtime.availablePlatforms=Object.keys(c.runtime.engine.platforms||[]);return _context3.abrupt("return",
(0,_engineManager.getEngineTask)(task,c.runtime.engine.tasks));case 69:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.findSuitableTask=findSuitableTask;


var _selectPlatform=function _selectPlatform(c,suitableEngines,task){var supportedPlatforms,platforms,_await$inquirerPrompt3,platform;return _regenerator.default.async(function _selectPlatform$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
supportedPlatforms={};
suitableEngines.forEach(function(engine){
(0,_engineManager.getEngineTask)(task,engine.tasks).platforms.forEach(function(plat){
supportedPlatforms[plat]=true;
});
});
platforms=Object.keys(supportedPlatforms);if(!

platforms.length){_context4.next=9;break;}_context4.next=6;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'list',
name:'platform',
message:"Pick a platform for "+task,
choices:platforms}));case 6:_await$inquirerPrompt3=_context4.sent;platform=_await$inquirerPrompt3.platform;

c.platform=platform;case 9:case"end":return _context4.stop();}}},null,null,null,Promise);};




var _executePipe=function _executePipe(c,task,phase){return _regenerator.default.async(function _executePipe$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt("return",(0,_buildHooks.executePipe)(c,task.split(' ').join(':')+":"+phase));case 1:case"end":return _context5.stop();}}},null,null,null,Promise);};

var TASK_LIMIT=20;

var executeTask=function executeTask(c,task,parentTask,originTask,isFirstTask){var pt,prt;return _regenerator.default.async(function executeTask$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
pt=parentTask?"=> ["+parentTask+"] ":'';
c._currentTask=task;
(0,_logger.logInitTask)(pt+"=> ["+(0,_logger.chalk)().bold.rgb(170,106,170)(task)+"]");

if(!executedTasks[task])executedTasks[task]=0;if(!(
executedTasks[task]>TASK_LIMIT)){_context6.next=6;break;}return _context6.abrupt("return",
Promise.reject("You reached maximum amount of executions per one task ("+TASK_LIMIT+") task: "+task+".\nThis is to warn you ended up in task loop.\n("+

task+" calls same or another task which calls "+task+" again)\nbut issue migh not be necessarily with this task\n\nTo avoid that test your task code against parentTask and avoid executing same task X from within task X"));case 6:_context6.next=8;return _regenerator.default.awrap(




executeEngineTask(
c,task,parentTask,originTask,
(0,_engineManager.getEngineRunner)(c,task,CUSTOM_TASKS).tasks,
isFirstTask));case 8:


executedTasks[task]++;

c._currentTask=parentTask;
prt=parentTask?"<= ["+(0,_logger.chalk)().rgb(170,106,170)(parentTask)+"] ":'';
(0,_logger.logExitTask)(prt+"<= "+task);case 12:case"end":return _context6.stop();}}},null,null,null,Promise);};exports.executeTask=executeTask;


var executeOrSkipTask=function executeOrSkipTask(c,task,parentTask,originTask){return _regenerator.default.async(function executeOrSkipTask$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:if(
c.program.only){_context7.next=2;break;}return _context7.abrupt("return",
executeTask(c,task,parentTask,originTask));case 2:return _context7.abrupt("return",


executeTask(c,_constants.TASK_CONFIGURE_SOFT,parentTask,originTask));case 3:case"end":return _context7.stop();}}},null,null,null,Promise);};exports.executeOrSkipTask=executeOrSkipTask;


var ACCEPTED_CONDITIONS=[
'platform',
'target',
'appId',
'scheme'];


var _logSkip=function _logSkip(task){
(0,_logger.logInfo)("Original RNV task "+(0,_logger.chalk)().white(task)+" marked to ignore. SKIPPING...");
};

var shouldSkipTask=function shouldSkipTask(c,task,originTask){var _c$buildConfig;
var tasks=(_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.tasks;
c.runtime.platform=c.platform;
if(!tasks)return;

if(Array.isArray(tasks)){
for(var k=0;k<tasks.length;k++){
var t=tasks[k];
if(t.name===task){
if(t.filter){var _ret=function(){
var conditions=t.filter.split('&');
var conditionsToMatch=conditions.length;
conditions.forEach(function(con){
var conArr=con.split('=');
if(ACCEPTED_CONDITIONS.includes(conArr[0])){
if(c.runtime[conArr[0]]===conArr[1]){
conditionsToMatch--;
}
}else{
(0,_logger.logWarning)("Condition "+con+" not valid. only following keys are valid: "+ACCEPTED_CONDITIONS.join(',')+" SKIPPING...");
}
});
if(conditionsToMatch===0){
if(t.ignore){
_logSkip(task);
return{v:true};
}
}}();if(typeof _ret==="object")return _ret.v;
}else if(t.ignore){
_logSkip(task);
return true;
}
}
}
}else{var _tasks$task,_tasks$task$platform,_tasks$task$platform$,_tasks$originTask,_tasks$originTask$pla,_tasks$originTask$pla2;
var ignoreTask=(_tasks$task=tasks[task])==null?void 0:(_tasks$task$platform=_tasks$task.platform)==null?void 0:(_tasks$task$platform$=_tasks$task$platform[c.platform])==null?void 0:_tasks$task$platform$.ignore;
if(ignoreTask){
_logSkip(task);
return true;
}
var ignoreTasks=((_tasks$originTask=tasks[originTask])==null?void 0:(_tasks$originTask$pla=_tasks$originTask.platform)==null?void 0:(_tasks$originTask$pla2=_tasks$originTask$pla[c.platform])==null?void 0:_tasks$originTask$pla2.ignoreTasks)||[];
if(ignoreTasks.includes(task)){
(0,_logger.logInfo)("Task "+task+" marked to skip during rnv "+originTask+". SKIPPING...");
return true;
}
}

return false;
};exports.shouldSkipTask=shouldSkipTask;

var executeEngineTask=function executeEngineTask(c,task,parentTask,originTask,tasks,isFirstTask){var needsHelp,t,inOnlyMode,doPipe;return _regenerator.default.async(function executeEngineTask$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:
needsHelp=Object.prototype.hasOwnProperty.call(c.program,'help');

t=(0,_engineManager.getEngineTask)(task,tasks,CUSTOM_TASKS);if(!(

needsHelp&&!parentTask)){_context8.next=10;break;}
(0,_logger.logRaw)("\n=======================================================\nINTERACTIVE HELP FOR TASK: "+

(0,_logger.chalk)().green(t.task)+"\n\nDescription: "+

t.description+"\n\nOptions:\n\n"+



t.params.map(function(v){
var option=v.shortcut?"`-"+v.shortcut+"`, ":'';
return option+"`--"+v.key+"` - "+v.description;
}).join('\n')+"\n\n  ");if(!


t.fnHelp){_context8.next=7;break;}_context8.next=7;return _regenerator.default.awrap(
t.fnHelp(c,parentTask,originTask));case 7:_context8.next=9;return _regenerator.default.awrap(


(0,_prompt.pressAnyKeyToContinue)());case 9:
(0,_logger.logRaw)("\n=======================================================");case 10:if(!(


!t.isGlobalScope&&isFirstTask)){_context8.next=14;break;}if(!
c.files.project.package){_context8.next=14;break;}_context8.next=14;return _regenerator.default.awrap(

(0,_npmUtils.checkIfProjectAndNodeModulesExists)(c));case 14:


if(isFirstTask){
c.runtime.forceBuildHookRebuild=t.forceBuildHookRebuild;
}
inOnlyMode=c.program.only;
doPipe=!t.isGlobalScope&&(!inOnlyMode||inOnlyMode&&isFirstTask);if(!
doPipe){_context8.next=20;break;}_context8.next=20;return _regenerator.default.awrap(_executePipe(c,task,'before'));case 20:_context8.next=22;return _regenerator.default.awrap(
t.fn(c,parentTask,originTask));case 22:if(!
doPipe){_context8.next=25;break;}_context8.next=25;return _regenerator.default.awrap(_executePipe(c,task,'after'));case 25:case"end":return _context8.stop();}}},null,null,null,Promise);};exports.executeEngineTask=executeEngineTask;
//# sourceMappingURL=index.js.map