var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.taskRnvAppConfigure=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _configManager=require("../../core/configManager");
var _runtimeManager=require("../../core/runtimeManager");
var _constants=require("../../core/constants");
var _fileutils=require("../../core/systemManager/fileutils");





var _logger=require("../../core/systemManager/logger");








var _prompt=require("../../cli/prompt");

var _loadAppConfigIDfromDir=function _loadAppConfigIDfromDir(dirName,appConfigsDir){
(0,_logger.logDebug)("_loadAppConfigIDfromDir:"+dirName+":"+appConfigsDir,(0,_logger.chalk)().grey);
var filePath=_path.default.join(appConfigsDir,'renative.json');
if((0,_fileutils.fsExistsSync)(filePath)){
try{
var renativeConf=JSON.parse((0,_fileutils.fsReadFileSync)(filePath));
return{dir:dirName,id:renativeConf.id};
}catch(e){
(0,_logger.logError)("File "+filePath+" is MALFORMED:\n"+e);
}
}
return{dir:dirName,id:null};
};

var _askUserAboutConfigs=function _askUserAboutConfigs(c,dir,id,basePath){var _await$inquirer$promp,choice,conf,filePath,fileContents;return _regenerator.default.async(function _askUserAboutConfigs$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('_askUserAboutConfigs');
(0,_logger.logWarning)("AppConfig error - It seems you have a mismatch between appConfig folder name ("+

dir+") and the id defined in renative.json ("+
id+"). They must match.");if(!(

c.program.ci===true)){_context.next=4;break;}throw(
new Error(
'You cannot continue if you set --ci flag. please fix above error first'));case 4:_context.next=6;return _regenerator.default.awrap(


_inquirer.default.prompt({
type:'list',
name:'choice',
message:'You must choose what you want to keep',
choices:[




{
name:"Keep folder name ("+dir+") and rename the ID from renative.json ("+id+" -> "+dir+")",
value:'keepFolder'},

new _inquirer.default.Separator(),
{
name:"I'll do it manually",
value:'manually'}]}));case 6:_await$inquirer$promp=_context.sent;choice=_await$inquirer$promp.choice;if(!(




choice==='manually')){_context.next=10;break;}throw(
new Error('Please do the changes and rerun the command'));case 10:


conf={id:id,dir:dir};

if(choice==='keepID'){
conf.dir=_path.default.join(basePath,id);
(0,_fileutils.fsRenameSync)(_path.default.join(basePath,dir),conf.dir);
}

if(choice==='keepFolder'){
filePath=_path.default.join(basePath,dir,'renative.json');
fileContents=JSON.parse((0,_fileutils.fsReadFileSync)(filePath));
fileContents.id=dir;
conf.id=dir;

(0,_fileutils.writeFileSync)(filePath,fileContents);
}return _context.abrupt("return",

conf);case 14:case"end":return _context.stop();}}},null,null,null,Promise);};



var matchAppConfigID=function matchAppConfigID(c,appConfigID){var _c$paths$project,appConfigsDirs,appConfigsDirNames,acIndex,conf,_conf,dir,id;return _regenerator.default.async(function matchAppConfigID$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('matchAppConfigID',"appId:"+appConfigID);if(

appConfigID){_context2.next=3;break;}return _context2.abrupt("return",false);case 3:_c$paths$project=

c.paths.project,appConfigsDirs=_c$paths$project.appConfigsDirs,appConfigsDirNames=_c$paths$project.appConfigsDirNames;

acIndex=appConfigsDirNames.indexOf(appConfigID);if(!(
acIndex!==-1)){_context2.next=13;break;}
conf=_loadAppConfigIDfromDir(appConfigID,appConfigsDirs[acIndex]);_conf=
conf,dir=_conf.dir,id=_conf.id;if(!(
id!==dir)){_context2.next=12;break;}_context2.next=11;return _regenerator.default.awrap(
_askUserAboutConfigs(
c,
conf.dir,
conf.id,
_path.default.join(appConfigsDirs[acIndex],'..')));case 11:conf=_context2.sent;case 12:return _context2.abrupt("return",




conf.id);case 13:return _context2.abrupt("return",

false);case 14:case"end":return _context2.stop();}}},null,null,null,Promise);};


var _findAndSwitchAppConfigDir=function _findAndSwitchAppConfigDir(c){var appConfigsDirNames,_await$inquirerPrompt,conf;return _regenerator.default.async(function _findAndSwitchAppConfigDir$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('_findAndSwitchAppConfigDir');
appConfigsDirNames=c.paths.project.appConfigsDirNames;if(!
appConfigsDirNames.length){_context3.next=14;break;}if(!(
appConfigsDirNames.length===1)){_context3.next=7;break;}

(0,_logger.logInfo)("Found only one app config available. Will use "+
(0,_logger.chalk)().white(
appConfigsDirNames[0]));


_setAppId(c,appConfigsDirNames[0]);return _context3.abrupt("return",
true);case 7:_context3.next=9;return _regenerator.default.awrap(


(0,_prompt.inquirerPrompt)({
name:'conf',
type:'list',
message:'Which one would you like to pick?',
choices:appConfigsDirNames,
pageSize:50,
logMessage:'ReNative found multiple existing appConfigs'}));case 9:_await$inquirerPrompt=_context3.sent;conf=_await$inquirerPrompt.conf;if(!


conf){_context3.next=14;break;}
_setAppId(c,conf);return _context3.abrupt("return",
true);case 14:return _context3.abrupt("return",



false);case 15:case"end":return _context3.stop();}}},null,null,null,Promise);};


var _setAppId=function _setAppId(c,appId){var _c$files$project,_c$files$project$conf,_c$files$project$conf2;
var currentAppConfigId=(_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;

(0,_logger.logTask)('_setAppId',"appId:"+appId+" runtime.appId:"+c.runtime.appId+" _meta.appId:"+currentAppConfigId);
c.runtime.appId=appId||c.runtime.appId||currentAppConfigId;
c.runtime.appDir=_path.default.join(c.paths.project.builds.dir,c.runtime.appId+"_"+c.platform);
};

var taskRnvAppConfigure=function taskRnvAppConfigure(c){var _c$buildConfig,_c$buildConfig$paths;var appConfigsDirsExt,hasAppConfig,aid,_hasAppConfig;return _regenerator.default.async(function taskRnvAppConfigure$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('taskRnvAppConfigure');

c.paths.project.appConfigsDirNames=(0,_configManager.listAppConfigsFoldersSync)(c,true);
c.paths.project.appConfigsDirNames.forEach(function(dirName){
c.paths.project.appConfigsDirs.push(_path.default.join(c.paths.project.appConfigsDir,dirName));
});

appConfigsDirsExt=(_c$buildConfig=c.buildConfig)==null?void 0:(_c$buildConfig$paths=_c$buildConfig.paths)==null?void 0:_c$buildConfig$paths.appConfigsDirs;
if(appConfigsDirsExt){
appConfigsDirsExt.forEach(function(apePath){
var appConfigsExt=(0,_configManager.listAppConfigsFoldersSync)(c,true,apePath);
appConfigsExt.forEach(function(appExtName){
c.paths.project.appConfigsDirNames.push(appExtName);
c.paths.project.appConfigsDirs.push(_path.default.join(apePath,appExtName));
});
});
}



if(!c.paths.project.appConfigsDirNames.includes(c.runtime.appId)){
c.runtime.appId=null;
}if(!(

c.program.appConfigID===true||!c.program.appConfigID&&!c.runtime.appId)){_context4.next=14;break;}_context4.next=9;return _regenerator.default.awrap(
_findAndSwitchAppConfigDir(c));case 9:hasAppConfig=_context4.sent;if(
hasAppConfig){_context4.next=12;break;}return _context4.abrupt("return",

Promise.reject('No app configs found for this project'));case 12:_context4.next=26;break;case 14:if(!

c.program.appConfigID){_context4.next=26;break;}_context4.next=17;return _regenerator.default.awrap(
matchAppConfigID(c,c.program.appConfigID));case 17:aid=_context4.sent;if(
aid){_context4.next=25;break;}
(0,_logger.logWarning)("Cannot find app config "+(0,_logger.chalk)().white(c.program.appConfigID));_context4.next=22;return _regenerator.default.awrap(
_findAndSwitchAppConfigDir(c));case 22:_hasAppConfig=_context4.sent;if(
_hasAppConfig){_context4.next=25;break;}return _context4.abrupt("return",

Promise.reject('No app configs found for this project'));case 25:


_setAppId(c,aid);case 26:



c.runtime.appConfigDir=c.paths.project.appConfigsDirs[
c.paths.project.appConfigsDirNames.indexOf(c.runtime.appId)];_context4.next=29;return _regenerator.default.awrap(

(0,_runtimeManager.updateRenativeConfigs)(c));case 29:
(0,_logger.logAppInfo)(c);return _context4.abrupt("return",

true);case 31:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.taskRnvAppConfigure=taskRnvAppConfigure;var _default=


{
description:'Configure project with specific appConfig',
fn:taskRnvAppConfigure,
task:_constants.TASK_APP_CONFIGURE,
params:_constants.PARAMS.withBase(_constants.PARAMS.withConfigure()),
platforms:[]};exports.default=_default;
//# sourceMappingURL=task.rnv.app.configure.js.map