var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.getBuildFilePath=exports.getFlavouredProp=exports.checkPortInUse=exports.getIP=exports.getBuildsFolder=exports.getMonorepoRoot=exports.isMonorepo=exports.getAppVersionCode=exports.getAppVersion=exports.getAppDescription=exports.getGetJsBundleFile=exports.getEntryFile=exports.getAppLicense=exports.getAppAuthor=exports.getAppTitle=exports.getAppId=exports.getConfigPropArray=exports._getConfigProp=exports.getConfigProp=exports.getAppTemplateFolder=exports.getAppFolder=exports.getTemplateProjectDir=exports.getTemplateDir=exports.getPlatformServerDir=exports.getPlatformProjectDir=exports.getPlatformOutputDir=exports.getPlatformBuildDir=exports.confirmActiveBundler=exports.getDevServerHost=exports.sanitizeColor=exports.addSystemInjects=exports.getCliArguments=exports.getTimestampPathsConfig=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _colorString=_interopRequireDefault(require("color-string"));
var _detectPort=_interopRequireDefault(require("detect-port"));
var _ip=_interopRequireDefault(require("ip"));
var _killPort=_interopRequireDefault(require("kill-port"));
var _lodash=_interopRequireDefault(require("lodash.get"));
var _path=_interopRequireDefault(require("path"));
var _prompt=require("../cli/prompt");
var _constants=require("./constants");
var _fileutils=require("./systemManager/fileutils");
var _logger=require("./systemManager/logger");



var _utils=require("./systemManager/utils");

var getTimestampPathsConfig=function getTimestampPathsConfig(c,platform){var _timestampBuildFiles;
var timestampBuildFiles;
var pPath=_path.default.join(
c.paths.project.builds.dir,
c.runtime.appId+"_"+platform);

if(platform==='web'){
timestampBuildFiles=getConfigProp(c,platform,'timestampBuildFiles',[]).map(function(v){return _path.default.join(pPath,v);});
}
if((_timestampBuildFiles=timestampBuildFiles)!=null&&_timestampBuildFiles.length){
return{paths:timestampBuildFiles,timestamp:c.runtime.timestamp};
}
return null;
};exports.getTimestampPathsConfig=getTimestampPathsConfig;

var getCliArguments=function getCliArguments(c){var _missingArg;
var _c$program=c.program,args=_c$program.args,rawArgs=_c$program.rawArgs;
var argsCopy=(0,_toConsumableArray2.default)(args);
var missingArg=rawArgs[rawArgs.indexOf(argsCopy[1])+1];
if(((_missingArg=missingArg)==null?void 0:_missingArg[0])==='-'){
if(rawArgs[rawArgs.indexOf(argsCopy[1])+2]){
missingArg=rawArgs[rawArgs.indexOf(argsCopy[1])+2];
}else{
missingArg=undefined;
}
}
if(rawArgs.length===3)missingArg=undefined;
argsCopy[2]=missingArg;
return argsCopy.filter(function(arg){return!!arg;});
};exports.getCliArguments=getCliArguments;

var addSystemInjects=function addSystemInjects(c,injects){
if(!c.systemPropsInjects)c.systemPropsInjects=[];
if(injects){
injects.forEach(function(item){
c.systemPropsInjects.push(item);
});
}
};exports.addSystemInjects=addSystemInjects;

var sanitizeColor=function sanitizeColor(val,key){
if(!val){
(0,_logger.logWarning)("You are missing "+
(0,_logger.chalk)().white(key)+" in your renative config. will use default #FFFFFF instead");

return{
rgb:[255,255,255,1],
rgbDecimal:[1,1,1,1],
hex:'#FFFFFF'};

}

var rgb=_colorString.default.get.rgb(val);
var hex=_colorString.default.to.hex(rgb);

return{
rgb:rgb,
rgbDecimal:rgb.map(function(v){return v>1?Math.round(v/255*10)/10:v;}),
hex:hex};

};exports.sanitizeColor=sanitizeColor;

var getDevServerHost=function getDevServerHost(c){
var devServerHostOrig=getConfigProp(c,c.platform,'devServerHost');
if(!devServerHostOrig){
devServerHostOrig=getConfigProp(c,c.platform,'webpack',{}).devServerHost;
if(devServerHostOrig){
(0,_logger.logWarning)('DEPRECATED: webpack.devServerHost. use devServerHost directly instead');
}
}
if(!devServerHostOrig){
if(!devServerHostOrig){
devServerHostOrig=getConfigProp(c,c.platform,'webpackConfig',{}).devServerHost;
if(devServerHostOrig){
(0,_logger.logWarning)('DEPRECATED: webpackConfig.devServerHost. use devServerHost directly instead');
}
}
}
var devServerHostFixed=(0,_utils.getValidLocalhost)(devServerHostOrig,c.runtime.localhost);

return devServerHostFixed;
};exports.getDevServerHost=getDevServerHost;

var confirmActiveBundler=function confirmActiveBundler(c){var choices,_await$inquirerPrompt,selectedOption;return _regenerator.default.async(function confirmActiveBundler$(_context){while(1){switch(_context.prev=_context.next){case 0:if(!
c.runtime.skipActiveServerCheck){_context.next=2;break;}return _context.abrupt("return",true);case 2:if(!

c.program.ci){_context.next=4;break;}return _context.abrupt("return",
(0,_killPort.default)(c.runtime.port));case 4:


choices=['Restart the server (recommended)','Use existing session'];_context.next=7;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
name:'selectedOption',
type:'list',
choices:choices,
warningMessage:"Another "+c.platform+" server at port "+
(0,_logger.chalk)().white(c.runtime.port)+" already running"}));case 7:_await$inquirerPrompt=_context.sent;selectedOption=_await$inquirerPrompt.selectedOption;if(!(



choices[0]===selectedOption)){_context.next=14;break;}_context.next=12;return _regenerator.default.awrap(
(0,_killPort.default)(c.runtime.port));case 12:_context.next=15;break;case 14:return _context.abrupt("return",

false);case 15:return _context.abrupt("return",

true);case 16:case"end":return _context.stop();}}},null,null,null,Promise);};exports.confirmActiveBundler=confirmActiveBundler;


var getPlatformBuildDir=function getPlatformBuildDir(c,isRelativePath){
if(!c.runtime.engine){
(0,_logger.logError)('getPlatformBuildDir not available without specific engine');
return null;
}
return getAppFolder(c,isRelativePath);
};exports.getPlatformBuildDir=getPlatformBuildDir;

var getPlatformOutputDir=function getPlatformOutputDir(c){
if(!c.runtime.engine){
(0,_logger.logError)('getPlatformOutputDir not available without specific engine');
return null;
}
return _path.default.join(getAppFolder(c),c.runtime.engine.outputDirName||'');
};exports.getPlatformOutputDir=getPlatformOutputDir;

var getPlatformProjectDir=function getPlatformProjectDir(c){
if(!c.runtime.engine){
(0,_logger.logError)('getPlatformProjectDir not available without specific engine');
return null;
}
return _path.default.join(getAppFolder(c),c.runtime.engine.projectDirName||'');
};exports.getPlatformProjectDir=getPlatformProjectDir;

var getPlatformServerDir=function getPlatformServerDir(c){
if(!c.runtime.engine){
(0,_logger.logError)('getPlatformProjectDir not available without specific engine');
return null;
}
return _path.default.join(getAppFolder(c),c.runtime.engine.serverDirName||'');
};exports.getPlatformServerDir=getPlatformServerDir;

var getTemplateDir=function getTemplateDir(c){return _path.default.join(
c.paths.project.platformTemplatesDirs[c.platform],""+c.platform);};exports.getTemplateDir=getTemplateDir;


var getTemplateProjectDir=function getTemplateProjectDir(c){
if(!c.runtime.engine){
(0,_logger.logError)('getTemplateProjectDir not available without specific engine');
return null;
}
return _path.default.join(getTemplateDir(c),c.runtime.engine.projectDirName);
};exports.getTemplateProjectDir=getTemplateProjectDir;


var getAppFolder=function getAppFolder(c,isRelativePath){
if(isRelativePath){
return"platformBuilds/"+c.runtime.appId+"_"+c.platform;
}
return _path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+c.platform);
};exports.getAppFolder=getAppFolder;


var getAppTemplateFolder=function getAppTemplateFolder(c,platform){return _path.default.join(
c.paths.project.platformTemplatesDirs[platform],""+platform);};exports.getAppTemplateFolder=getAppTemplateFolder;


var _getValueOrMergedObject=function _getValueOrMergedObject(
resultCli,
resultScheme,
resultPlatforms,
resultCommon)
{
if(resultCli!==undefined){
return resultCli;
}
if(resultScheme!==undefined){
if(Array.isArray(resultScheme)||typeof resultScheme!=='object'){return resultScheme;}
var val=(0,_extends2.default)(
resultCommon||{},
resultPlatforms||{},
resultScheme);

return val;
}
if(resultPlatforms!==undefined){
if(
Array.isArray(resultPlatforms)||
typeof resultPlatforms!=='object')
{return resultPlatforms;}
return(0,_extends2.default)(resultCommon||{},resultPlatforms);
}
if(resultPlatforms===null)return null;
return resultCommon;
};

var getConfigProp=function getConfigProp(c,platform,key,defaultVal){
if(!c.buildConfig){
(0,_logger.logError)('getConfigProp: c.buildConfig is undefined!');
return null;
}
return _getConfigProp(c,platform,key,defaultVal,c.buildConfig);
};exports.getConfigProp=getConfigProp;

var _getConfigProp=function _getConfigProp(c,platform,key,defaultVal,sourceObj){var _sourceObj$platforms;
if(!sourceObj)return null;

if(!key||!key.split){
(0,_logger.logError)('getConfigProp: invalid key!');
return null;
}
var p=(_sourceObj$platforms=sourceObj.platforms)==null?void 0:_sourceObj$platforms[platform];
var ps=c.runtime.scheme;
var keyArr=key.split('.');
var baseKey=keyArr.shift();
var subKey=keyArr.join('.');

var resultPlatforms;
var scheme;
if(p){
scheme=p.buildSchemes?p.buildSchemes[ps]:undefined;
resultPlatforms=getFlavouredProp(
c,
sourceObj.platforms[platform],
baseKey);

}

scheme=scheme||{};
var resultCli=_constants.CLI_PROPS.includes(baseKey)?c.program[baseKey]:undefined;
var resultScheme=scheme[baseKey];
var resultCommon=getFlavouredProp(c,sourceObj.common,baseKey);

var result=_getValueOrMergedObject(
resultCli,
resultScheme,
resultPlatforms,
resultCommon);

if(result===undefined||result===null){
result=getFlavouredProp(c,sourceObj,baseKey);
}

if(result===undefined||result===null)result=defaultVal;
if(typeof result==='object'&&subKey.length){
return(0,_lodash.default)(result,subKey);
}
return result;
};exports._getConfigProp=_getConfigProp;


var getConfigPropArray=function getConfigPropArray(c,platform,key){
var result=[];
var configArr=[
c.files.defaultWorkspace.config,
c.files.rnv.projectTemplates.config,


c.files.workspace.config,
c.files.workspace.configPrivate,
c.files.workspace.configLocal,
c.files.workspace.project.config,
c.files.workspace.project.configPrivate,
c.files.workspace.project.configLocal].concat((0,_toConsumableArray2.default)(
c.files.workspace.appConfig.configs),(0,_toConsumableArray2.default)(
c.files.workspace.appConfig.configsPrivate),(0,_toConsumableArray2.default)(
c.files.workspace.appConfig.configsLocal),[
c.files.project.config,
c.files.project.configPrivate,
c.files.project.configLocal],(0,_toConsumableArray2.default)(
c.files.appConfig.configs),(0,_toConsumableArray2.default)(
c.files.appConfig.configsPrivate),(0,_toConsumableArray2.default)(
c.files.appConfig.configsLocal));

configArr.forEach(function(config){
var val=_getConfigProp(c,platform,key,null,config);
if(val){
result.push(val);
}
});


return result;
};exports.getConfigPropArray=getConfigPropArray;


var getAppId=function getAppId(c,platform){
var id=getConfigProp(c,platform,'id');
var idSuffix=getConfigProp(c,platform,'idSuffix');
return idSuffix?""+id+idSuffix:id;
};exports.getAppId=getAppId;

var getAppTitle=function getAppTitle(c,platform){return getConfigProp(c,platform,'title');};exports.getAppTitle=getAppTitle;

var getAppAuthor=function getAppAuthor(c,platform){var _c$files$project$pack;return getConfigProp(c,platform,'author')||((_c$files$project$pack=c.files.project.package)==null?void 0:_c$files$project$pack.author);};exports.getAppAuthor=getAppAuthor;

var getAppLicense=function getAppLicense(c,platform){var _c$files$project$pack2;return getConfigProp(c,platform,'license')||((_c$files$project$pack2=c.files.project.package)==null?void 0:_c$files$project$pack2.license);};exports.getAppLicense=getAppLicense;

var getEntryFile=function getEntryFile(c,platform){var _c$buildConfig$platfo,_c$buildConfig$platfo2;return(_c$buildConfig$platfo=c.buildConfig.platforms)==null?void 0:(_c$buildConfig$platfo2=_c$buildConfig$platfo[platform])==null?void 0:_c$buildConfig$platfo2.entryFile;};exports.getEntryFile=getEntryFile;

var getGetJsBundleFile=function getGetJsBundleFile(c,platform){return getConfigProp(c,platform,'getJsBundleFile');};exports.getGetJsBundleFile=getGetJsBundleFile;

var getAppDescription=function getAppDescription(c,platform){var _c$files$project$pack3;return getConfigProp(c,platform,'description')||((_c$files$project$pack3=
c.files.project.package)==null?void 0:_c$files$project$pack3.description);};exports.getAppDescription=getAppDescription;

var getAppVersion=function getAppVersion(c,platform){var _c$files$project$pack4;
var version=getConfigProp(c,platform,'version')||((_c$files$project$pack4=c.files.project.package)==null?void 0:_c$files$project$pack4.version);
if(!version){
(0,_logger.logWarning)('You are missing version prop in your config. will default to 0');
return'0';
}
var versionFormat=getConfigProp(c,platform,'versionFormat');
if(!versionFormat)return version;
var versionCodeArr=versionFormat.split('.');
var dotLength=versionCodeArr.length;
var isNumArr=versionCodeArr.map(function(v){return!Number.isNaN(Number(v));});

var verArr=[];
var i=0;
version.split('.').map(function(v){return v.split('-').map(function(v2){return v2.split('+').forEach(function(v3){
var isNum=!Number.isNaN(Number(v3));
if(isNumArr[i]&&isNum){
verArr.push(v3);
}else if(!isNumArr[i]){
verArr.push(v3);
}

i++;
});});});
if(verArr.length>dotLength){
verArr.length=dotLength;
}

var output=verArr.join('.');

return output;
};exports.getAppVersion=getAppVersion;

var getAppVersionCode=function getAppVersionCode(c,platform){var _c$files$project$pack5;
var versionCode=getConfigProp(c,platform,'versionCode');
if(versionCode)return versionCode;
var version=getConfigProp(c,platform,'version')||((_c$files$project$pack5=c.files.project.package)==null?void 0:_c$files$project$pack5.version);
if(!version||typeof version!=='string'){
(0,_logger.logWarning)('You are missing version prop in your config. will default to 0');
return'0';
}
var versionCodeFormat=getConfigProp(c,platform,'versionCodeFormat','00.00.00');
var vFormatArr=versionCodeFormat.split('.').map(function(v){return v.length;});
var versionCodeMaxCount=vFormatArr.length;
var verArr=[];


version.split('.').map(function(v){return v.split('-').map(function(v2){return v2.split('+').forEach(function(v3){
var asNumber=Number(v3);
if(!Number.isNaN(asNumber)){
var val=v3;
var maxDigits=vFormatArr[verArr.length]||2;

if(v3.length>maxDigits){
val=v3.substr(0,maxDigits);
}else if(v3.length<maxDigits){
var toAdd=maxDigits-v3.length;
val=v3;
while(toAdd>0){
val="0"+val;
toAdd--;
}
}
verArr.push(val);
}
});});});
var verCountDiff=verArr.length-versionCodeMaxCount;
if(verCountDiff<0){
while(verCountDiff<0){
var extraVersionLen=vFormatArr[versionCodeMaxCount+verCountDiff];
var num='';
while(extraVersionLen){
num+='0';
extraVersionLen--;
}
verArr.push(num);
verCountDiff++;
}
}

var output=Number(verArr.join('')).toString();

return output;
};exports.getAppVersionCode=getAppVersionCode;

var isMonorepo=function isMonorepo(){
try{
(0,_fileutils.fsExistsSync)(_path.default.resolve(__dirname,'../../../../lerna.json'));
return true;
}catch(_err){
return false;
}
};exports.isMonorepo=isMonorepo;

var getMonorepoRoot=function getMonorepoRoot(){
if(isMonorepo()){
return _path.default.resolve(__dirname,'../../../..');
}
};exports.getMonorepoRoot=getMonorepoRoot;

var getBuildsFolder=function getBuildsFolder(c,platform,customPath){
var pp=customPath||c.paths.appConfig.dir;



if(!pp){
(0,_logger.logWarning)("getBuildsFolder: Path "+(0,_logger.chalk)().white('c.paths.appConfig.dir')+" not defined! can't return path. You might not be in renative project");
return null;
}
var p=_path.default.join(pp,"builds/"+platform+"@"+c.runtime.scheme);
if((0,_fileutils.fsExistsSync)(p))return p;
return _path.default.join(pp,"builds/"+platform);
};exports.getBuildsFolder=getBuildsFolder;

var getIP=function getIP(){return _ip.default.address();};exports.getIP=getIP;

var checkPortInUse=function checkPortInUse(c,platform,port){return new Promise(function(resolve,reject){
if(port===undefined||port===null){
resolve(false);
return;
}
(0,_detectPort.default)(port,function(err,availablePort){
if(err){
reject(err);
return;
}
var result=parseInt(port,10)!==parseInt(availablePort,10);
resolve(result);
});
});};exports.checkPortInUse=checkPortInUse;

var getFlavouredProp=function getFlavouredProp(c,obj,key){
if(!key||!obj)return null;
var val1=obj[key+"@"+c.runtime.scheme];
if(val1)return val1;
return obj[key];
};exports.getFlavouredProp=getFlavouredProp;

var getBuildFilePath=function getBuildFilePath(c,platform,filePath){

var sp=_path.default.join(getAppTemplateFolder(c,platform),filePath);

var sp2=_path.default.join(
getBuildsFolder(c,platform,c.paths.project.appConfigBase.dir),
filePath);

if((0,_fileutils.fsExistsSync)(sp2))sp=sp2;

var sp3=_path.default.join(getBuildsFolder(c,platform),filePath);
if((0,_fileutils.fsExistsSync)(sp3))sp=sp3;
return sp;
};exports.getBuildFilePath=getBuildFilePath;var _default=

{
getBuildFilePath:getBuildFilePath,
getBuildsFolder:getBuildsFolder,
getAppFolder:getAppFolder,
getAppTemplateFolder:getAppTemplateFolder,
getAppId:getAppId,
getAppTitle:getAppTitle,
getAppVersion:getAppVersion,
getAppVersionCode:getAppVersionCode,
writeCleanFile:_fileutils.writeCleanFile,
getEntryFile:getEntryFile,
getGetJsBundleFile:getGetJsBundleFile,
getAppDescription:getAppDescription,
getAppAuthor:getAppAuthor,
getAppLicense:getAppLicense,
getConfigProp:getConfigProp,
getIP:getIP,
checkPortInUse:checkPortInUse,
logTask:function logTask(val){
(0,_logger.logError)(
'DEPRECATED: Common.logTask() has been removed. use Logger.logTask() instead');

(0,_logger.logTask)(val);
},
logWarning:function logWarning(val){
(0,_logger.logError)(
'DEPRECATED: Common.logWarning() has been removed. use Logger.logWarning() instead');

(0,_logger.logWarning)(val);
},
logError:function logError(val){
(0,_logger.logError)(
'DEPRECATED: Common.logError() has been removed. use Logger.logError() instead');

(0,_logger.logError)(val);
},
logSuccess:function logSuccess(val){
(0,_logger.logError)(
'DEPRECATED: Common.logError() has been removed. use Logger.logError() instead');

(0,_logger.logSuccess)(val);
},
logDebug:function logDebug(val){
(0,_logger.logError)(
'DEPRECATED: Common.logDebug() has been removed. use Logger.logDebug() instead');

(0,_logger.logDebug)(val);
}};exports.default=_default;
//# sourceMappingURL=common.js.map