var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.getRegisteredEngines=exports.getEngineRunner=exports.getEngineSubTasks=exports.hasEngineTask=exports.getEngineTask=exports.getEngineRunnerByPlatform=exports.getPlatformExtensions=exports.getEngineConfigByPlatform=exports.generateEnvVars=exports.registerPlatformEngine=exports.loadEngineConfigs=exports.registerMissingPlatformEngines=exports.configureEngines=exports.registerEngine=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));
var _path=_interopRequireDefault(require("path"));
var _logger=require("../systemManager/logger");
var _common=require("../common");
var _exec=require("../systemManager/exec");
var _resolve=require("../systemManager/resolve");
var _utils=require("../systemManager/utils");
var _fileutils=require("../systemManager/fileutils");



var REGISTERED_ENGINES=[];
var ENGINES={};
var ENGINE_CORE='engine-core';

var registerEngine=function registerEngine(c,engine,platform){return _regenerator.default.async(function registerEngine$(_context){while(1){switch(_context.prev=_context.next){case 0:
ENGINES[engine.getId()]=engine;
REGISTERED_ENGINES.push(engine);
_registerEnginePlatform(c,platform,engine);case 3:case"end":return _context.stop();}}},null,null,null,Promise);};exports.registerEngine=registerEngine;


var _registerEnginePlatform=function _registerEnginePlatform(c,platform,engine){
if(platform){
c.runtime.enginePlatforms[platform]=engine;
}
};

var configureEngines=function configureEngines(c){var engines,devDependencies,needsPackageUpdate;return _regenerator.default.async(function configureEngines$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('configureEngines');if(!
c.runtime.isWrapper){_context2.next=3;break;}return _context2.abrupt("return",true);case 3:
engines=c.files.project.config.engines;
devDependencies=c.files.project.package.devDependencies;
needsPackageUpdate=false;
if(engines){
Object.keys(engines).forEach(function(k){var _c$buildConfig$engine,_c$buildConfig$engine2;
var engVer=(_c$buildConfig$engine=c.buildConfig.engineTemplates)==null?void 0:(_c$buildConfig$engine2=_c$buildConfig$engine[k])==null?void 0:_c$buildConfig$engine2.version;
if(engVer){
if(devDependencies[k]){
if(devDependencies[k]!==engVer){
needsPackageUpdate=true;
(0,_logger.logInfo)("Updating missing engine "+k+" "+
(0,_logger.chalk)().red(devDependencies[k])+"=>"+engVer+" to package.json");
devDependencies[k]=engVer;
}
}else{
needsPackageUpdate=true;
(0,_logger.logInfo)("Adding missing engine "+k+"@"+engVer+" to package.json");
devDependencies[k]=engVer;
}
}
});
if(needsPackageUpdate){
(0,_fileutils.writeFileSync)(c.paths.project.package,c.files.project.package);
}
}return _context2.abrupt("return",
true);case 8:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.configureEngines=configureEngines;


var registerMissingPlatformEngines=function registerMissingPlatformEngines(c,taskInstance){var _taskInstance$platfor;var registerEngineList;return _regenerator.default.async(function registerMissingPlatformEngines$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('registerMissingPlatformEngines');if(!(
!taskInstance.isGlobalScope&&(taskInstance==null?void 0:(_taskInstance$platfor=taskInstance.platforms)==null?void 0:_taskInstance$platfor.length)===0)){_context3.next=7;break;}
registerEngineList=[];
c.buildConfig.defaults.supportedPlatforms.forEach(function(platform){
registerEngineList.push(
registerPlatformEngine(c,platform));

});if(!

registerEngineList.length){_context3.next=7;break;}_context3.next=7;return _regenerator.default.awrap(
Promise.all(registerEngineList));case 7:return _context3.abrupt("return",



true);case 8:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.registerMissingPlatformEngines=registerMissingPlatformEngines;


var loadEngineConfigs=function loadEngineConfigs(c){var _c$buildConfig;var engines,enginesToInstall;return _regenerator.default.async(function loadEngineConfigs$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('loadEngineConfigs');
engines=(_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.engines;
c.runtime.engineConfigs={};
enginesToInstall=[];if(!
engines){_context4.next=13;break;}
Object.keys(engines).forEach(function(k){
var engineRootPath=(0,_resolve.doResolve)(k);
var configPath=engineRootPath?_path.default.join(engineRootPath,'renative.engine.json'):null;
if(configPath&&(0,_fileutils.fsExistsSync)(configPath)){
var engineConfig=(0,_fileutils.readObjectSync)(configPath);
c.runtime.engineConfigs[engineConfig.id]=engineConfig;
}else{
var engVer=(0,_utils.getScopedVersion)(c,k,engines[k],'engineTemplates');
if(engVer){
enginesToInstall.push(k+"@"+engVer);
}
}
});if(!
enginesToInstall.length){_context4.next=11;break;}
(0,_logger.logInfo)("Some engines not installed in your project:\n"+
enginesToInstall.map(function(v){return"> "+v;}).join('\n')+"\n INSTALLING...");_context4.next=10;return _regenerator.default.awrap(

Promise.all(enginesToInstall.map(function(v){return(0,_exec.executeAsync)("npm i "+v+" --no-save",{
cwd:c.paths.project.dir});})));case 10:return _context4.abrupt("return",

loadEngineConfigs(c));case 11:_context4.next=18;break;case 13:if(!

c.files.project.config){_context4.next=18;break;}
(0,_logger.logInfo)('Engine configs missing in your renative.json. FIXING...DONE');
c.files.project.config.engines={
'@rnv/engine-rn':'source:rnv',
'@rnv/engine-rn-web':'source:rnv',
'@rnv/engine-rn-next':'source:rnv',
'@rnv/engine-rn-electron':'source:rnv'};

(0,_fileutils.writeFileSync)(c.paths.project.config,c.files.project.config);return _context4.abrupt("return",
false);case 18:return _context4.abrupt("return",

true);case 19:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.loadEngineConfigs=loadEngineConfigs;


var registerPlatformEngine=function registerPlatformEngine(c,platform){

var selectedEngineConfig=getEngineConfigByPlatform(c,platform);
if(selectedEngineConfig){
var existingEngine=ENGINES[selectedEngineConfig.id];
if(!existingEngine){var _require;
registerEngine(c,(_require=require(selectedEngineConfig.packageName))==null?void 0:_require.default,platform);
}else{
_registerEnginePlatform(c,platform,existingEngine);
}
}
};exports.registerPlatformEngine=registerPlatformEngine;

var generateEnvVars=function generateEnvVars(c,moduleConfig,nextConfig){return{
RNV_EXTENSIONS:getPlatformExtensions(c),
RNV_MODULE_PATHS:(moduleConfig==null?void 0:moduleConfig.modulePaths)||[],
RNV_MODULE_ALIASES:(moduleConfig==null?void 0:moduleConfig.moduleAliasesArray)||[],
RNV_NEXT_TRANSPILE_MODULES:nextConfig,
RNV_PROJECT_ROOT:c.paths.project.dir,
RNV_MONO_ROOT:c.runtime.isWrapper?_path.default.join(c.paths.project.dir,'../..'):c.paths.project.dir};};exports.generateEnvVars=generateEnvVars;

var getEngineConfigByPlatform=function getEngineConfigByPlatform(c,platform,ignoreMissingError){
var selectedEngineKey;
if(c.buildConfig&&!!platform&&c.runtime.engineConfigs){
selectedEngineKey=c.program.engine||(0,_common.getConfigProp)(c,platform,'engine');
var selectedEngine=c.runtime.engineConfigs[selectedEngineKey];
if(!selectedEngine&&!ignoreMissingError){
(0,_logger.logDebug)("ERROR: Engine: "+selectedEngineKey+" does not exists or is not registered "+new Error());

}
return selectedEngine;
}
return null;
};exports.getEngineConfigByPlatform=getEngineConfigByPlatform;

var getPlatformExtensions=function getPlatformExtensions(c){var excludeServer=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var addDotPrefix=arguments.length>2&&arguments[2]!==undefined?arguments[2]:false;var
engine=c.runtime.engine;
var output;
var platforms=engine.PLATFORMS;
if(addDotPrefix){
output=platforms[c.platform].extenstions.map(function(v){return"."+v;}).filter(function(ext){return!excludeServer||!ext.includes('server.');});
}else{
output=platforms[c.platform].extenstions.filter(function(ext){return!excludeServer||!ext.includes('server.');});
}
return output;
};exports.getPlatformExtensions=getPlatformExtensions;

var getEngineRunnerByPlatform=function getEngineRunnerByPlatform(c,platform){
var selectedEngine=getEngineConfigByPlatform(c,platform);
return ENGINES[selectedEngine==null?void 0:selectedEngine.id];
};exports.getEngineRunnerByPlatform=getEngineRunnerByPlatform;









var getEngineTask=function getEngineTask(task,tasks,customTasks){
var customTask=customTasks==null?void 0:customTasks[task];
if(customTask)return customTask;
var tsk;
var taskCleaned=task.split(' ')[0];
tsk=tasks[task];
if(!tsk){
tsk=tasks[taskCleaned];
}
return tsk;
};exports.getEngineTask=getEngineTask;

var hasEngineTask=function hasEngineTask(task,tasks,isProjectScope){var _getEngineTask;return(
isProjectScope?!!getEngineTask(task,tasks):(_getEngineTask=getEngineTask(task,tasks))==null?void 0:_getEngineTask.isGlobalScope);};exports.hasEngineTask=hasEngineTask;

var getEngineSubTasks=function getEngineSubTasks(task,tasks,exactMatch){return Object.values(tasks).filter(function(v){return exactMatch?v.task.split(' ')[0]===task:v.task.startsWith(task);});};exports.getEngineSubTasks=getEngineSubTasks;

var getEngineRunner=function getEngineRunner(c,task,customTasks){
if(customTasks==null?void 0:customTasks[task]){
return ENGINES[ENGINE_CORE];
}

var selectedEngine=getEngineConfigByPlatform(c,c.platform);var
configExists=c.paths.project.configExists;
if(!selectedEngine){
if(ENGINES[ENGINE_CORE].hasTask(task,configExists))return ENGINES[ENGINE_CORE];

throw new Error("Cound not find suitable executor for task "+(0,_logger.chalk)().white(task));
}
c.runtime.engineConfig=selectedEngine;
var engine=ENGINES[selectedEngine==null?void 0:selectedEngine.id];
if(!engine){
if(ENGINES[ENGINE_CORE].hasTask(task,configExists))return ENGINES[ENGINE_CORE];
throw new Error("Cound not find active engine with id "+(selectedEngine==null?void 0:selectedEngine.id)+". Available engines:\n        "+
Object.keys(ENGINES).join(', '));
}
if(engine.hasTask(task,configExists))return engine;
if(ENGINES[ENGINE_CORE].hasTask(task,configExists))return ENGINES[ENGINE_CORE];

throw new Error("Cound not find suitable executor for task "+(0,_logger.chalk)().white(task));
};exports.getEngineRunner=getEngineRunner;

var getRegisteredEngines=function getRegisteredEngines(){return REGISTERED_ENGINES;};exports.getRegisteredEngines=getRegisteredEngines;
//# sourceMappingURL=index.js.map