var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportWeb=exports.deployWeb=exports.configureWebProject=exports.buildWeb=exports.runWebpackServer=exports.configureCoreWebProject=exports.getModuleConfigs=exports.waitForWebpack=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));
var _path=_interopRequireDefault(require("path"));
var _betterOpn=_interopRequireDefault(require("better-opn"));
var _axios=_interopRequireDefault(require("axios"));
var _ip=_interopRequireDefault(require("ip"));
var _commandExists=_interopRequireDefault(require("command-exists"));
var _fileutils=require("../core/systemManager/fileutils");
var _exec=require("../core/systemManager/exec");
var _common=require("../core/common");



















var _resolve=require("../core/resolve");
var _platformManager=require("../core/platformManager");
var _logger=require("../core/systemManager/logger");










var _projectParser=require("../core/projectManager/projectParser");



var _engineManager=require("../core/engineManager");
var _pluginManager=require("../core/pluginManager");
var _webTools=require("../core/deployManager/webTools");



var _utils=require("../core/utils");

var _constants=require("../core/constants");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 WEBPACK=_path.default.join(_constants.RNV_NODE_MODULES_DIR,'webpack/bin/webpack.js');
var WEBPACK_DEV_SERVER=_path.default.join(_constants.RNV_NODE_MODULES_DIR,'webpack-dev-server/bin/webpack-dev-server.js');

var waitForWebpack=function waitForWebpack(c){var suffix,attempts,maxAttempts,CHECK_INTEVAL,extendConfig,devServerHost,url,_args=arguments;return _regenerator.default.async(function waitForWebpack$(_context){while(1){switch(_context.prev=_context.next){case 0:suffix=_args.length>1&&_args[1]!==undefined?_args[1]:'assets/bundle.js';
(0,_logger.logTask)('waitForWebpack',"port:"+c.runtime.port);
attempts=0;
maxAttempts=10;
CHECK_INTEVAL=2000;


extendConfig=(0,_common.getConfigProp)(c,c.platform,'webpackConfig',{});
devServerHost=(0,_utils.getValidLocalhost)(extendConfig.devServerHost,c.runtime.localhost);
url="http://"+devServerHost+":"+c.runtime.port+"/"+suffix;return _context.abrupt("return",

new Promise(function(resolve,reject){
var interval=setInterval(function(){
_axios.default.
get(url).
then(function(res){
if(res.status===200){
clearInterval(interval);

return resolve(true);
}
attempts++;
if(attempts===maxAttempts){
clearInterval(interval);

return reject(
"Can't connect to webpack. Try restarting it.");

}
}).
catch(function(){
attempts++;
if(attempts>maxAttempts){
clearInterval(interval);

return reject(
"Can't connect to webpack. Try restarting it.");

}
});
},CHECK_INTEVAL);
}));case 9:case"end":return _context.stop();}}},null,null,null,Promise);};exports.waitForWebpack=waitForWebpack;


var getModuleConfigs=function getModuleConfigs(c){
var modulePaths=[];
var moduleAliases={};

var doNotResolveModulePaths=[];


(0,_pluginManager.parsePlugins)(c,c.platform,function(plugin,pluginPlat,key){
var webpackConfig=plugin.webpack||plugin.webpackConfig;

if(webpackConfig){
if(webpackConfig.modulePaths){
if(webpackConfig.modulePaths===false){

}else if(webpackConfig.modulePaths===true){
modulePaths.push("node_modules/"+key);
}else{
webpackConfig.modulePaths.forEach(function(v){
if(typeof v==='string'){
modulePaths.push(v);
}else if(v==null?void 0:v.projectPath){
doNotResolveModulePaths.push(_path.default.join(c.paths.project.dir,v.projectPath));
}
});
}
}
if(webpackConfig.moduleAliases){
if(webpackConfig.moduleAliases===true){
moduleAliases[key]=(0,_resolve.doResolvePath)(key,true,{},c.paths.project.nodeModulesDir);
}else{
Object.keys(webpackConfig.moduleAliases).forEach(function(aKey){
var mAlias=webpackConfig.moduleAliases[aKey];
if(typeof mAlias==='string'){
moduleAliases[key]=(0,_resolve.doResolvePath)(mAlias,true,{},c.paths.project.nodeModulesDir);
}else if(mAlias.path){
moduleAliases[key]=_path.default.join(c.paths.project.dir,mAlias.path);
}else if(mAlias.projectPath){
moduleAliases[key]=_path.default.join(c.paths.project.dir,mAlias.projectPath);
}
});
}
}
}
},true);

var moduleAliasesArray=[];
Object.keys(moduleAliases).forEach(function(key){
moduleAliasesArray.push(key+":"+moduleAliases[key]);
});

modulePaths=modulePaths.
map(function(v){return(0,_resolve.doResolvePath)(v,true,{},c.paths.project.dir);}).
concat(doNotResolveModulePaths).
concat([c.paths.project.assets.dir]).
filter(Boolean);


return{modulePaths:modulePaths,moduleAliases:moduleAliases,moduleAliasesArray:moduleAliasesArray};
};exports.getModuleConfigs=getModuleConfigs;

var _generateWebpackConfigs=function _generateWebpackConfigs(c,subFolderName){
(0,_logger.logTask)('_generateWebpackConfigs');var
platform=c.platform;
var appFolder=(0,_common.getPlatformBuildDir)(c);
var appFolderServer=_path.default.join(appFolder,subFolderName);var _getModuleConfigs=


getModuleConfigs(c),modulePaths=_getModuleConfigs.modulePaths,moduleAliases=_getModuleConfigs.moduleAliases;

var modulePath=_path.default.join(appFolder,'modules.json');
var externalModulePaths=[];
var localModulePaths=[];
if((0,_fileutils.fsExistsSync)(modulePath)){
var modules=(0,_fileutils.readObjectSync)(modulePath);
externalModulePaths=modules.externalPaths;
localModulePaths=modules.localPaths;
if(modules.aliases){
moduleAliases=_objectSpread(_objectSpread({},modules.aliases),moduleAliases);
}
}

modulePaths=modulePaths.
concat(externalModulePaths.map(function(v){return(0,_resolve.doResolvePath)(v,true,{},c.paths.project.nodeModulesDir);})).
concat(localModulePaths.map(function(v){return _path.default.join(c.paths.project.dir,v);})).
filter(Boolean);


var extendConfig=(0,_common.getConfigProp)(c,platform,'webpackConfig',{});
var entryFile=(0,_common.getConfigProp)(c,platform,'entryFile','index.web');
var title=(0,_common.getAppTitle)(c,platform);
var analyzer=(0,_common.getConfigProp)(c,platform,'analyzer')||c.program.analyzer;

if(!(0,_fileutils.fsExistsSync)(appFolderServer)){
(0,_fileutils.mkdirSync)(appFolderServer);
}











var assetVersion='';
var versionedAssets=(0,_common.getConfigProp)(c,platform,'versionedAssets',false);
if(versionedAssets){
assetVersion="-"+(0,_common.getAppVersion)(c,platform);
}
var timestampAssets=(0,_common.getConfigProp)(c,platform,'timestampAssets',false);
if(timestampAssets){
assetVersion="-"+c.runtime.timestamp;
}

var bundleAssets=(0,_common.getConfigProp)(c,c.platform,'bundleAssets',false);

var obj=_objectSpread({
modulePaths:modulePaths,
moduleAliases:moduleAliases,
analyzer:analyzer,
entryFile:entryFile,
title:title,
assetVersion:assetVersion,

buildFolder:bundleAssets?_constants.RNV_PROJECT_DIR_NAME:_constants.RNV_SERVER_DIR_NAME,
extensions:(0,_engineManager.getPlatformExtensions)(c,true)},

extendConfig);


var extendJs="\n    module.exports = "+
JSON.stringify(obj,null,2);

(0,_fileutils.fsWriteFileSync)(_path.default.join(appFolder,'webpack.extend.js'),extendJs);
};

var buildWeb=function buildWeb(c){var _c$program,debug,debugIp,platform,debugVariables;return _regenerator.default.async(function buildWeb$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_c$program=
c.program,debug=_c$program.debug,debugIp=_c$program.debugIp;
platform=c.platform;
(0,_logger.logTask)('buildWeb');

debugVariables='';

if(debug){
(0,_logger.logInfo)("Starting a remote debugger build with ip "+(
debugIp||
_ip.default.address())+". If this IP is not correct, you can always override it with --debugIp");

debugVariables+="DEBUG=true DEBUG_IP="+(debugIp||_ip.default.address());
}_context2.next=7;return _regenerator.default.awrap(

(0,_exec.executeAsync)(c,"npx cross-env PLATFORM="+platform+" NODE_ENV=production "+
debugVariables+" node "+
WEBPACK+" -p --config ./platformBuilds/"+c.runtime.appId+"_"+platform+"/webpack.config.prod.js",{}));case 7:




(0,_logger.logSuccess)("Your Build is located in "+
(0,_logger.chalk)().cyan(
_path.default.join((0,_common.getPlatformProjectDir)(c)))+" .");return _context2.abrupt("return",


true);case 9:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.buildWeb=buildWeb;


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

platform=c.platform;

c.runtime.platformBuildsProjectPath=(0,_common.getPlatformProjectDir)(c);if(

(0,_platformManager.isPlatformActive)(c,platform)){_context3.next=5;break;}return _context3.abrupt("return");case 5:_context3.next=7;return _regenerator.default.awrap(

(0,_projectParser.copyAssetsFolder)(c,platform));case 7:

bundleAssets=(0,_common.getConfigProp)(c,platform,'bundleAssets')===true;_context3.next=10;return _regenerator.default.awrap(

configureCoreWebProject(c,bundleAssets?_constants.RNV_PROJECT_DIR_NAME:_constants.RNV_SERVER_DIR_NAME));case 10:return _context3.abrupt("return",

(0,_projectParser.copyBuildsFolder)(c,platform));case 11:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.configureWebProject=configureWebProject;


var configureCoreWebProject=function configureCoreWebProject(c){var subFolderName,_args4=arguments;return _regenerator.default.async(function configureCoreWebProject$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:subFolderName=_args4.length>1&&_args4[1]!==undefined?_args4[1]:'';
(0,_logger.logTask)('configureCoreWebProject');
_generateWebpackConfigs(c,subFolderName);
_parseCssSync(c,subFolderName);case 4:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.configureCoreWebProject=configureCoreWebProject;


var _parseCssSync=function _parseCssSync(c,subFolderName){
var appFolder=(0,_common.getPlatformBuildDir)(c);
var timestampPathsConfig=(0,_common.getTimestampPathsConfig)(c,c.platform);
var backgroundColor=(0,_common.getConfigProp)(c,c.platform,'backgroundColor');

var injects=[
{
pattern:'{{PLUGIN_COLORS_BG}}',
override:(0,_common.sanitizeColor)(
backgroundColor,
'backgroundColor').
hex}];



(0,_common.addSystemInjects)(c,injects);

(0,_fileutils.writeCleanFile)(
(0,_common.getBuildFilePath)(c,c.platform,'project/app.css'),
_path.default.join(appFolder,subFolderName,'app.css'),
injects,
timestampPathsConfig,c);

};


var runWebpackServer=function runWebpackServer(c,enableRemoteDebugger){var port,platform,devServerHost,extendConfig,isPortActive,bundleAssets,resetCompleted;return _regenerator.default.async(function runWebpackServer$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
port=c.runtime.port;
platform=c.platform;
(0,_logger.logTask)('runWeb',"port:"+port+" debugger:"+!!enableRemoteDebugger);

devServerHost=c.runtime.localhost;

extendConfig=(0,_common.getConfigProp)(c,c.platform,'webpackConfig',{});
devServerHost=(0,_utils.getValidLocalhost)(
extendConfig.devServerHost,
c.runtime.localhost);_context5.next=8;return _regenerator.default.awrap(


(0,_common.checkPortInUse)(c,platform,port));case 8:isPortActive=_context5.sent;
bundleAssets=(0,_common.getConfigProp)(c,c.platform,'bundleAssets',false);if(

isPortActive){_context5.next=19;break;}
(0,_logger.logInfo)("Your "+
(0,_logger.chalk)().white(
platform)+" devServerHost "+
(0,_logger.chalk)().white(
devServerHost)+" at port "+
(0,_logger.chalk)().white(
port)+" is not running. Starting it up for you...");_context5.next=14;return _regenerator.default.awrap(


_runWebBrowser(c,platform,devServerHost,port,false));case 14:
if(!bundleAssets){
(0,_logger.logSummary)('BUNDLER STARTED');
}_context5.next=17;return _regenerator.default.awrap(
runWebDevServer(c,enableRemoteDebugger));case 17:_context5.next=32;break;case 19:_context5.next=21;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 21:resetCompleted=_context5.sent;if(!

resetCompleted){_context5.next=30;break;}_context5.next=25;return _regenerator.default.awrap(
_runWebBrowser(c,platform,devServerHost,port,false));case 25:
if(!bundleAssets){
(0,_logger.logSummary)('BUNDLER STARTED');
}_context5.next=28;return _regenerator.default.awrap(
runWebDevServer(c,enableRemoteDebugger));case 28:_context5.next=32;break;case 30:_context5.next=32;return _regenerator.default.awrap(

_runWebBrowser(c,platform,devServerHost,port,true));case 32:case"end":return _context5.stop();}}},null,null,null,Promise);};exports.runWebpackServer=runWebpackServer;




var _runWebBrowser=function _runWebBrowser(c,platform,devServerHost,port,alreadyStarted){return new Promise(function(resolve){
(0,_logger.logTask)(
'_runWebBrowser',"ip:"+devServerHost+" port:"+port+" openBrowser:"+!!c.runtime.shouldOpenBrowser);

if(!c.runtime.shouldOpenBrowser)return resolve();
var wait=waitForWebpack(c).
then(function(){
(0,_betterOpn.default)("http://"+devServerHost+":"+port+"/");
}).
catch(function(e){
(0,_logger.logWarning)(e);
});
if(alreadyStarted)return wait;
return resolve();
});};

var runWebDevServer=function runWebDevServer(c,enableRemoteDebugger){var _c$program2,debug,debugIp,appFolder,wpPublic,wpConfig,debugVariables,lineBreaks,resolvedDebugIp,debugUrl,_command,devServerHost,url,WPS_ALTERNATIVE,wps,command;return _regenerator.default.async(function runWebDevServer$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
(0,_logger.logTask)('runWebDevServer');_c$program2=
c.program,debug=_c$program2.debug,debugIp=_c$program2.debugIp;


appFolder=(0,_common.getPlatformBuildDir)(c);
wpPublic=_path.default.join(appFolder,'server');
wpConfig=_path.default.join(appFolder,'webpack.config.dev.js');

debugVariables='';
lineBreaks='\n\n\n';if(!(
debug||enableRemoteDebugger)){_context6.next=33;break;}_context6.prev=8;_context6.next=11;return _regenerator.default.awrap(

(0,_commandExists.default)('weinre'));case 11:

resolvedDebugIp=debugIp||_ip.default.address();
(0,_logger.logInfo)("Starting a remote debugger build with ip "+

resolvedDebugIp+". If this IP is not correct, you can always override it with --debugIp");

debugVariables+="DEBUG=true DEBUG_IP="+resolvedDebugIp;
lineBreaks='\n';
debugUrl=(0,_logger.chalk)().cyan("http://"+resolvedDebugIp+":"+_constants.WEINRE_PORT+"/client/#"+c.platform);

_command="weinre --boundHost -all- --httpPort "+_constants.WEINRE_PORT;
(0,_exec.executeAsync)(c,_command,{stdio:'inherit',silent:true});_context6.prev=18;_context6.next=21;return _regenerator.default.awrap(


(0,_common.waitForUrl)("http://"+resolvedDebugIp+":"+_constants.WEINRE_PORT));case 21:
(0,_logger.logRaw)("\n\n          Debugger running at: "+

debugUrl);
(0,_betterOpn.default)("http://"+resolvedDebugIp+":"+_constants.WEINRE_PORT+"/client/#"+c.platform);_context6.next=28;break;case 25:_context6.prev=25;_context6.t0=_context6["catch"](18);

(0,_logger.logError)(_context6.t0);case 28:_context6.next=33;break;case 30:_context6.prev=30;_context6.t1=_context6["catch"](8);


(0,_logger.logWarning)("You are missing weinre. Skipping debug. install via "+(0,_logger.chalk)().white('npm i -g weinre'));case 33:



devServerHost=(0,_utils.getValidLocalhost)((0,_common.getConfigProp)(c,c.platform,'webpackConfig',{}).devServerHost,c.runtime.localhost);

url=(0,_logger.chalk)().cyan("http://"+devServerHost+":"+c.runtime.port);
(0,_logger.logRaw)(lineBreaks+"Dev server running at: "+url+"\n\n");




WPS_ALTERNATIVE=(0,_resolve.doResolve)('webpack-dev-server')+"/bin/webpack-dev-server.js";

wps='webpack-dev-server';
if((0,_fileutils.fsExistsSync)(WEBPACK_DEV_SERVER)){
wps=WEBPACK_DEV_SERVER;
}else if((0,_fileutils.fsExistsSync)(WPS_ALTERNATIVE)){
wps=WPS_ALTERNATIVE;
}else{
(0,_logger.logWarning)("cannot find installed webpack-dev-server. looked in following locations:\n"+
(0,_logger.chalk)().white(WEBPACK_DEV_SERVER)+",\n"+
(0,_logger.chalk)().white(WPS_ALTERNATIVE)+"\nwill try to use globally installed one");

}


command="npx cross-env PLATFORM="+c.platform+" "+
debugVariables+" "+
wps+" -d --devtool source-map --config "+
wpConfig+"  --inline --hot --colors --content-base "+

wpPublic+" --history-api-fallback --port "+
c.runtime.port+" --mode=development";_context6.prev=40;_context6.next=43;return _regenerator.default.awrap(

(0,_exec.executeAsync)(c,command,{
stdio:'inherit',
silent:true}));case 43:





(0,_logger.logDebug)('runWebDevServer: running');_context6.next=50;break;case 46:_context6.prev=46;_context6.t2=_context6["catch"](40);

(0,_logger.logDebug)(_context6.t2);return _context6.abrupt("return",
true);case 50:case"end":return _context6.stop();}}},null,null,[[8,30],[18,25],[40,46]],Promise);};



var deployWeb=function deployWeb(c){
(0,_logger.logTask)('deployWeb');var
platform=c.platform;

return(0,_webTools.selectWebToolAndDeploy)(c,platform);
};exports.deployWeb=deployWeb;

var exportWeb=function exportWeb(c){
(0,_logger.logTask)('exportWeb');var
platform=c.platform;

return(0,_webTools.selectWebToolAndExport)(c,platform);
};exports.exportWeb=exportWeb;
//# sourceMappingURL=index.js.map