var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportWeb=exports.deployWeb=exports.runWebDevServer=exports.runWebNext=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 _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 _config=_interopRequireDefault(require("../../config"));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

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,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 obj=_objectSpread({
modulePaths:modulePaths,
moduleAliases:moduleAliases,
analyzer:analyzer,
entryFile:entryFile,
title:title,
extensions:(0,_common.getSourceExts)(c)},
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;
(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,_common.resolveNodeModulePath)(c,'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 configureNextIfRequired=function configureNextIfRequired(c){var _c$paths$project,srcDir,platformTemplatesDirs,pagesDir,publicDir,baseFontsDir,stylesDir,platformTemplateDir,cssOutput,fontFiles;return _regenerator.default.async(function configureNextIfRequired$(_context){while(1){switch(_context.prev=_context.next){case 0:_c$paths$project=
c.paths.project,srcDir=_c$paths$project.srcDir,platformTemplatesDirs=_c$paths$project.platformTemplatesDirs;
pagesDir=_path.default.join(srcDir,'pages');
publicDir=_path.default.join(srcDir,'public');
baseFontsDir=c.paths.appConfig.fontDirs[0];
stylesDir=_path.default.join(srcDir,'styles');

platformTemplateDir=_path.default.join(platformTemplatesDirs[c.platform],c.platform);
(0,_fileutils.copyFolderContentsRecursiveSync)(platformTemplateDir,srcDir);


!_fs.default.existsSync(_path.default.join(pagesDir,'index.js'))&&_fs.default.symlinkSync(_path.default.join(srcDir,'app/index.web.js'),_path.default.join(pagesDir,'index.js'));
!_fs.default.existsSync(_path.default.join(pagesDir,'my-page.js'))&&_fs.default.symlinkSync(_path.default.join(srcDir,'screenMyPage.js'),_path.default.join(pagesDir,'my-page.js'));


!_fs.default.existsSync(publicDir)&&_fs.default.mkdirSync(publicDir);
!_fs.default.existsSync(_path.default.join(publicDir,'fonts'))&&_fs.default.symlinkSync(baseFontsDir,_path.default.join(publicDir,'fonts'));


if(!_fs.default.existsSync(stylesDir)){
_fs.default.mkdirSync(stylesDir);
cssOutput='';

fontFiles=_fs.default.readdirSync(baseFontsDir);
fontFiles.forEach(function(file){
cssOutput+="\n                @font-face {\n                    font-family: '"+

file.split('.')[0]+"';\n                    src: url('/fonts/"+
file+"');\n                }\n\n            ";



});

_fs.default.writeFileSync(_path.default.join(stylesDir,'fonts.css'),cssOutput);
}case 12:case"end":return _context.stop();}}},null,null,null,Promise);};


var runWebNext=function runWebNext(c,platform,port){var devServerHost,isPortActive;return _regenerator.default.async(function runWebNext$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)("runWebNext:"+platform+":"+port);_context2.next=3;return _regenerator.default.awrap(
configureNextIfRequired(c));case 3:

devServerHost=c.runtime.localhost;_context2.next=6;return _regenerator.default.awrap(

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

isPortActive){_context2.next=15;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...");_context2.next=11;return _regenerator.default.awrap(


_runWebBrowser(c,platform,devServerHost,port,false));case 11:_context2.next=13;return _regenerator.default.awrap(
runWebDevServer(c,platform,port));case 13:_context2.next=19;break;case 15:_context2.next=17;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 17:_context2.next=19;return _regenerator.default.awrap(
_runWebBrowser(c,platform,devServerHost,port,true));case 19:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.runWebNext=runWebNext;



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,port).
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){
(0,_logger.logTask)("runWebDevServer:"+platform);var _c$paths$project2=

c.paths.project,srcDir=_c$paths$project2.srcDir,dir=_c$paths$project2.builds.dir;


return(0,_exec.executeAsync)(c,"npx next "+srcDir+" --port "+port);
























};exports.runWebDevServer=runWebDevServer;

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=webNext.js.map