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 _util=require("util");
var _inquirer=_interopRequireDefault(require("inquirer"));
var _configParser=require("../core/configManager/configParser");
var _constants=require("../core/constants");
var _fileutils=require("../core/systemManager/fileutils");







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








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

var readdirAsync=(0,_util.promisify)(_fileutils.fsReaddir);


var _loadAppConfigIDfromDir=function _loadAppConfigIDfromDir(dir,appConfigsDir){
(0,_logger.logDebug)("_loadAppConfigIDfromDir:"+dir+":"+appConfigsDir,(0,_logger.chalk)().grey);
var filePath=_path.default.join(appConfigsDir,dir,'renative.json');
if((0,_fileutils.fsExistsSync)(filePath)){
try{
var renativeConf=JSON.parse((0,_fileutils.fsReadFileSync)(filePath));
return{dir:dir,id:renativeConf.id};
}catch(e){
(0,_logger.logError)("File "+filePath+" is MALFORMED:\n"+e);
}
}
return{dir:dir,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 ID from renative.json ("+id+") and rename the folder ("+dir+" -> "+id+")",
value:'keepID'},

{
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$buildConfig,_c$buildConfig$paths,_c$paths$project;var appConfigsDirs,_loop,i,_ret;return _regenerator.default.async(function matchAppConfigID$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('matchAppConfigID',"appId:"+appConfigID);if(

appConfigID){_context4.next=3;break;}return _context4.abrupt("return",false);case 3:

appConfigsDirs=((_c$buildConfig=c.buildConfig)==null?void 0:(_c$buildConfig$paths=_c$buildConfig.paths)==null?void 0:_c$buildConfig$paths.appConfigsDirs)||[(_c$paths$project=
c.paths.project)==null?void 0:_c$paths$project.appConfigsDir];_loop=function _loop(



i){var appConfigsDir,appConfigDirContents,appConfigs,ids,dirs,confId,foundConfig;return _regenerator.default.async(function _loop$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
appConfigsDir=appConfigsDirs[i];if(!
(0,_fileutils.fsExistsSync)(appConfigsDir)){_context3.next=17;break;}_context3.t0=_regenerator.default;_context3.next=5;return _regenerator.default.awrap(
readdirAsync(
appConfigsDir));case 5:_context3.t1=_context3.sent.
filter(function(folder){return(0,_fileutils.fsStatSync)(_path.default.join(appConfigsDir,folder)).isDirectory();});_context3.next=8;return _context3.t0.awrap.call(_context3.t0,_context3.t1);case 8:appConfigDirContents=_context3.sent;

appConfigs=appConfigDirContents.
map(function(dir){return _loadAppConfigIDfromDir(dir,appConfigsDir);}).
filter(function(conf){return conf.id!==null;});

ids=[];
dirs=[];_context3.next=14;return _regenerator.default.awrap(

Promise.all(
appConfigs.map(function _callee(_conf){var conf,_conf2,dir;return _regenerator.default.async(function _callee$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
conf=_conf;
confId=conf.id;_conf2=
conf,dir=_conf2.dir;if(!(

confId!==dir)){_context2.next=7;break;}_context2.next=6;return _regenerator.default.awrap(
_askUserAboutConfigs(
c,
conf.dir,
conf.id,
appConfigsDir));case 6:conf=_context2.sent;case 7:


confId=conf.id;if(!
ids.includes(confId)){_context2.next=10;break;}throw(
new Error("AppConfig error - You have 2 duplicate app configs with ID "+

confId+". Keep in mind that ID is case insensitive.\nPlease edit one of them in /appConfigs/<folder>/renative.json"));case 10:




ids.push(confId);if(!
dirs.includes(dir)){_context2.next=13;break;}throw(
new Error("AppConfig error - You have 2 duplicate app config folders named "+

dir+". Keep in mind that folder names are case insensitive.\nPlease rename one /appConfigs/<folder>"));case 13:




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



foundConfig=appConfigs.filter(
function(cfg){return cfg.id===appConfigID||cfg.dir===appConfigID;});if(!

foundConfig.length){_context3.next=17;break;}return _context3.abrupt("return",{v:foundConfig[0].id});case 17:case"end":return _context3.stop();}}},null,null,null,Promise);};i=0;case 6:if(!(i<appConfigsDirs.length)){_context4.next=15;break;}_context4.next=9;return _regenerator.default.awrap(_loop(i));case 9:_ret=_context4.sent;if(!(typeof _ret==="object")){_context4.next=12;break;}return _context4.abrupt("return",_ret.v);case 12:i++;_context4.next=6;break;case 15:return _context4.abrupt("return",


false);case 16:case"end":return _context4.stop();}}},null,null,null,Promise);};


var _findAndSwitchAppConfigDir=function _findAndSwitchAppConfigDir(c){var appConfigsDirNames,_await$inquirerPrompt,conf;return _regenerator.default.async(function _findAndSwitchAppConfigDir$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('_findAndSwitchAppConfigDir');
appConfigsDirNames=c.paths.project.appConfigsDirNames;if(!
appConfigsDirNames.length){_context5.next=14;break;}if(!(
appConfigsDirNames.length===1)){_context5.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 _context5.abrupt("return",
true);case 7:_context5.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=_context5.sent;conf=_await$inquirerPrompt.conf;if(!


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



false);case 15:case"end":return _context5.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 hasAppConfig,aid,_hasAppConfig;return _regenerator.default.async(function taskRnvAppConfigure$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
(0,_logger.logTask)('taskRnvAppConfigure');

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


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

Promise.reject('No app configs found for this project'));case 9:_context6.next=23;break;case 11:if(!

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

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


_setAppId(c,aid);case 23:_context6.next=25;return _regenerator.default.awrap(


(0,_configParser.parseRenativeConfigs)(c));case 25:
(0,_logger.logAppInfo)(c);return _context6.abrupt("return",

true);case 27:case"end":return _context6.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