var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.deployWeb=exports.runWebDevServer=exports.configureWebProject=exports.runWeb=exports.buildWeb=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _openBrowser=_interopRequireDefault(require("react-dev-utils/openBrowser"));
var _ip=_interopRequireDefault(require("ip"));
var _exec=require("../../systemTools/exec");
var _common=require("../../common");













var _projectParser=require("../../projectTools/projectParser");
var _fileutils=require("../../systemTools/fileutils");
var _pluginTools=require("../../pluginTools");
var _webTools=require("../../deployTools/webTools");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(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}


var isRunningOnWindows=process.platform==='win32';

var _generateWebpackConfigs=function _generateWebpackConfigs(c){
var appFolder=(0,_common.getAppFolder)(c,c.platform);
var templateFolder=(0,_common.getAppTemplateFolder)(c,c.platform);var

plugins=c.buildConfig.plugins;
var modulePaths=[];
var moduleAliasesString='';
var moduleAliases={};

for(var key in plugins){
var plugin=(0,_pluginTools.getMergedPlugin)(c,key,plugins);
if(!plugin){

}else if(plugin.webpack){
if(plugin.webpack.modulePaths){
if(plugin.webpack.modulePaths===true){
modulePaths.push("node_modules/"+key);
}else{
modulePaths=modulePaths.concat(plugin.webpack.modulePaths);
}
}
if(plugin.webpack.moduleAliases){
if(plugin.webpack.moduleAliases===true){
moduleAliasesString+="'"+key+"': {\n                  projectPath: 'node_modules/"+
key+"'\n                },";

moduleAliases[key]={projectPath:"node_modules/"+key};
}else{
for(var aKey in plugin.webpack.moduleAliases){
if(typeof plugin.webpack.moduleAliases[aKey]==='string'){
moduleAliasesString+="'"+aKey+"': '"+plugin.webpack.moduleAliases[aKey]+"',";
moduleAliases[key]=plugin.webpack.moduleAliases[aKey];
}else{
moduleAliasesString+="'"+aKey+"': {projectPath: '"+plugin.webpack.moduleAliases[aKey].projectPath+"'},";
if(plugin.webpack.moduleAliases[aKey].projectPath){
moduleAliases[key]={projectPath:plugin.webpack.moduleAliases[aKey].projectPath};
}
}
}
}
}
}
}

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

(0,_fileutils.copyFileSync)(
_path.default.join(templateFolder,'_privateConfig',env==='production'?'webpack.config.js':'webpack.config.dev.js'),
_path.default.join(appFolder,'webpack.config.js'));


var obj=_objectSpread({
modulePaths:modulePaths,
moduleAliases:moduleAliases,
analyzer:analyzer,
entryFile:entryFile,
title:title},
extendConfig);


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

_fs.default.writeFileSync(_path.default.join(appFolder,'webpack.extend.js'),extendJs);
};

var buildWeb=function buildWeb(c,platform){return new Promise(function(resolve,reject){var _c$program=
c.program,debug=_c$program.debug,debugIp=_c$program.debugIp,maxErrorLength=_c$program.maxErrorLength;
(0,_common.logTask)("buildWeb:"+platform);

var appFolder=(0,_common.getAppFolder)(c,platform);

var debugVariables='';

if(debug){
(0,_common.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());
}

_generateWebpackConfigs(c);

var wbp=(0,_common.resolveNodeModulePath)(c,'webpack/bin/webpack.js');

(0,_exec.executeAsync)(c,"npx cross-env NODE_ENV=production "+debugVariables+" node "+wbp+" -p --config ./platformBuilds/"+c.runtime.appId+"_"+platform+"/webpack.config.js").
then(function(){
(0,_common.logSuccess)("Your Build is located in "+_chalk.default.white(_path.default.join(appFolder,'public'))+" .");
resolve();
}).
catch(function(e){return reject(e);});
});};exports.buildWeb=buildWeb;

var configureWebProject=function configureWebProject(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("configureWebProject:"+platform);

if(!(0,_common.isPlatformActive)(c,platform,resolve))return;

(0,_projectParser.copyBuildsFolder)(c,platform).
then(function(){return configureProject(c,platform);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};exports.configureWebProject=configureWebProject;

var configureProject=function configureProject(c,platform,appFolderName){return _regenerator.default.async(function configureProject$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_common.logTask)("configureProject:"+platform);_context.next=3;return _regenerator.default.awrap(

(0,_projectParser.copyAssetsFolder)(c,platform));case 3:case"end":return _context.stop();}}});};


var runWeb=function runWeb(c,platform,port){return new Promise(function(resolve,reject){
(0,_common.logTask)("runWeb:"+platform+":"+port);

var extendConfig=(0,_common.getConfigProp)(c,c.platform,'webpackConfig',{});
var devServerHost=extendConfig.devServerHost||'0.0.0.0';


if(isRunningOnWindows&&devServerHost==='0.0.0.0'){
devServerHost='127.0.0.1';
}

(0,_common.checkPortInUse)(c,platform,port).
then(function(isPortActive){
if(!isPortActive){
(0,_common.logInfo)("Looks like your "+
_chalk.default.white(platform)+" devServerHost "+_chalk.default.white(devServerHost)+" at port "+_chalk.default.white(
port)+" is not running. Starting it up for you...");


_runWebBrowser(c,platform,devServerHost,port,500).
then(function(){return runWebDevServer(c,platform,port);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
}else{
(0,_common.logInfo)("Looks like your "+
_chalk.default.white(platform)+" devServerHost at port "+_chalk.default.white(
port)+" is already running. ReNative Will use it!");


_runWebBrowser(c,platform,devServerHost,port).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
}
}).
catch(function(e){return reject(e);});
});};exports.runWeb=runWeb;

var _runWebBrowser=function _runWebBrowser(c,platform,devServerHost,port){var delay=arguments.length>4&&arguments[4]!==undefined?arguments[4]:0;return new Promise(function(resolve,reject){
(0,_common.waitForWebpack)(c,port).
then(function(){return(0,_openBrowser.default)("http://"+devServerHost+":"+port+"/");}).
catch(_common.logError);
resolve();
});};

var runWebDevServer=function runWebDevServer(c,platform,port){return new Promise(function(resolve,reject){
(0,_common.logTask)("runWebDevServer:"+platform);

var appFolder=(0,_common.getAppFolder)(c,platform);
var wpPublic=_path.default.join(appFolder,'public');
var wpConfig=_path.default.join(appFolder,'webpack.config.js');

_generateWebpackConfigs(c);
var command="webpack-dev-server -d --devtool source-map --config "+wpConfig+"  --inline --hot --colors --content-base "+wpPublic+" --history-api-fallback --port "+port+" --mode=development";
return(0,_exec.executeAsync)(c,command,{stdio:'inherit',silent:true});
});};exports.runWebDevServer=runWebDevServer;

var deployWeb=function deployWeb(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("deployWeb:"+platform);
(0,_webTools.selectWebToolAndDeploy)(c,platform).then(resolve).catch(reject);
});};exports.deployWeb=deployWeb;