var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.configureRnvGlobal=exports.loadPlatformTemplates=exports.loadPluginTemplates=exports.loadProjectTemplates=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 _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _deepmerge=_interopRequireDefault(require("deepmerge"));
var _constants=require("../constants");






























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




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




var _projectParser=require("../projectTools/projectParser");



var _prompt=require("../systemTools/prompt");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:{},
projectTemplates:{},
platformTemplate:{},
plugins:{},
projectTemplate:{}},

global:{},


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

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

appConfig:{}},

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

appConfig:{}}},



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

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

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

appConfig:{}},

defaultWorkspace:{
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.platformTemplates.config=_path.default.join(c.paths.rnv.platformTemplates.dir,_constants.RENATIVE_CONFIG_PLATFORMS_NAME);
c.paths.rnv.pluginTemplates.config=_path.default.join(c.paths.rnv.pluginTemplates.dir,_constants.RENATIVE_CONFIG_PLUGINS_NAME);
c.paths.rnv.projectTemplates.dir=_path.default.join(c.paths.rnv.dir,'projectTemplates');
c.paths.rnv.projectTemplates.config=_path.default.join(c.paths.rnv.projectTemplates.dir,_constants.RENATIVE_CONFIG_TEMPLATES_NAME);
c.paths.rnv.package=_path.default.join(c.paths.rnv.dir,'package.json');
c.paths.rnv.package=_path.default.join(c.paths.rnv.dir,'package.json');

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);
c.paths.rnv.configWorkspaces=_path.default.join(c.paths.GLOBAL_RNV_DIR,_constants.RENATIVE_CONFIG_WORKSPACES_NAME);

if(!_fs.default.existsSync(c.paths.GLOBAL_RNV_DIR))(0,_fileutils.mkdirSync)(c.paths.GLOBAL_RNV_DIR);

_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.workspace,c.paths.GLOBAL_RNV_DIR);


try{
_loadWorkspacesSync(c);
}catch(e){
(0,_logger.logError)(e);
}

return c;
};exports.createRnvConfig=createRnvConfig;

var parseRenativeConfigs=function parseRenativeConfigs(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('parseRenativeConfigs');
try{var _c$files$project,_c$files$project$conf,_c$files$project$conf2;

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);
c.paths.project.builds.config=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+c.platform+".json");


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


_generateConfigPaths(c.paths.workspace,(0,_fileutils.getRealPath)(c,_getWorkspaceDirPath(c)));
_loadConfigFiles(c,c.files.workspace,c.paths.workspace);


_generateConfigPaths(c.paths.defaultWorkspace,c.paths.GLOBAL_RNV_DIR);
_loadConfigFiles(c,c.files.defaultWorkspace,c.paths.defaultWorkspace);


loadProjectTemplates(c);


loadPluginTemplates(c);


loadPlatformTemplates(c);

if(!c.files.project.config)return resolve();


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


c.paths.workspace.project.projectConfig.dir=_path.default.join(c.paths.workspace.project.dir,'projectConfig');


_findAndSwitchAppConfigDir(c);

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

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

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

var _getWorkspaceDirPath=function _getWorkspaceDirPath(c){var _c$buildConfig,_c$buildConfig2,_c$buildConfig2$paths;
(0,_logger.logTask)('_getWorkspaceDirPath');
var wss=c.files.rnv.configWorkspaces;
var ws=c.runtime.selectedWorkspace||((_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.workspaceID);

var dirPath;
if((wss==null?void 0:wss.workspaces)&&ws){var _wss$workspaces$ws;
dirPath=(_wss$workspaces$ws=wss.workspaces[ws])==null?void 0:_wss$workspaces$ws.path;
if(!dirPath){
var wsDir=_path.default.join(c.paths.home.dir,"."+ws);
if(_fs.default.existsSync(wsDir)){
wss.workspaces[ws]={
path:wsDir};

(0,_fileutils.writeObjectSync)(c.paths.rnv.configWorkspaces,wss);
(0,_logger.logInfo)("Found workspace id "+ws+" and compatible directory "+wsDir+". Your "+c.paths.rnv.configWorkspaces+" has been updated.");
}
}
}
if((_c$buildConfig2=c.buildConfig)==null?void 0:(_c$buildConfig2$paths=_c$buildConfig2.paths)==null?void 0:_c$buildConfig2$paths.globalConfigDir){
(0,_logger.logWarning)("paths.globalConfigDir in "+c.paths.project.config+" is DEPRECATED. use workspaceID instead. more info at https://renative.org/docs/workspaces");
}
if(!dirPath){var _c$buildConfig3,_c$buildConfig3$paths;
return((_c$buildConfig3=c.buildConfig)==null?void 0:(_c$buildConfig3$paths=_c$buildConfig3.paths)==null?void 0:_c$buildConfig3$paths.globalConfigDir)||c.paths.GLOBAL_RNV_DIR;
}
return dirPath;
};

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){var _c$files$rnv,_c$files$rnv$package,_c$files$project2,_c$files$project2$pac,_c$files$project2$pac2;var recCmd,actionNoUpdate,actionWithUpdate,actionUpgrade,_ref,chosenAction;return _regenerator.default.async(function versionCheck$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('versionCheck');if(!(

c.runtime.isWrapper||c.runtime.versionCheckCompleted)){_context.next=3;break;}return _context.abrupt("return",
true);case 3:

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)){_context.next=19;break;}if(!(
c.runtime.rnvVersionRunner!==c.runtime.rnvVersionProject)){_context.next=19;break;}
recCmd=_chalk.default.white("$ npx "+(0,_logger.getCurrentCommand)(true));
actionNoUpdate='Continue and skip updating package.json';
actionWithUpdate='Continue and update package.json';
actionUpgrade="Upgrade project to "+c.runtime.rnvVersionRunner;_context.next=14;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
message:'What to do next?',
type:'list',
name:'chosenAction',
choices:[
actionNoUpdate,
actionWithUpdate,
actionUpgrade],

warningMessage:"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."}));case 14:_ref=_context.sent;chosenAction=_ref.chosenAction;


c.runtime.versionCheckCompleted=true;

c.runtime.skipPackageUpdate=chosenAction===actionNoUpdate;

if(chosenAction===actionUpgrade){
(0,_projectParser.upgradeProjectDependencies)(c,c.runtime.rnvVersionRunner);
}case 19:case"end":return _context.stop();}}});};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];

pathObj.fontDirs=[
_path.default.join(pathObj.dirs[0],'fonts'),
_path.default.join(pathObj.dirs[1],'fonts')];

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


generateBuildConfig(c);
return result;
};


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

if(!appId||appId==='?'||appId===true)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));
c.paths.appConfig.fontsDir=_path.default.join(c.paths.appConfig.dir,'fonts');
_loadConfigFiles(c,c.files.appConfig,c.paths.appConfig,c.paths.project.appConfigsDir);

var workspaceAppConfigsDir=(0,_fileutils.getRealPath)(c,c.buildConfig.workspaceAppConfigsDir);
c.paths.workspace.project.appConfigsDir=workspaceAppConfigsDir||_path.default.join(c.paths.workspace.project.dir,'appConfigs');

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

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

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

c.paths.project.appConfigsDir=(0,_fileutils.getRealPath)(c,(_c$buildConfig$paths=c.buildConfig.paths)==null?void 0:_c$buildConfig$paths.appConfigsDir,'appConfigsDir',c.paths.project.appConfigsDir);
if(((_c$buildConfig$paths2=c.buildConfig.paths)==null?void 0:_c$buildConfig$paths2.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.rnv.projectTemplates.config,
c.paths.rnv.pluginTemplates.config,
c.files.rnv.platformTemplates.config,
c.paths.workspace.config,
c.paths.project.config,
c.paths.project.configPrivate,
c.paths.project.configLocal,
c.paths.workspace.project.config,
c.paths.workspace.project.configPrivate,
c.paths.workspace.project.configLocal,
c.paths.appConfig.configBase,
c.paths.appConfig.config,
c.paths.appConfig.configPrivate,
c.paths.appConfig.configLocal,
c.paths.workspace.appConfig.configBase,
c.paths.workspace.appConfig.config,
c.paths.workspace.appConfig.configPrivate,
c.paths.workspace.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 pluginTemplates=[];
if(c.files.rnv.pluginTemplates.configs){
pluginTemplates=Object.keys(c.files.rnv.pluginTemplates.configs).map(function(v){return c.files.rnv.pluginTemplates.configs[v];});
}

var mergeFiles=[
c.files.rnv.projectTemplates.config].concat((0,_toConsumableArray2.default)(
pluginTemplates),[
c.files.workspace.config,
c.files.project.config,
c.files.project.configPrivate,
c.files.project.configLocal,
c.files.workspace.project.config,
c.files.workspace.project.configPrivate,
c.files.workspace.project.configLocal,
c.files.appConfig.configBase,
c.files.appConfig.config,
c.files.appConfig.configPrivate,
c.files.appConfig.configLocal,
c.files.workspace.appConfig.configBase,
c.files.workspace.appConfig.config,
c.files.workspace.appConfig.configPrivate,
c.files.workspace.appConfig.configLocal]);


var mergeFolders=[

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

c.paths.appConfig.buildsDir,
c.paths.workspace.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);
}
var localMetroPath=_path.default.join(c.paths.project.dir,'metro.config.local.js');

if(c.platform){
_fs.default.writeFileSync(localMetroPath,"module.exports = "+(0,_common.getSourceExtsAsString)(c));
}else if(!_fs.default.existsSync(localMetroPath)){
_fs.default.writeFileSync(localMetroPath,'module.exports = []');
}
};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 isPureRnv,configDirs,_ref2,_conf,_ref3,conf;return _regenerator.default.async(function updateConfig$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)("updateConfig:"+appConfigId);

setAppConfig(c,appConfigId);

isPureRnv=!c.command&&!c.subCommand;if(!(

!_fs.default.existsSync(c.paths.appConfig.dir)||isPureRnv)){_context2.next=25;break;}
configDirs=listAppConfigsFoldersSync(c,true);

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

}else if(appConfigId!=='?'&&!isPureRnv){
(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){_context2.next=17;break;}if(!(
configDirs.length===1)){_context2.next=10;break;}

setAppConfig(c,configDirs[0]);return _context2.abrupt("return",
true);case 10:_context2.next=12;return _regenerator.default.awrap(


(0,_prompt.inquirerPrompt)({
name:'conf',
type:'list',
message:'Which one would you like to pick?',
choices:configDirs,
pageSize:50,
logMessage:'ReNative found multiple existing appConfigs'}));case 12:_ref2=_context2.sent;_conf=_ref2.conf;if(!


_conf){_context2.next=17;break;}
setAppConfig(c,_conf);return _context2.abrupt("return",
true);case 17:_context2.next=19;return _regenerator.default.awrap(


(0,_prompt.inquirerPrompt)({
name:'conf',
type:'confirm',
message:"Do you want ReNative to create new sample appConfig ("+_chalk.default.white(
appConfigId)+") for you?",

warningMessage:'No app configs found for this project'}));case 19:_ref3=_context2.sent;conf=_ref3.conf;if(!


conf){_context2.next=25;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 _context2.abrupt("return",

true);case 25:return _context2.abrupt("return",


true);case 26:case"end":return _context2.stop();}}});};exports.updateConfig=updateConfig;


var listAppConfigsFoldersSync=function listAppConfigsFoldersSync(c,ignoreHiddenConfigs){var _c$buildConfig4,_c$buildConfig4$paths;
(0,_logger.logTask)("listAppConfigsFoldersSync:"+ignoreHiddenConfigs);
var configDirs=[];
if((_c$buildConfig4=c.buildConfig)==null?void 0:(_c$buildConfig4$paths=_c$buildConfig4.paths)==null?void 0:_c$buildConfig4$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 loadProjectTemplates=function loadProjectTemplates(c){
c.files.rnv.projectTemplates.config=(0,_fileutils.readObjectSync)(c.paths.rnv.projectTemplates.config);
};exports.loadProjectTemplates=loadProjectTemplates;

var loadPluginTemplates=function loadPluginTemplates(c){var _c$files$project$conf3,_c$files$project$conf4;
c.files.rnv.pluginTemplates.config=(0,_fileutils.readObjectSync)(c.paths.rnv.pluginTemplates.config);

c.files.rnv.pluginTemplates.configs={
rnv:c.files.rnv.pluginTemplates.config};


c.paths.rnv.pluginTemplates.dirs=[c.paths.rnv.pluginTemplates.dir];

var customPluginTemplates=(_c$files$project$conf3=c.files.project.config)==null?void 0:(_c$files$project$conf4=_c$files$project$conf3.paths)==null?void 0:_c$files$project$conf4.pluginTemplates;

if(customPluginTemplates){
Object.keys(customPluginTemplates).forEach(function(k){
var val=customPluginTemplates[k];
if(val.npm){var _c$files$project$pack,_c$files$project$pack2;
var npmDep=((_c$files$project$pack=c.files.project.package)==null?void 0:_c$files$project$pack.dependencies[val.npm])||((_c$files$project$pack2=c.files.project.package)==null?void 0:_c$files$project$pack2.devDependencies[val.npm]);

if(npmDep){
var ptPath;
if(npmDep.startsWith('file:')){
ptPath=_path.default.join(c.paths.project.dir,npmDep.replace('file:',''),val.path||'');
}else{
ptPath=_path.default.join(c.paths.project.nodeModulesDir,val.npm,val.path||'');
}

var ptConfig=_path.default.join(ptPath,_constants.RENATIVE_CONFIG_PLUGINS_NAME);
c.paths.rnv.pluginTemplates.dirs.push(ptPath);
if(_fs.default.existsSync(ptConfig)){
c.files.rnv.pluginTemplates.configs[k]=(0,_fileutils.readObjectSync)(ptConfig);
}
}
}
});
}
};exports.loadPluginTemplates=loadPluginTemplates;

var loadPlatformTemplates=function loadPlatformTemplates(c){
c.files.rnv.platformTemplates.config=(0,_fileutils.readObjectSync)(c.paths.rnv.platformTemplates.config);
};exports.loadPlatformTemplates=loadPlatformTemplates;

var _loadWorkspacesSync=function _loadWorkspacesSync(c){

if(_fs.default.existsSync(c.paths.rnv.configWorkspaces)){
(0,_logger.logDebug)(c.paths.rnv.configWorkspaces+" file exists!");
c.files.rnv.configWorkspaces=(0,_fileutils.readObjectSync)(c.paths.rnv.configWorkspaces);
}else{
(0,_logger.logWarning)("Cannot find "+c.paths.rnv.configWorkspaces+". creating one..");
c.files.rnv.configWorkspaces={
workspaces:{
rnv:{
path:c.paths.workspace.dir}}};



(0,_fileutils.writeObjectSync)(c.paths.rnv.configWorkspaces,c.files.rnv.configWorkspaces);
}
};

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



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


if(_fs.default.existsSync(c.paths.workspace.config)){
(0,_logger.logDebug)(c.paths.workspace.dir+"/"+_constants.RENATIVE_CONFIG_NAME+" file exists!");
}else{
var oldGlobalConfigPath=_path.default.join(c.paths.workspace.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.workspace.config);
}else{
console.log(c.paths.workspace.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.workspace.config);
console.log("Don't forget to Edit: "+

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

}
}

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

if((_c$files$workspace$co=c.files.workspace.config)==null?void 0:_c$files$workspace$co.appConfigsPath){
if(!_fs.default.existsSync(c.files.workspace.config.appConfigsPath)){
(0,_logger.logWarning)("Looks like your custom global appConfig is pointing to "+
_chalk.default.white(
c.files.workspace.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.workspace.config.appConfigsPath)+". ReNativewill now swith to that location!");


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

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


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



if(c.files.workspace.config.defaultTargets===undefined){
(0,_logger.logWarning)("Looks like you're missing defaultTargets in your config "+
_chalk.default.white(c.paths.workspace.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.workspace.config,{defaultTargets:defaultConfig.defaultTargets});
_fs.default.writeFileSync(c.paths.workspace.config,JSON.stringify(newConfig,null,2));
}
}

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