var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.configureRnvGlobal=exports.listAppConfigsFoldersSync=exports.updateConfig=exports.generateLocalConfig=exports.generateRuntimeConfig=exports.generateBuildConfig=exports.setAppConfig=exports.loadFile=exports.versionCheck=exports.fixRenativeConfigsSync=exports.checkIsRenativeProject=exports.parseRenativeConfigs=exports.createRnvConfig=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _deepmerge=_interopRequireDefault(require("deepmerge"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _constants=require("../constants");

























var _fileutils=require("../systemTools/fileutils");




var _logger=require("../systemTools/logger");




var _prompt=require("../systemTools/prompt");
var _projectParser=require("../projectTools/projectParser");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(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}




var base=_path.default.resolve('.');
var homedir=require('os').homedir();

var IGNORE_FOLDERS=['.git'];

var createRnvConfig=function createRnvConfig(program,process,cmd,subCmd){
var c={
cli:{},
runtime:{},


paths:{
buildHooks:{
dist:{}},

home:{},
rnv:{
pluginTemplates:{},
platformTemplates:{},
platformTemplate:{},
plugins:{},
projectTemplate:{}},

global:{},


project:{
projectConfig:{},
builds:{},
assets:{},
platformTemplates:{}},

template:{},
appConfig:{},
private:{
project:{
projectConfig:{},
builds:{},
assets:{},
platformTemplates:{}},

appConfig:{}}},


files:{
rnv:{
pluginTemplates:{},
platformTemplates:{},
plugins:{},
projectTemplate:{}},

project:{
projectConfig:{},
builds:{},
assets:{},
platformTemplates:{}},

appConfig:{},
private:{
project:{
projectConfig:{},
builds:{},
assets:{},
platformTemplates:{}},

appConfig:{}}}};




c.program=program;
c.process=process;
c.command=cmd;
c.subCommand=subCmd;
c.platformDefaults=_constants.PLATFORMS;

c.paths.rnv.dir=_path.default.join(__dirname,'../..');
c.paths.rnv.nodeModulesDir=_path.default.join(c.paths.rnv.dir,'node_modules');
c.paths.rnv.platformTemplates.dir=_path.default.join(c.paths.rnv.dir,'platformTemplates');
c.paths.rnv.pluginTemplates.dir=_path.default.join(c.paths.rnv.dir,'pluginTemplates');
c.paths.rnv.pluginTemplates.config=_path.default.join(c.paths.rnv.pluginTemplates.dir,'plugins.json');
c.paths.rnv.package=_path.default.join(c.paths.rnv.dir,'package.json');
c.paths.rnv.plugins.dir=_path.default.join(c.paths.rnv.dir,'plugins');
c.paths.rnv.projectTemplate.dir=_path.default.join(c.paths.rnv.dir,'projectTemplate');
c.files.rnv.package=JSON.parse(_fs.default.readFileSync(c.paths.rnv.package).toString());

c.platform=c.program.platform;
c.runtime.platform=c.platform;
c.paths.home.dir=homedir;
c.paths.GLOBAL_RNV_DIR=_path.default.join(c.paths.home.dir,'.rnv');
c.paths.GLOBAL_RNV_CONFIG=_path.default.join(c.paths.GLOBAL_RNV_DIR,_constants.RENATIVE_CONFIG_NAME);

_generateConfigPaths(c.paths.project,base);

c.paths.buildHooks.dir=_path.default.join(c.paths.project.dir,'buildHooks/src');
c.paths.buildHooks.dist.dir=_path.default.join(c.paths.project.dir,'buildHooks/dist');
c.paths.buildHooks.index=_path.default.join(c.paths.buildHooks.dir,'index.js');
c.paths.buildHooks.dist.index=_path.default.join(c.paths.buildHooks.dist.dir,'index.js');
c.paths.project.nodeModulesDir=_path.default.join(c.paths.project.dir,'node_modules');
c.paths.project.srcDir=_path.default.join(c.paths.project.dir,'src');
c.paths.project.appConfigsDir=_path.default.join(c.paths.project.dir,'appConfigs');
c.paths.project.package=_path.default.join(c.paths.project.dir,'package.json');
c.paths.project.rnCliConfig=_path.default.join(c.paths.project.dir,_constants.RN_CLI_CONFIG_NAME);
c.paths.project.babelConfig=_path.default.join(c.paths.project.dir,_constants.RN_BABEL_CONFIG_NAME);
c.paths.project.npmLinkPolyfill=_path.default.join(c.paths.project.dir,'npm_link_polyfill.json');
c.paths.project.projectConfig.dir=_path.default.join(c.paths.project.dir,'projectConfig');
c.paths.project.projectConfig.pluginsDir=_path.default.join(c.paths.project.projectConfig.dir,'plugins');
c.paths.project.projectConfig.fontsDir=_path.default.join(c.paths.project.projectConfig.dir,'fonts');
c.paths.project.assets.dir=_path.default.join(c.paths.project.dir,'platformAssets');
c.paths.project.assets.runtimeDir=_path.default.join(c.paths.project.assets.dir,'runtime');
c.paths.project.assets.config=_path.default.join(c.paths.project.assets.dir,_constants.RENATIVE_CONFIG_RUNTIME_NAME);
c.paths.project.builds.dir=_path.default.join(c.paths.project.dir,'platformBuilds');
c.paths.project.builds.config=_path.default.join(c.paths.project.builds.dir,_constants.RENATIVE_CONFIG_BUILD_NAME);

_generateConfigPaths(c.paths.private,c.paths.GLOBAL_RNV_DIR);

c.files.rnv.pluginTemplates.config=JSON.parse(_fs.default.readFileSync(_path.default.join(c.paths.rnv.pluginTemplates.config)).toString());

return c;
};exports.createRnvConfig=createRnvConfig;

var parseRenativeConfigs=function parseRenativeConfigs(c){return new Promise(function(resolve,reject){
try{var _c$files$project,_c$files$project$conf,_c$files$project$conf2,_c$buildConfig,_c$buildConfig$paths;

if(!loadFile(c.files.project.builds,c.paths.project.builds,'config'));


loadFile(c.files.project,c.paths.project,'package');


_loadConfigFiles(c,c.files.project,c.paths.project);
c.runtime.appId=c.program.appConfigID||((_c$files$project=c.files.project)==null?void 0:(_c$files$project$conf=_c$files$project.configLocal)==null?void 0:(_c$files$project$conf2=_c$files$project$conf._meta)==null?void 0:_c$files$project$conf2.currentAppConfigId);
if(!c.files.project.config)return resolve();


_generateConfigPaths(c.paths.private,(0,_fileutils.getRealPath)(c,(_c$buildConfig=c.buildConfig)==null?void 0:(_c$buildConfig$paths=_c$buildConfig.paths)==null?void 0:_c$buildConfig$paths.globalConfigDir)||c.paths.GLOBAL_RNV_DIR);
_loadConfigFiles(c,c.files.private,c.paths.private);


_generateConfigPaths(c.paths.private.project,_path.default.join(c.paths.private.dir,c.files.project.config.projectName));
_loadConfigFiles(c,c.files.private.project,c.paths.private.project);


c.paths.private.project.projectConfig.dir=_path.default.join(c.paths.private.project.dir,'projectConfig');
c.paths.private.project.appConfigsDir=_path.default.join(c.paths.private.project.dir,'appConfigs');

_findAndSwitchAppConfigDir(c);

c.runtime.isWrapper=c.buildConfig.isWrapper;

c.paths.project.platformTemplatesDirs=_generatePlatformTemplatePaths(c);
}catch(e){
reject(e);
return;
}

resolve();
});};exports.parseRenativeConfigs=parseRenativeConfigs;

var checkIsRenativeProject=function checkIsRenativeProject(c){return new Promise(function(resolve,reject){
if(!c.paths.project.configExists){
return reject("Looks like this directory is not ReNative project. Project config "+
_chalk.default.white(
c.paths.project.config)+" is missing!. You can create new project with "+
_chalk.default.white('rnv new'));

}

return resolve();
});};exports.checkIsRenativeProject=checkIsRenativeProject;

var fixRenativeConfigsSync=function fixRenativeConfigsSync(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('fixRenativeConfigsSync');





(0,_projectParser.checkAndCreateGitignore)(c);


(0,_logger.logTask)('configureProject:check rn-cli',_chalk.default.grey);
if(!_fs.default.existsSync(c.paths.project.rnCliConfig)){
(0,_logger.logInfo)("Looks like your rn-cli config file "+
_chalk.default.white(c.paths.project.rnCliConfig)+" is missing! Let's create one for you.");

(0,_fileutils.copyFileSync)(_path.default.join(c.paths.rnv.projectTemplate.dir,_constants.RN_CLI_CONFIG_NAME),c.paths.project.rnCliConfig);
}


(0,_logger.logTask)('configureProject:check babel config',_chalk.default.grey);
if(!_fs.default.existsSync(c.paths.project.babelConfig)){
(0,_logger.logInfo)("Looks like your babel config file "+
_chalk.default.white(c.paths.project.babelConfig)+" is missing! Let's create one for you.");

(0,_fileutils.copyFileSync)(_path.default.join(c.paths.rnv.dir,_constants.RN_BABEL_CONFIG_NAME),c.paths.project.babelConfig);
}

resolve();
});};exports.fixRenativeConfigsSync=fixRenativeConfigsSync;

var _generateConfigPaths=function _generateConfigPaths(pathObj,dir){
pathObj.dir=dir;
pathObj.config=_path.default.join(dir,_constants.RENATIVE_CONFIG_NAME);
pathObj.configLocal=_path.default.join(dir,_constants.RENATIVE_CONFIG_LOCAL_NAME);
pathObj.configPrivate=_path.default.join(dir,_constants.RENATIVE_CONFIG_PRIVATE_NAME);
};

var versionCheck=function versionCheck(c){return new Promise(function(resolve,reject){var _c$files$rnv,_c$files$rnv$package,_c$files$project2,_c$files$project2$pac,_c$files$project2$pac2;
(0,_logger.logTask)('versionCheck');

if(c.runtime.isWrapper){
resolve();
return;
}
c.runtime.rnvVersionRunner=(_c$files$rnv=c.files.rnv)==null?void 0:(_c$files$rnv$package=_c$files$rnv.package)==null?void 0:_c$files$rnv$package.version;
c.runtime.rnvVersionProject=(_c$files$project2=c.files.project)==null?void 0:(_c$files$project2$pac=_c$files$project2.package)==null?void 0:(_c$files$project2$pac2=_c$files$project2$pac.devDependencies)==null?void 0:_c$files$project2$pac2.rnv;
(0,_logger.logTask)("versionCheck:rnvRunner:"+c.runtime.rnvVersionRunner+",rnvProject:"+c.runtime.rnvVersionProject,_chalk.default.grey);
if(c.runtime.rnvVersionRunner&&c.runtime.rnvVersionProject){
if(c.runtime.rnvVersionRunner!==c.runtime.rnvVersionProject){
var recCmd=_chalk.default.white("$ npx "+(0,_logger.getCurrentCommand)(true));
(0,_logger.logWarning)("You are running $rnv v"+_chalk.default.red(c.runtime.rnvVersionRunner)+" against project built with rnv v"+_chalk.default.red(c.runtime.rnvVersionProject)+". This might result in unexpected behaviour! It is recommended that you run your rnv command with npx prefix: "+recCmd+" . or manually update your devDependencies.rnv version in your package.json\n\nIf you see this error while running rnv with npx, you need some beer, then chill, then run: npm i\n");


(0,_prompt.askQuestion)('Do you want to proceed anyway? (y/n)').
then(function(v){
if(v==='y'){
resolve();
}else{
reject('Action cancelled');
}
(0,_prompt.finishQuestion)();
}).catch(function(e){
reject(e);
(0,_prompt.finishQuestion)();
});
}else{
resolve();
}
}else{
resolve();
}
});};exports.versionCheck=versionCheck;

var loadFile=function loadFile(fileObj,pathObj,key){
if(!_fs.default.existsSync(pathObj[key])){
pathObj[key+"Exists"]=false;
(0,_logger.logDebug)("WARNING: loadFile: Path "+pathObj[key]+" does not exists!");
return false;
}
pathObj[key+"Exists"]=true;
try{
fileObj[key]=JSON.parse(_fs.default.readFileSync(pathObj[key]).toString());
pathObj[key+"Exists"]=true;
return true;
}catch(e){
(0,_logger.logError)("loadFile: "+pathObj[key]+" :: "+e,true);
return false;
}
};exports.loadFile=loadFile;

var _loadConfigFiles=function _loadConfigFiles(c,fileObj,pathObj,extendDir){
var result=false;
var extendAppId;
if(loadFile(fileObj,pathObj,'config')){
extendAppId=fileObj.config.extend||extendAppId;
result=true;
}

if(loadFile(fileObj,pathObj,'configLocal')){
extendAppId=fileObj.configLocal.extend||extendAppId;
result=true;
}

if(loadFile(fileObj,pathObj,'configPrivate')){
extendAppId=fileObj.configPrivate.extend||extendAppId;
result=true;
}

if(extendAppId&&extendDir){
pathObj.configBase=_path.default.join(extendDir,extendAppId,'renative.json');
pathObj.dirs=[
_path.default.join(extendDir,extendAppId),
pathObj.dir];

loadFile(fileObj,pathObj,'configBase');
}


generateBuildConfig(c);
return result;
};


var setAppConfig=function setAppConfig(c,appId){
(0,_logger.logTask)("setAppConfig:"+appId);

if(!appId||appId==='?')return;

c.runtime.appId=appId;
c.runtime.appDir=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+c.runtime.platform);

_findAndSwitchAppConfigDir(c,appId);

_generateConfigPaths(c.paths.appConfig,_path.default.join(c.paths.project.appConfigsDir,appId));
_loadConfigFiles(c,c.files.appConfig,c.paths.appConfig,c.paths.project.appConfigsDir);

_generateConfigPaths(c.paths.private.appConfig,_path.default.join(c.paths.private.project.appConfigsDir,appId));

_loadConfigFiles(c,c.files.private.appConfig,c.paths.private.appConfig,c.paths.private.project.appConfigsDir);
generateBuildConfig(c);
generateLocalConfig(c);
};exports.setAppConfig=setAppConfig;

var _findAndSwitchAppConfigDir=function _findAndSwitchAppConfigDir(c,appId){var _c$buildConfig$paths2,_c$buildConfig$paths3;
(0,_logger.logTask)("_findAndSwitchAppConfigDir:"+appId);

c.paths.project.appConfigsDir=(0,_fileutils.getRealPath)(c,(_c$buildConfig$paths2=c.buildConfig.paths)==null?void 0:_c$buildConfig$paths2.appConfigsDir,'appConfigsDir',c.paths.project.appConfigsDir);
if(((_c$buildConfig$paths3=c.buildConfig.paths)==null?void 0:_c$buildConfig$paths3.appConfigsDirs)&&appId){
c.buildConfig.paths.appConfigsDirs.forEach(function(v){
var altPath=_path.default.join(v,appId);
if(_fs.default.existsSync(altPath)){
(0,_logger.logInfo)("Found config in following location: "+altPath+". Will use it");
c.paths.project.appConfigsDir=v;
}
});
}
};

var _arrayMergeOverride=function _arrayMergeOverride(destinationArray,sourceArray,mergeOptions){return sourceArray;};

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

var mergeOrder=[
c.paths.project.config,
c.paths.project.configPrivate,
c.paths.project.configLocal,
c.paths.private.project.config,
c.paths.private.project.configPrivate,
c.paths.private.project.configLocal,
c.paths.appConfig.configBase,
c.paths.appConfig.config,
c.paths.appConfig.configPrivate,
c.paths.appConfig.configLocal,
c.paths.private.appConfig.configBase,
c.paths.private.appConfig.config,
c.paths.private.appConfig.configPrivate,
c.paths.private.appConfig.configLocal];

var cleanPaths=mergeOrder.filter(function(v){return v;});
var existsPaths=cleanPaths.filter(function(v){
var exists=_fs.default.existsSync(v);
if(exists){
(0,_logger.logDebug)("Merged: "+v);

}else{

}
return exists;
});

var mergeFiles=[
c.files.project.config,
c.files.project.configPrivate,
c.files.project.configLocal,
c.files.private.project.config,
c.files.private.project.configPrivate,
c.files.private.project.configLocal,
c.files.appConfig.configBase,
c.files.appConfig.config,
c.files.appConfig.configPrivate,
c.files.appConfig.configLocal,
c.files.private.appConfig.configBase,
c.files.private.appConfig.config,
c.files.private.appConfig.configPrivate,
c.files.private.appConfig.configLocal];


var mergeFolders=[

c.paths.rnv.platformTemplate.dir,
c.paths.project.projectConfig.buildsDir,
c.paths.private.project.projectConfig.buildsDir,

c.paths.appConfig.buildsDir,
c.paths.private.appConfig.buildsDir];








(0,_logger.logDebug)('mergeFolders:',mergeFolders);

var meta=[{
_meta:{
generated:new Date().getTime(),
mergedConfigs:existsPaths}}];



var existsFiles=mergeFiles.filter(function(v,i){return v;});

(0,_logger.logTask)("generateBuildConfig:"+mergeOrder.length+":"+cleanPaths.length+":"+existsPaths.length+":"+existsFiles.length,_chalk.default.grey);

var out=_deepmerge.default.all([].concat(meta,(0,_toConsumableArray2.default)(existsFiles)),{arrayMerge:_arrayMergeOverride});
out=(0,_deepmerge.default)({},out);
(0,_logger.logDebug)("generateBuildConfig: will sanitize file at: "+c.paths.project.builds.config);
c.buildConfig=(0,_fileutils.sanitizeDynamicRefs)(c,out);
c.buildConfig=(0,_fileutils.sanitizeDynamicProps)(c.buildConfig,c.buildConfig._refs);
if(_fs.default.existsSync(c.paths.project.builds.dir)){
(0,_fileutils.writeObjectSync)(c.paths.project.builds.config,c.buildConfig);
}
};exports.generateBuildConfig=generateBuildConfig;

var generateRuntimeConfig=function generateRuntimeConfig(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('generateRuntimeConfig');
c.assetConfig={
common:c.buildConfig.common,
runtime:c.buildConfig.runtime};


if(_fs.default.existsSync(c.paths.project.assets.dir)){
(0,_fileutils.writeObjectSync)(c.paths.project.assets.config,c.assetConfig);
}
resolve();
});};exports.generateRuntimeConfig=generateRuntimeConfig;

var generateLocalConfig=function generateLocalConfig(c,resetAppId){
(0,_logger.logTask)("generateLocalConfig:"+resetAppId+":"+c.paths.project.configLocal);
var configLocal=c.files.project.configLocal||{};
configLocal._meta=configLocal._meta||{};
if(resetAppId){
delete configLocal._meta.currentAppConfigId;
}else{
configLocal._meta.currentAppConfigId=c.runtime.appId;
}
c.files.project.configLocal=configLocal;
(0,_fileutils.writeObjectSync)(c.paths.project.configLocal,configLocal);
};exports.generateLocalConfig=generateLocalConfig;

var _generatePlatformTemplatePaths=function _generatePlatformTemplatePaths(c){
var pt=c.buildConfig.platformTemplatesDirs||{};
var originalPath=c.buildConfig.platformTemplatesDir||'RNV_HOME/platformTemplates';
var result={};
_constants.SUPPORTED_PLATFORMS.forEach(function(v){
if(!pt[v]){
result[v]=(0,_fileutils.getRealPath)(
c,
originalPath,
'platformTemplatesDir',
originalPath);

}else{
result[v]=(0,_fileutils.getRealPath)(
c,
pt[v],
'platformTemplatesDir',
originalPath);

}
});
return result;
};

var updateConfig=function updateConfig(c,appConfigId){var configDirs,_ref,_conf,_ref2,conf;return _regenerator.default.async(function updateConfig$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)("updateConfig:"+appConfigId);

setAppConfig(c,appConfigId);if(

_fs.default.existsSync(c.paths.appConfig.dir)){_context.next=21;break;}
configDirs=listAppConfigsFoldersSync(c,true);

if(!appConfigId){
(0,_logger.logWarning)(
'It seems you don\'t have any appConfig active');

}else if(appConfigId!=='?'){
(0,_logger.logWarning)("It seems you don't have appConfig named "+
_chalk.default.white(appConfigId)+" present in your config folder: "+_chalk.default.white(
c.paths.project.appConfigsDir)+" !");


}if(!

configDirs.length){_context.next=13;break;}_context.next=8;return _regenerator.default.awrap(
_inquirer.default.prompt({
name:'conf',
type:'list',
message:'ReNative found existing appConfigs. Which one would you like to pick?',
choices:configDirs,
pageSize:50}));case 8:_ref=_context.sent;_conf=_ref.conf;if(!


_conf){_context.next=13;break;}
setAppConfig(c,_conf);return _context.abrupt("return",
true);case 13:_context.next=15;return _regenerator.default.awrap(


_inquirer.default.prompt({
name:'conf',
type:'confirm',
message:"Do you want ReNative to create new sample appConfig ("+_chalk.default.white(
appConfigId)+") for you?"}));case 15:_ref2=_context.sent;conf=_ref2.conf;if(!



conf){_context.next=21;break;}
setAppConfig(c,_constants.SAMPLE_APP_ID);
(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(c.paths.rnv.dir,'appConfigs',_constants.SAMPLE_APP_ID),
_path.default.join(c.paths.appConfig.dir));return _context.abrupt("return",

true);case 21:return _context.abrupt("return",


true);case 22:case"end":return _context.stop();}}});};exports.updateConfig=updateConfig;


var listAppConfigsFoldersSync=function listAppConfigsFoldersSync(c,ignoreHiddenConfigs){var _c$buildConfig2,_c$buildConfig2$paths;
(0,_logger.logTask)("listAppConfigsFoldersSync:"+ignoreHiddenConfigs);
var configDirs=[];
if((_c$buildConfig2=c.buildConfig)==null?void 0:(_c$buildConfig2$paths=_c$buildConfig2.paths)==null?void 0:_c$buildConfig2$paths.appConfigsDirs){
c.buildConfig.paths.appConfigsDirs.forEach(function(v){
_listAppConfigsFoldersSync(v,configDirs,ignoreHiddenConfigs);
});
}else{
_listAppConfigsFoldersSync(c.paths.project.appConfigsDir,configDirs,ignoreHiddenConfigs);
}

return configDirs;
};exports.listAppConfigsFoldersSync=listAppConfigsFoldersSync;

var _listAppConfigsFoldersSync=function _listAppConfigsFoldersSync(dirPath,configDirs,ignoreHiddenConfigs){
if(!_fs.default.existsSync(dirPath))return;
_fs.default.readdirSync(dirPath).forEach(function(dir){
var appConfigDir=_path.default.join(dirPath,dir);
if(!IGNORE_FOLDERS.includes(dir)&&_fs.default.lstatSync(appConfigDir).isDirectory()){
if(ignoreHiddenConfigs){
var appConfig=_path.default.join(appConfigDir,_constants.RENATIVE_CONFIG_NAME);
if(_fs.default.existsSync(appConfig)){
try{
var config=(0,_fileutils.readObjectSync)(appConfig);
if((config==null?void 0:config.hidden)!==true){
configDirs.push(dir);
}
}catch(e){
(0,_logger.logWarning)("_listAppConfigsFoldersSync: "+e);
}
}
}else{
configDirs.push(dir);
}
}
});
};

var configureRnvGlobal=function configureRnvGlobal(c){return new Promise(function(resolve,reject){
_configureRnvGlobal(c).
then(function(){return _configureRnvGlobal(c);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};exports.configureRnvGlobal=configureRnvGlobal;

var _configureRnvGlobal=function _configureRnvGlobal(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('configureRnvGlobal');

if(_fs.default.existsSync(c.paths.private.dir)){
(0,_logger.logDebug)(c.paths.private.dir+" folder exists!");
}else{
console.log(c.paths.private.dir+" folder missing! Creating one for you...");
(0,_fileutils.mkdirSync)(c.paths.private.dir);
}


if(_fs.default.existsSync(c.paths.private.config)){
(0,_logger.logDebug)(c.paths.private.dir+"/"+_constants.RENATIVE_CONFIG_NAME+" file exists!");
}else{
var oldGlobalConfigPath=_path.default.join(c.paths.private.dir,'config.json');
if(_fs.default.existsSync(oldGlobalConfigPath)){
(0,_logger.logWarning)('Found old version of your config. will copy it to new renative.json config');
(0,_fileutils.copyFileSync)(oldGlobalConfigPath,c.paths.private.config);
}else{
console.log(c.paths.private.dir+"/"+_constants.RENATIVE_CONFIG_NAME+" file missing! Creating one for you...");
(0,_fileutils.copyFileSync)(_path.default.join(c.paths.rnv.dir,'supportFiles','global-config-template.json'),c.paths.private.config);
console.log("Don't forget to Edit: "+

c.paths.private.dir+"/"+
_constants.RENATIVE_CONFIG_NAME+" with correct paths to your SDKs before continuing!");

}
}

if(_fs.default.existsSync(c.paths.private.config)){var _c$files$private$conf;
c.files.private.config=JSON.parse(_fs.default.readFileSync(c.paths.private.config).toString());

if((_c$files$private$conf=c.files.private.config)==null?void 0:_c$files$private$conf.appConfigsPath){
if(!_fs.default.existsSync(c.files.private.config.appConfigsPath)){
(0,_logger.logWarning)("Looks like your custom global appConfig is pointing to "+
_chalk.default.white(
c.files.private.config.appConfigsPath)+" which doesn't exist! Make sure you create one in that location");


}else{
(0,_logger.logInfo)("Found custom appConfing location pointing to "+
_chalk.default.white(
c.files.private.config.appConfigsPath)+". ReNativewill now swith to that location!");


c.paths.project.appConfigsDir=c.files.private.config.appConfigsPath;
}
}

var isRunningOnWindows=process.platform==='win32';var


sdks=c.files.private.config.sdks;
if(sdks){
if(sdks.ANDROID_SDK){
c.cli[_constants.CLI_ANDROID_EMULATOR]=_path.default.join(sdks.ANDROID_SDK,"emulator/emulator"+(isRunningOnWindows?'.exe':''));
c.cli[_constants.CLI_ANDROID_ADB]=_path.default.join(sdks.ANDROID_SDK,"platform-tools/adb"+(isRunningOnWindows?'.exe':''));
c.cli[_constants.CLI_ANDROID_AVDMANAGER]=_path.default.join(sdks.ANDROID_SDK,"tools/bin/avdmanager"+(isRunningOnWindows?'.bat':''));
c.cli[_constants.CLI_ANDROID_SDKMANAGER]=_path.default.join(sdks.ANDROID_SDK,"tools/bin/sdkmanager"+(isRunningOnWindows?'.bat':''));
}
if(sdks.TIZEN_SDK){
c.cli[_constants.CLI_TIZEN_EMULATOR]=_path.default.join(sdks.TIZEN_SDK,"tools/emulator/bin/em-cli"+(isRunningOnWindows?'.bat':''));
c.cli[_constants.CLI_TIZEN]=_path.default.join(sdks.TIZEN_SDK,"tools/ide/bin/tizen"+(isRunningOnWindows?'.bat':''));
c.cli[_constants.CLI_SDB_TIZEN]=_path.default.join(sdks.TIZEN_SDK,'tools/sdb');
}
if(sdks.WEBOS_SDK){
c.cli[_constants.CLI_WEBOS_ARES]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares"+(isRunningOnWindows?'.cmd':''));
c.cli[_constants.CLI_WEBOS_ARES_PACKAGE]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares-package"+(isRunningOnWindows?'.cmd':''));
c.cli[_constants.CLI_WEBOS_ARES_INSTALL]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares-install"+(isRunningOnWindows?'.cmd':''));
c.cli[_constants.CLI_WEBOS_ARES_LAUNCH]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares-launch"+(isRunningOnWindows?'.cmd':''));
c.cli[_constants.CLI_WEBOS_ARES_SETUP_DEVICE]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares-setup-device"+(isRunningOnWindows?'.cmd':''));
c.cli[_constants.CLI_WEBOS_ARES_DEVICE_INFO]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares-device-info"+(isRunningOnWindows?'.cmd':''));
c.cli[_constants.CLI_WEBOS_ARES_NOVACOM]=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"CLI/bin/ares-novacom"+(isRunningOnWindows?'.cmd':''));
}
}else{
(0,_logger.logWarning)("Your "+c.paths.private.config+" is missing SDK configuration object");
}



if(c.files.private.config.defaultTargets===undefined){
(0,_logger.logWarning)("Looks like you're missing defaultTargets in your config "+
_chalk.default.white(c.paths.private.config)+". Let's add them!");

var defaultConfig=JSON.parse(
_fs.default.readFileSync(_path.default.join(c.paths.rnv.dir,'supportFiles','global-config-template.json')).toString());

var newConfig=_objectSpread({},c.files.private.config,{defaultTargets:defaultConfig.defaultTargets});
_fs.default.writeFileSync(c.paths.private.config,JSON.stringify(newConfig,null,2));
}
}

resolve();
});};