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.generateEnvVars=exports.loadEngines=exports.registerAllPlatformEngines=exports.registerMissingPlatformEngines=exports.configureEngines=exports.generateEngineTasks=exports.generateEngineExtensions=exports.registerEngineExtension=exports.registerEngine=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));
var _path=_interopRequireDefault(require("path"));
var _constants=require("../constants");
var _logger=require("../systemManager/logger");
var _common=require("../common");
var _resolve=require("../systemManager/resolve");
var _utils=require("../systemManager/utils");
var _fileutils=require("../systemManager/fileutils");
var _npmUtils=require("../systemManager/npmUtils");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(Object(source),true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}

var ENGINE_CORE='engine-core';

var registerEngine=function registerEngine(c,engine,platform,engConfig){return _regenerator.default.async(function registerEngine$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)("registerEngine:"+engine.config.id);
c.runtime.enginesById[engine.config.id]=engine;
engine.initializeRuntimeConfig(c);
c.runtime.enginesByIndex.push(engine);
if(engConfig==null?void 0:engConfig.packageName){
engine.rootPath=_resolvePkgPath(c,engConfig.packageName);
engine.originalTemplatePlatformsDir=_path.default.join(engine.rootPath,'templates/platforms');
engine.originalTemplateAssetsDir=_path.default.join(engine.rootPath,'templates/assets');
engine.originalTemplatePlatformProjectDir=_path.default.join(
engine.originalTemplatePlatformsDir,
engine.projectDirName);

}
_registerEnginePlatform(c,platform,engine);case 6:case"end":return _context.stop();}}},null,null,null,Promise);};exports.registerEngine=registerEngine;


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

var registerEngineExtension=function registerEngineExtension(ext,eExt){var extras=arguments.length>2&&arguments[2]!==undefined?arguments[2]:[];
var e1=ext?ext+".":'';
var e2=eExt?eExt+".":'';
var extArr;
if(eExt){
extArr=[""+
e1+e2+"jsx",e1+"jsx",""+e1+e2+"js",e1+"js",""+
e1+e2+"tsx",e1+"tsx",""+e1+e2+"ts",e1+"ts"].concat((0,_toConsumableArray2.default)(
extras));

}else{
extArr=[
e1+"jsx",e1+"js",
e1+"tsx",e1+"ts"].concat((0,_toConsumableArray2.default)(
extras));

}

return extArr;
};exports.registerEngineExtension=registerEngineExtension;

var generateEngineExtensions=function generateEngineExtensions(exts,config){var
id=config.id,engineExtension=config.engineExtension;
var extArr=(0,_toConsumableArray2.default)(registerEngineExtension(id));
exts.forEach(function(ext){
extArr=extArr.concat(registerEngineExtension(ext,engineExtension));
});
extArr=extArr.concat(registerEngineExtension(null,null,['mjs','json','wasm']));
return extArr;
};exports.generateEngineExtensions=generateEngineExtensions;

var generateEngineTasks=function generateEngineTasks(taskArr){
var tasks={};
taskArr.forEach(function(taskInstance){
tasks[taskInstance.task]=taskInstance;
});
return tasks;
};exports.generateEngineTasks=generateEngineTasks;

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&&!c.runtime.skipPackageUpdate&&!c.program.skipDependencyCheck){
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])+"=>"+(0,_logger.chalk)().green(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 registerAllPlatformEngines=function registerAllPlatformEngines(c){var registerEngineList;return _regenerator.default.async(function registerAllPlatformEngines$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('registerAllPlatformEngines');
registerEngineList=[];
c.buildConfig.defaults.supportedPlatforms.forEach(function(platform){
registerEngineList.push(
_registerPlatformEngine(c,platform));

});if(!

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


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


var loadEngines=function loadEngines(c){var _c$buildConfig;var engines,enginesToInstall,readyEngines;return _regenerator.default.async(function loadEngines$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('loadEngines');
engines=(_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.engines;

enginesToInstall=[];
readyEngines=[];if(!
engines){_context5.next=17;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 engVer=(0,_utils.getScopedVersion)(c,k,engines[k],'engineTemplates');
if(engVer){
enginesToInstall.push({
key:k,
version:engVer});

}
}else{
readyEngines.push(k);
}
});if(!
enginesToInstall.length){_context5.next=14;break;}
(0,_logger.logInfo)("Some engines not installed in your project:\n"+
enginesToInstall.map(function(v){return"> "+v.key+"@"+v.version;}).join('\n')+"\n ADDING TO PACKAGE.JSON...DONE");_context5.next=10;return _regenerator.default.awrap(


(0,_npmUtils.checkAndCreateProjectPackage)(c));case 10:
enginesToInstall.forEach(function(v){
c.files.project.package.devDependencies[v.key]=v.version;
(0,_fileutils.writeFileSync)(c.paths.project.package,c.files.project.package);
});_context5.next=13;return _regenerator.default.awrap(
(0,_npmUtils.installPackageDependencies)(c));case 13:return _context5.abrupt("return",

loadEngines(c));case 14:


_registerPlatformEngine(c,c.platform);_context5.next=23;break;case 17:if(!
c.files.project.config){_context5.next=23;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'};


c.buildConfig.engines=c.files.project.config.engines;

(0,_fileutils.writeFileSync)(c.paths.project.config,c.files.project.config);return _context5.abrupt("return",
loadEngines(c));case 23:return _context5.abrupt("return",

true);case 24:case"end":return _context5.stop();}}},null,null,null,Promise);};exports.loadEngines=loadEngines;


var _getMergedEngineConfigs=function _getMergedEngineConfigs(c){var _c$buildConfig2,_c$buildConfig3;
var engines=(_c$buildConfig2=c.buildConfig)==null?void 0:_c$buildConfig2.engines;
var engineTemplates=(_c$buildConfig3=c.buildConfig)==null?void 0:_c$buildConfig3.engineTemplates;
var mergedEngineConfigs={};
Object.keys(engineTemplates).forEach(function(packageName){
mergedEngineConfigs[packageName]=_objectSpread({
packageName:packageName},
engineTemplates[packageName]);

});

var engineTemplatesKeys=Object.keys(engineTemplates);
if(engines){
Object.keys(engines).forEach(function(enginePackageName){
var engineVal=engines[enginePackageName];
if(engineVal==='source:rnv'){
if(!engineTemplatesKeys.includes(enginePackageName)){
(0,_logger.logWarning)("Engine "+enginePackageName+" not found in default engineTemplates");
}
}else{
mergedEngineConfigs[enginePackageName]=engineVal;
}
});
}
return mergedEngineConfigs;
};

var _getEngineConfigByPlatform=function _getEngineConfigByPlatform(c,platform){
var mergedEngineConfigs=_getMergedEngineConfigs(c);
var engineId=c.program.engine||(0,_common.getConfigProp)(c,platform,'engine');
var selectedEngineConfig;
Object.values(mergedEngineConfigs).forEach(function(engineConfig){
if(engineConfig.id===engineId){
selectedEngineConfig=engineConfig;
}
});

return selectedEngineConfig;
};

var _resolvePkgPath=function _resolvePkgPath(c,packageName){
if(_constants.IS_LINKED){

var pkgPathLocal=require.resolve(packageName,{paths:[_path.default.join(_constants.RNV_HOME_DIR,'..')]});
pkgPathLocal=pkgPathLocal.replace('/dist/index.js','');
return pkgPathLocal;
}
var pkgPath=_path.default.join(c.paths.project.dir,'node_modules',packageName);
if((0,_fileutils.fsExistsSync)(pkgPath)){
return pkgPath;
}
pkgPath=_path.default.join(c.paths.project.dir,'../..','node_modules',packageName);
if((0,_fileutils.fsExistsSync)(pkgPath)){
return pkgPath;
}
pkgPath=require.resolve(packageName);
return pkgPath;
};


var _registerPlatformEngine=function _registerPlatformEngine(c,platform){

if(platform===true||!platform)return;
var selectedEngineConfig=_getEngineConfigByPlatform(c,platform);
if(selectedEngineConfig){
var existingEngine=c.runtime.enginesById[selectedEngineConfig.id];
if(!existingEngine){var _require;












registerEngine(c,(_require=require(
_resolvePkgPath(c,selectedEngineConfig.packageName)))==null?void 0:_require.
default,
platform,selectedEngineConfig);
}else{
_registerEnginePlatform(c,platform,existingEngine);
}
}else if(platform!==true&&platform){
(0,_logger.logWarning)("Could not find suitable engine for platform "+platform+"\nMaybe you forgot to define platforms."+
platform+".engine in your renative.json?");
}
};

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 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,ignoreMissingError){
var selectedEngine=c.runtime.enginesByPlatform[platform];
if(!selectedEngine&&!ignoreMissingError){
(0,_logger.logDebug)("ERROR: Engine for platform: "+platform+" does not exists or is not registered "+new Error());

}
return selectedEngine;
};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 c.runtime.enginesById[ENGINE_CORE];
}var

configExists=c.paths.project.configExists;

var engine=c.runtime.enginesByPlatform[c.platform];
if(!engine){
if(hasEngineTask(task,c.runtime.enginesById[ENGINE_CORE].tasks,configExists)){
return c.runtime.enginesById[ENGINE_CORE];
}
throw new Error("Cound not find active engine for platform "+c.platform+". Available engines:\n        "+
c.runtime.enginesByIndex.join(', '));
}
if(hasEngineTask(task,engine.tasks,configExists))return engine;
if(hasEngineTask(task,c.runtime.enginesById[ENGINE_CORE].tasks,configExists)){
return c.runtime.enginesById[ENGINE_CORE];
}

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

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