var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportWebNext=exports.deployWebNext=exports.runWebDevServer=exports.buildWebNext=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 _webTools=require("../core/deployManager/webTools");

var _utils=require("../core/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$appConfig$fo;var _c$paths$project,platformTemplatesDirs,dir,publicDir,baseFontsDir,stylesDir,pagesDir,_appFile,supportFilesDir,configFile,fontsSymLinkPath,cssOutput,fontFiles;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;
publicDir=_path.default.join(dir,'public');
baseFontsDir=(_c$paths$appConfig$fo=c.paths.appConfig.fontsDirs)==null?void 0:_c$paths$appConfig$fo[0];
stylesDir=_path.default.join(dir,'styles');
pagesDir=_path.default.resolve((0,_common.getConfigProp)(c,c.platform,'pagesDir')||'src/app');
_appFile=_path.default.join(pagesDir,'_app.js');
supportFilesDir=_path.default.join(platformTemplatesDirs[c.platform],'../supportFiles');
configFile=_path.default.join(dir,_constants.NEXT_CONFIG_NAME);


!(0,_fileutils.fsExistsSync)(publicDir)&&(0,_fileutils.fsMkdirSync)(publicDir);
fontsSymLinkPath=_path.default.join(publicDir,'fonts');

if((0,_fileutils.fsExistsSync)(baseFontsDir)){
if(!(0,_fileutils.fsExistsSync)(fontsSymLinkPath)){
try{
(0,_fileutils.fsUnlinkSync)(fontsSymLinkPath);
}catch(e){
(0,_logger.logDebug)(e);
}
(0,_fileutils.fsSymlinkSync)(baseFontsDir,fontsSymLinkPath);
}


if(!(0,_fileutils.fsExistsSync)(stylesDir)){
(0,_fileutils.fsMkdirSync)(stylesDir);
cssOutput='';

fontFiles=(0,_fileutils.fsReaddirSync)(baseFontsDir);
fontFiles.forEach(function(file){
cssOutput+="\n                  @font-face {\n                      font-family: '"+

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



});

(0,_fileutils.fsWriteFileSync)(_path.default.join(stylesDir,'fonts.css'),cssOutput);
}
}


if(!(0,_fileutils.fsExistsSync)(_appFile)){
if(!(0,_fileutils.fsExistsSync)(pagesDir)){
(0,_fileutils.fsMkdirSync)(pagesDir);
}
(0,_fileutils.writeCleanFile)(
_path.default.join(supportFilesDir,'_app.js'),
_appFile,
[{pattern:'{{FONTS_CSS}}',override:_path.default.relative(pagesDir,_path.default.resolve('styles/fonts.css')).replace(/\\/g,'/')}],
null,
c);

}


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


var runWebNext=function runWebNext(c){var port,platform,devServerHost,isPortActive,bundleAssets;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)("Looks like 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=22;break;case 17:_context2.next=19;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 19:_context2.next=21;return _regenerator.default.awrap(
_runWebBrowser(c,platform,devServerHost,port,true));case 21:
if(!bundleAssets){
(0,_logger.logSummary)('BUNDLER STARTED');
}case 22: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,'next').
then(function(){
(0,_betterOpn.default)("http://"+devServerHost+":"+port+"/");
}).
catch(function(e){
(0,_logger.logWarning)(e);
});
if(alreadyStarted)return wait;
return resolve();
});};

var buildWebNext=function buildWebNext(c){var env,appFolder,pagesDir;return _regenerator.default.async(function buildWebNext$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('buildWebNext');
env=(0,_common.getConfigProp)(c,c.platform,'environment');
appFolder=(0,_common.getPlatformBuildDir)(c);
pagesDir=(0,_common.getConfigProp)(c,c.platform,'pagesDir');
if(!pagesDir)(0,_logger.logWarning)("You're missing "+c.platform+".pagesDir config. Defaulting to 'src/app'");_context3.next=7;return _regenerator.default.awrap(

(0,_exec.executeAsync)(c,"npx next build "+appFolder+" --pagesDir "+(pagesDir||'src/app'),_objectSpread(_objectSpread({},process.env),{},{env:{NODE_ENV:env||'development'}})));case 7:
(0,_logger.logSuccess)("Your build is located in "+
(0,_logger.chalk)().cyan(_path.default.join(appFolder,'platformBuilds'))+" .");return _context3.abrupt("return",

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


var runWebDevServer=function runWebDevServer(c){
(0,_logger.logTask)('runWebDevServer');
var env=(0,_common.getConfigProp)(c,c.platform,'environment');
var pagesDir=(0,_common.getConfigProp)(c,c.platform,'pagesDir');
if(!pagesDir)(0,_logger.logWarning)("You're missing "+c.platform+".pagesDir config. Defaulting to 'src/app'");
var devServerHost=(0,_utils.getValidLocalhost)((0,_common.getConfigProp)(c,c.platform,'devServerHost',c.runtime.localhost),c.runtime.localhost);

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

url+"\n\n");



return(0,_exec.executeAsync)(c,"npx next dev --pagesDir "+(pagesDir||'src/app')+" --port "+c.runtime.port,{env:{NODE_ENV:env||'development'},interactive:true});
};exports.runWebDevServer=runWebDevServer;

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

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

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

(0,_logger.logTask)('_exportNext');
appFolder=(0,_common.getPlatformBuildDir)(c);
env=(0,_common.getConfigProp)(c,c.platform,'environment');
pagesDir=(0,_common.getConfigProp)(c,c.platform,'pagesDir');
if(!pagesDir)(0,_logger.logWarning)("You're missing "+c.platform+".pagesDir config. Defaulting to 'src/app'");_context4.next=9;return _regenerator.default.awrap(
(0,_exec.executeAsync)(c,"npx next export "+appFolder+" --pagesDir "+(pagesDir||'src/app'),_objectSpread(_objectSpread({},process.env),{},{env:{NODE_ENV:env||'development'}})));case 9:
(0,_logger.logSuccess)("Your export is located in "+
(0,_logger.chalk)().cyan(_path.default.join(appFolder,'out'))+" .");_context4.next=12;return _regenerator.default.awrap(


(0,_webTools.selectWebToolAndExport)(c,platform));case 12:return _context4.abrupt("return",
true);case 13:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.exportWebNext=exportWebNext;
//# sourceMappingURL=webNext.js.map