var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportWeb=exports.deployWeb=exports.configureWebProject=exports.runWeb=exports.buildWeb=exports.configureCoreWebProject=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 _betterOpn=_interopRequireDefault(require("better-opn"));
var _ip=_interopRequireDefault(require("ip"));
var _exec=require("../../systemTools/exec");
var _common=require("../../common");













var _=require("..");
var _logger=require("../../systemTools/logger");






var _constants=require("../../constants");
var _projectParser=require("../../projectTools/projectParser");



var _fileutils=require("../../systemTools/fileutils");
var _pluginTools=require("../../pluginTools");
var _webTools=require("../../deployTools/webTools");



var _utils=require("../../utils");
var _resolve=require("../../resolve");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 _generateWebpackConfigs=function _generateWebpackConfigs(c,platform){
var appFolder=(0,_common.getAppFolder)(c,platform);
var templateFolder=(0,_common.getAppTemplateFolder)(c,platform);

var modulePaths=[];
var doNotResolveModulePaths=[];
var moduleAliases={};

var modulePath=_path.default.join(c.paths.project.builds.dir,'_shared','modules.json');
var externalModulePaths=[];
var localModulePaths=[];
if(_fs.default.existsSync(modulePath)){
var modules=(0,_fileutils.readObjectSync)(modulePath);
externalModulePaths=modules.externalPaths;
localModulePaths=modules.localPaths;
moduleAliases=modules.aliases;
}


(0,_pluginTools.parsePlugins)(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);

modulePaths=modulePaths.
map(function(v){return(0,_resolve.doResolvePath)(v,true,{},c.paths.project.dir);}).
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);})).
concat(doNotResolveModulePaths).
concat([c.paths.project.assets.dir]).
filter(Boolean);

var env=(0,_common.getConfigProp)(c,platform,'environment');
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;

(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 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 obj=_objectSpread({
modulePaths:modulePaths,
moduleAliases:moduleAliases,
analyzer:analyzer,
entryFile:entryFile,
title:title,
assetVersion:assetVersion,
extensions:(0,_common.getSourceExts)(c,platform)},
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,platform){return new Promise(function(resolve,reject){var _c$program=
c.program,debug=_c$program.debug,debugIp=_c$program.debugIp;
(0,_logger.logTask)("buildWeb:"+platform);

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

var 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());
}

var wbp=(0,_resolve.doResolvePath)('webpack/bin/webpack.js');

(0,_exec.executeAsync)(
c,"npx cross-env PLATFORM="+
platform+" NODE_ENV=production "+debugVariables+" node "+wbp+" -p --config ./platformBuilds/"+c.runtime.appId+"_"+platform+"/webpack.config.js").

then(function(){
(0,_logger.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 _regenerator.default.async(function configureWebProject$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)("configureWebProject:"+platform);if(

(0,_.isPlatformActive)(c,platform)){_context.next=3;break;}return _context.abrupt("return");case 3:_context.next=5;return _regenerator.default.awrap(

(0,_projectParser.copyAssetsFolder)(c,platform));case 5:_context.next=7;return _regenerator.default.awrap(

configureCoreWebProject(c,platform));case 7:return _context.abrupt("return",

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


var configureCoreWebProject=function configureCoreWebProject(c,platform){return _regenerator.default.async(function configureCoreWebProject$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
_generateWebpackConfigs(c,platform);
_parseCssSync(c,platform);case 2:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.configureCoreWebProject=configureCoreWebProject;


var _parseCssSync=function _parseCssSync(c,platform){
var appFolder=(0,_common.getAppFolder)(c,platform);
var stringsPath='public/app.css';
var timestampPathsConfig=(0,_common.getTimestampPathsConfig)(c,platform);
var backgroundColor=(0,_common.getConfigProp)(c,platform,'backgroundColor');
(0,_fileutils.writeCleanFile)(
(0,_common.getBuildFilePath)(c,platform,stringsPath),
_path.default.join(appFolder,stringsPath),
[
{
pattern:'{{PLUGIN_COLORS_BG}}',
override:(0,_common.sanitizeColor)(
backgroundColor,
'backgroundColor').
hex}],


timestampPathsConfig,c);

};


var runWeb=function runWeb(c,platform,port){var devServerHost,extendConfig,isPortActive;return _regenerator.default.async(function runWeb$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)("runWeb:"+platform+":"+port);

devServerHost=c.runtime.localhost;

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











}_context3.next=5;return _regenerator.default.awrap(

(0,_common.checkPortInUse)(c,platform,port));case 5:isPortActive=_context3.sent;if(

isPortActive){_context3.next=14;break;}
(0,_logger.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...");_context3.next=10;return _regenerator.default.awrap(


_runWebBrowser(c,platform,devServerHost,port,false));case 10:_context3.next=12;return _regenerator.default.awrap(
runWebDevServer(c,platform,port));case 12:_context3.next=18;break;case 14:_context3.next=16;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 16:_context3.next=18;return _regenerator.default.awrap(
_runWebBrowser(c,platform,devServerHost,port,true));case 18:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.runWeb=runWeb;



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

if(!c.runtime.shouldOpenBrowser)return resolve();
var wait=(0,_common.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,platform,port){return new Promise(function(resolve,reject){
(0,_logger.logTask)("runWebDevServer:"+platform);var _c$program2=
c.program,debug=_c$program2.debug,debugIp=_c$program2.debugIp;

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

var 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());
}

var command="npx cross-env PLATFORM="+platform+" "+debugVariables+" webpack-dev-server -d --devtool source-map --config "+wpConfig+"  --inline --hot --colors --content-base "+wpPublic+" --history-api-fallback --port "+port+" --mode=development";
(0,_exec.executeAsync)(c,command,{stdio:'inherit',silent:true}).
then(function(){
(0,_logger.logDebug)('runWebDevServer: running');
resolve();
}).
catch(function(e){
(0,_logger.logDebug)(e);
resolve();
});
});};

var deployWeb=function deployWeb(c,platform){
(0,_logger.logTask)("deployWeb:"+platform);
return(0,_webTools.selectWebToolAndDeploy)(c,platform);
};exports.deployWeb=deployWeb;

var exportWeb=function exportWeb(c,platform){
(0,_logger.logTask)("exportWeb:"+platform);
return(0,_webTools.selectWebToolAndExport)(c,platform);
};exports.exportWeb=exportWeb;
//# sourceMappingURL=index.js.map