var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.waitForBundler=exports.isBundlerActive=exports.configureMetroConfigs=exports.waitForBundlerIfRequired=exports.startBundlerIfRequired=exports.getPlatformProjectDir=exports.ejectPlatform=exports.getTemplateRootDir=exports.getTemplateProjectDir=exports.getPlatformOutputDir=exports.getPlatformBuildDir=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _axios=_interopRequireDefault(require("axios"));
var _ora=_interopRequireDefault(require("ora"));
var _common=require("../core/common");
var _logger=require("../core/systemManager/logger");
var _fileutils=require("../core/systemManager/fileutils");
var _constants=require("../core/constants");
var _engineManager=require("../core/engineManager");

var keepRNVRunning=false;

var getPlatformBuildDir=function getPlatformBuildDir(c){return(0,_common.getAppFolder)(c);};exports.getPlatformBuildDir=getPlatformBuildDir;

var getPlatformOutputDir=function getPlatformOutputDir(c){
var dir=getPlatformBuildDir(c);
var output;
switch(c.platform){
default:
output=dir;}

return output;
};exports.getPlatformOutputDir=getPlatformOutputDir;

var getTemplateProjectDir=function getTemplateProjectDir(c){
var dir=(0,_common.getTemplateDir)(c);
var output;
switch(c.platform){
default:
output=dir;}

return output;
};exports.getTemplateProjectDir=getTemplateProjectDir;

var getTemplateRootDir=function getTemplateRootDir(c,platform){
var dir=c.paths.project.platformTemplatesDirs[platform];
return dir;
};exports.getTemplateRootDir=getTemplateRootDir;

var ejectPlatform=function ejectPlatform(c,platform,destFolder){
var sourcePlatformDir=getTemplateRootDir(c,platform);
(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(sourcePlatformDir,platform),
destFolder);

(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(sourcePlatformDir,'_shared'),
destFolder);

};exports.ejectPlatform=ejectPlatform;

var getPlatformProjectDir=function getPlatformProjectDir(c){
var dir=getPlatformBuildDir(c);
var output;
switch(c.platform){
default:
output=dir;}

return output;
};exports.getPlatformProjectDir=getPlatformProjectDir;

var startBundlerIfRequired=function startBundlerIfRequired(c,parentTask,originTask){var bundleAssets,isRunning,resetCompleted;return _regenerator.default.async(function startBundlerIfRequired$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('startBundlerIfRequired');
bundleAssets=(0,_common.getConfigProp)(c,c.platform,'bundleAssets');if(!(
bundleAssets===true)){_context.next=4;break;}return _context.abrupt("return");case 4:_context.next=6;return _regenerator.default.awrap(

isBundlerActive(c));case 6:isRunning=_context.sent;if(
isRunning){_context.next=15;break;}_context.next=10;return _regenerator.default.awrap(

(0,_engineManager.executeTask)(c,_constants.TASK_START,parentTask,originTask));case 10:

keepRNVRunning=true;_context.next=13;return _regenerator.default.awrap(
waitForBundler(c));case 13:_context.next=24;break;case 15:_context.next=17;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 17:resetCompleted=_context.sent;if(!
resetCompleted){_context.next=24;break;}_context.next=21;return _regenerator.default.awrap(
(0,_engineManager.executeTask)(c,_constants.TASK_START,parentTask,originTask));case 21:

keepRNVRunning=true;_context.next=24;return _regenerator.default.awrap(
waitForBundler(c));case 24:case"end":return _context.stop();}}},null,null,null,Promise);};exports.startBundlerIfRequired=startBundlerIfRequired;




var waitForBundlerIfRequired=function waitForBundlerIfRequired(c){var bundleAssets;return _regenerator.default.async(function waitForBundlerIfRequired$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
bundleAssets=(0,_common.getConfigProp)(c,c.platform,'bundleAssets');if(!(
bundleAssets===true)){_context2.next=3;break;}return _context2.abrupt("return");case 3:if(!

keepRNVRunning){_context2.next=5;break;}return _context2.abrupt("return",new Promise(function(){}));case 5:return _context2.abrupt("return",
true);case 6:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.waitForBundlerIfRequired=waitForBundlerIfRequired;


var configureMetroConfigs=function configureMetroConfigs(c,platform){var cfPath;return _regenerator.default.async(function configureMetroConfigs$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('configureMetroConfigs');

cfPath=_path.default.join(c.paths.project.dir,'configs',"metro.config."+platform+".js");
if((0,_fileutils.fsExistsSync)(cfPath)){
(0,_logger.logWarning)((0,_logger.chalk)().white(cfPath)+" is DEPRECATED. use withRnvMetro(config) directly in /.metro.config.js");
}



if(!(0,_fileutils.fsExistsSync)(c.paths.project.rnCliConfig)){
(0,_logger.logInfo)("Looks like your rn-cli config file "+
(0,_logger.chalk)().white(
c.paths.project.rnCliConfig)+" is missing! INSTALLING...");


(0,_fileutils.copyFileSync)(
_path.default.join(c.paths.rnv.projectTemplate.dir,_constants.RN_CLI_CONFIG_NAME),
c.paths.project.rnCliConfig);

}case 4:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.configureMetroConfigs=configureMetroConfigs;


var _isBundlerRunning=function _isBundlerRunning(c){var _await$axios$get,data;return _regenerator.default.async(function _isBundlerRunning$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('_isBundlerRunning');_context4.prev=1;_context4.next=4;return _regenerator.default.awrap(

_axios.default.get("http://"+
c.runtime.localhost+":"+c.runtime.port+"/"+(0,_common.getConfigProp)(
c,
c.platform,
'entryFile')+".js"));case 4:_await$axios$get=_context4.sent;data=_await$axios$get.data;if(!


data.includes('import')){_context4.next=9;break;}
(0,_logger.logTask)('_isBundlerRunning','(YES)');return _context4.abrupt("return",
true);case 9:

(0,_logger.logTask)('_isBundlerRunning','(NO)');return _context4.abrupt("return",
false);case 13:_context4.prev=13;_context4.t0=_context4["catch"](1);

(0,_logger.logTask)('_isBundlerRunning','(NO)');return _context4.abrupt("return",
false);case 17:case"end":return _context4.stop();}}},null,null,[[1,13]],Promise);};



var isBundlerActive=function isBundlerActive(c){return _regenerator.default.async(function isBundlerActive$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('isBundlerActive',"(http://"+c.runtime.localhost+":"+c.runtime.port+")");_context5.prev=1;_context5.next=4;return _regenerator.default.awrap(

_axios.default.get("http://"+c.runtime.localhost+":"+c.runtime.port));case 4:return _context5.abrupt("return",
true);case 7:_context5.prev=7;_context5.t0=_context5["catch"](1);return _context5.abrupt("return",

false);case 10:case"end":return _context5.stop();}}},null,null,[[1,7]],Promise);};exports.isBundlerActive=isBundlerActive;



var poll=function poll(fn){var timeout=arguments.length>1&&arguments[1]!==undefined?arguments[1]:10000;var interval=arguments.length>2&&arguments[2]!==undefined?arguments[2]:1000;
var endTime=Number(new Date())+timeout;

var spinner=(0,_ora.default)('Waiting for bundler to finish...').start();
var checkCondition=function checkCondition(resolve,reject){var result;return _regenerator.default.async(function checkCondition$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:_context6.prev=0;_context6.next=3;return _regenerator.default.awrap(

fn());case 3:result=_context6.sent;
if(result){
spinner.succeed();
resolve();
}else if(Number(new Date())<endTime){
setTimeout(checkCondition,interval,resolve,reject);
}else{
spinner.fail("Can't connect to bundler. Try restarting it.");
reject("Can't connect to bundler. Try restarting it.");
}_context6.next=11;break;case 7:_context6.prev=7;_context6.t0=_context6["catch"](0);

spinner.fail("Can't connect to bundler. Try restarting it.");
reject(_context6.t0);case 11:case"end":return _context6.stop();}}},null,null,[[0,7]],Promise);};



return new Promise(checkCondition);
};

var waitForBundler=function waitForBundler(c){return _regenerator.default.async(function waitForBundler$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:return _context7.abrupt("return",poll(function(){return _isBundlerRunning(c);}));case 1:case"end":return _context7.stop();}}},null,null,null,Promise);};exports.waitForBundler=waitForBundler;
//# sourceMappingURL=commonEngine.js.map