var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportWebNext=exports.deployWeb=exports.runWebDevServer=exports.buildWebNext=exports.runWebNext=exports.buildWeb=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));
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 _logger=require("../../systemTools/logger");
var _constants=require("../../constants");
var _webTools=require("../../deployTools/webTools");
var _resolve=require("../../../dist/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 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 configureNextIfRequired=function configureNextIfRequired(c){var _c$paths$appConfig$fo;var _c$paths$project,platformTemplatesDirs,dir,publicDir,baseFontsDir,stylesDir,pagesDir,_appFile,platformTemplateDir,configFile,fontsSymLinkPath,xxx,yyy,cssOutput,fontFiles;return _regenerator.default.async(function configureNextIfRequired$(_context){while(1){switch(_context.prev=_context.next){case 0:_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.fontDirs)==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');
platformTemplateDir=_path.default.join(platformTemplatesDirs[c.platform],"_"+c.platform);
configFile=_path.default.join(dir,_constants.NEXT_CONFIG_NAME);


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

if(_fs.default.existsSync(baseFontsDir)){
if(!_fs.default.existsSync(fontsSymLinkPath)){
xxx=_fs.default.readlinkSync(fontsSymLinkPath);
yyy=_fs.default.lstatSync(fontsSymLinkPath);
console.log('AGAGGA',fontsSymLinkPath,xxx,yyy);
_fs.default.symlinkSync(baseFontsDir,fontsSymLinkPath);
}


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



if(!_fs.default.existsSync(_appFile)){
if(!_fs.default.existsSync(pagesDir)){
_fs.default.mkdirSync(pagesDir);
}
(0,_common.writeCleanFile)(_path.default.join(platformTemplateDir,'_app.js'),_appFile,[{pattern:'{{FONTS_CSS}}',override:_path.default.relative(pagesDir,_path.default.resolve('styles/fonts.css'))}]);
}


if(!_fs.default.existsSync(configFile)){
(0,_common.writeCleanFile)(_path.default.join(platformTemplateDir,_constants.NEXT_CONFIG_NAME),configFile);
}case 13: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,'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){
(0,_logger.logTask)('buildWebNext');
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'");

return(0,_exec.executeAsync)(c,"npx next build ./platformBuilds/"+c.runtime.appId+"_"+c.platform+" --pagesDir "+(pagesDir||'src/app'),_objectSpread(_objectSpread({},process.env),{},{env:{NODE_ENV:env||'development'},interactive:true}));
};exports.buildWebNext=buildWebNext;

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

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

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

var exportWebNext=function exportWebNext(c){
(0,_logger.logTask)('exportWebNext');
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'");

return(0,_exec.executeAsync)(c,"npx next export ./platformBuilds/"+c.runtime.appId+"_"+c.platform+" --pagesDir "+(pagesDir||'src/app'),_objectSpread(_objectSpread({},process.env),{},{env:{NODE_ENV:env||'development'},interactive:true}));
};exports.exportWebNext=exportWebNext;
//# sourceMappingURL=webNext.js.map