var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportWebNext=exports.deployWebNext=exports.runWebDevServer=exports.buildWebNext=exports.getTranspileModules=exports.runWebNext=exports.configureNextIfRequired=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 _exec=require("../core/systemManager/exec");
var _common=require("../core/common");






var _index=require("./index");

var _fileutils=require("../core/systemManager/fileutils");








var _logger=require("../core/systemManager/logger");

var _constants=require("../core/constants");
var _engineManager=require("../core/engineManager");
var _pluginManager=require("../core/pluginManager");
var _utils=require("../core/systemManager/utils");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 configureNextIfRequired=function configureNextIfRequired(c){var _c$paths$project,platformTemplatesDirs,dir,supportFilesDir,configFile;return _regenerator.default.async(function configureNextIfRequired$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('configureNextIfRequired');
c.runtime.platformBuildsProjectPath=""+(0,_common.getPlatformBuildDir)(c);_c$paths$project=
c.paths.project,platformTemplatesDirs=_c$paths$project.platformTemplatesDirs,dir=_c$paths$project.dir;


supportFilesDir=_path.default.join(platformTemplatesDirs[c.platform],'../supportFiles');
configFile=_path.default.join(dir,_constants.NEXT_CONFIG_NAME);


















































if(!(0,_fileutils.fsExistsSync)(configFile)){
(0,_fileutils.writeCleanFile)(_path.default.join(supportFilesDir,_constants.NEXT_CONFIG_NAME),configFile,null,null,c);
}case 6:case"end":return _context.stop();}}},null,null,null,Promise);};exports.configureNextIfRequired=configureNextIfRequired;


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

devServerHost=(0,_utils.getValidLocalhost)((0,_common.getConfigProp)(c,c.platform,'devServerHost',c.runtime.localhost),c.runtime.localhost);_context2.next=6;return _regenerator.default.awrap(

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

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


_runWebBrowser(c,platform,devServerHost,port,false));case 12:

if(!bundleAssets){
(0,_logger.logSummary)('BUNDLER STARTED');
}_context2.next=15;return _regenerator.default.awrap(
runWebDevServer(c));case 15:_context2.next=30;break;case 17:_context2.next=19;return _regenerator.default.awrap(

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

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

_runWebBrowser(c,platform,devServerHost,port,true));case 30: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',"ip:"+devServerHost+" port:"+port+" openBrowser:"+!!c.runtime.shouldOpenBrowser);

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

var _checkPagesDir=function _checkPagesDir(c){var pagesDir,distDir,pagesDirPath,fallbackPagesDir,fallbackPagesDirPath;return _regenerator.default.async(function _checkPagesDir$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
pagesDir=(0,_common.getConfigProp)(c,c.platform,'pagesDir');
distDir="platformBuilds/"+c.runtime.appId+"_"+c.platform+"/.next";if(!
pagesDir){_context3.next=6;break;}
pagesDirPath=_path.default.join(c.paths.project.dir,pagesDir);
if(!(0,_fileutils.fsExistsSync)(pagesDirPath)){
(0,_logger.logWarning)("You configured custom "+c.platform+"pagesDir: "+
(0,_logger.chalk)().white(pagesDir)+" in your renative.json but it is missing at "+
(0,_logger.chalk)().red(pagesDirPath));
}return _context3.abrupt("return",
{NEXT_PAGES_DIR:pagesDir,NEXT_DIST_DIR:distDir});case 6:

fallbackPagesDir='src/app';
(0,_logger.logWarning)("You're missing "+c.platform+".pagesDir config. Defaulting to '"+fallbackPagesDir+"'");

fallbackPagesDirPath=_path.default.join(c.paths.project.dir,fallbackPagesDir);
if(!(0,_fileutils.fsExistsSync)(fallbackPagesDirPath)){
(0,_logger.logWarning)("Folder "+
(0,_logger.chalk)().white(fallbackPagesDir)+" is missing. make sure your entry code is located there in order for next to work correctly!\nAlternatively you can configure custom entry folder via "+

c.platform+".pagesDir in renative.json");
}return _context3.abrupt("return",
{NEXT_PAGES_DIR:'src/app',NEXT_DIST_DIR:distDir});case 11:case"end":return _context3.stop();}}},null,null,null,Promise);};


var getTranspileModules=function getTranspileModules(c){
var transModules=[];
(0,_pluginManager.parsePlugins)(c,c.platform,function(plugin,pluginPlat,key){
var webpackConfig=plugin.webpack||plugin.webpackConfig;
if(webpackConfig){var _webpackConfig$nextTr;
transModules.push(key);
if((_webpackConfig$nextTr=webpackConfig.nextTranspileModules)==null?void 0:_webpackConfig$nextTr.length){
transModules=transModules.concat(webpackConfig.nextTranspileModules);
}
}
},true);
return transModules;
};exports.getTranspileModules=getTranspileModules;

var buildWebNext=function buildWebNext(c){var env,platformBuildDir,envExt;return _regenerator.default.async(function buildWebNext$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('buildWebNext');
env=(0,_common.getConfigProp)(c,c.platform,'environment');
platformBuildDir=(0,_common.getPlatformBuildDir)(c);_context4.next=5;return _regenerator.default.awrap(

_checkPagesDir(c));case 5:envExt=_context4.sent;_context4.next=8;return _regenerator.default.awrap(

(0,_exec.executeAsync)(c,'npx next build',_objectSpread(_objectSpread({},
process.env),{},{
env:_objectSpread(_objectSpread({
NODE_ENV:env||'development'},
envExt),
(0,_engineManager.generateEnvVars)(c,(0,_index.getModuleConfigs)(c),getTranspileModules(c)))})));case 8:


(0,_logger.logSuccess)("Your build is located in "+
(0,_logger.chalk)().cyan(_path.default.join(platformBuildDir,'.next'))+" .");return _context4.abrupt("return",

true);case 10:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.buildWebNext=buildWebNext;


var runWebDevServer=function runWebDevServer(c){var env,envExt,devServerHost,url,bundleAssets;return _regenerator.default.async(function runWebDevServer$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('runWebDevServer');
env=(0,_common.getConfigProp)(c,c.platform,'environment');_context5.next=4;return _regenerator.default.awrap(
_checkPagesDir(c));case 4:envExt=_context5.sent;

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

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

url+"\n\n");




bundleAssets=(0,_common.getConfigProp)(c,c.platform,'bundleAssets',false);return _context5.abrupt("return",
(0,_exec.executeAsync)(c,"npx next "+(bundleAssets?'start':'dev')+" --port "+c.runtime.port,
{
env:_objectSpread(_objectSpread({
NODE_ENV:env||'development'},
envExt),
(0,_engineManager.generateEnvVars)(c,(0,_index.getModuleConfigs)(c),getTranspileModules(c))),

interactive:true}));case 10:case"end":return _context5.stop();}}},null,null,null,Promise);};exports.runWebDevServer=runWebDevServer;



var deployWebNext=function deployWebNext(){
(0,_logger.logTask)('deployWebNext');





return true;
};exports.deployWebNext=deployWebNext;

var exportWebNext=function exportWebNext(c){var exportDir,env,envExt;return _regenerator.default.async(function exportWebNext$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
(0,_logger.logTask)('exportWebNext');


(0,_logger.logTask)('_exportNext');
exportDir=_path.default.join((0,_common.getPlatformBuildDir)(c),'output');
env=(0,_common.getConfigProp)(c,c.platform,'environment');_context6.next=6;return _regenerator.default.awrap(
_checkPagesDir(c));case 6:envExt=_context6.sent;_context6.next=9;return _regenerator.default.awrap(

(0,_exec.executeAsync)(c,"npx next export --outdir "+exportDir,_objectSpread(_objectSpread({},
process.env),{},{
env:_objectSpread(_objectSpread({
NODE_ENV:env||'development'},
envExt),
(0,_engineManager.generateEnvVars)(c,(0,_index.getModuleConfigs)(c),getTranspileModules(c)))})));case 9:


(0,_logger.logSuccess)("Your export is located in "+
(0,_logger.chalk)().cyan(exportDir)+" .");return _context6.abrupt("return",




true);case 11:case"end":return _context6.stop();}}},null,null,null,Promise);};exports.exportWebNext=exportWebNext;
//# sourceMappingURL=webNext.js.map