var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.cleanNodeModules=exports.jetifyIfRequired=exports.installPackageDependencies=exports.checkIfProjectAndNodeModulesExists=exports.listAndSelectNpmVersion=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));
var _path=_interopRequireDefault(require("path"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _exec=require("./exec");
var _fileutils=require("./fileutils");
var _logger=require("./logger");
var _constants=require("../constants");
var _resolve=require("../resolve");


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

var listAndSelectNpmVersion=function listAndSelectNpmVersion(c,npmPackage,rnvTemplates){var templateVersionsStr,versionArr,templateTagsStr,tagArr,rnvVersion,validVersions,recommendedVersion,_await$inquirer$promp,inputTemplateVersion;return _regenerator.default.async(function listAndSelectNpmVersion$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.next=2;return _regenerator.default.awrap(
(0,_exec.executeAsync)(
c,"npm view "+
npmPackage+" versions"));case 2:templateVersionsStr=_context.sent;

versionArr=templateVersionsStr.
replace(/\r?\n|\r|\s|'|\[|\]/g,'').
split(',');_context.next=6;return _regenerator.default.awrap(

(0,_exec.executeAsync)(
c,"npm dist-tag ls "+
npmPackage));case 6:templateTagsStr=_context.sent;

tagArr=[];
templateTagsStr.split('\n').forEach(function(tString){
var tArr=tString.split(': ');
tagArr.push({
name:tArr[0],
version:tArr[1]});

});

rnvVersion=c.rnvVersion;
validVersions=versionArr.map(function(v){return{name:v,value:v};});


validVersions.forEach(function(item){
var matchStr='';
var matchArr=[];
tagArr.forEach(function(tag){
if(tag.version===item.value){
matchArr.push(tag.name);
}
});
if(matchArr.length){
matchStr=" (HEAD: "+matchArr.join(', ')+")";
item.name=""+item.value+matchStr;
}
if((rnvTemplates==null?void 0:rnvTemplates.includes)&&rnvTemplates.includes(npmPackage)){
if(item.value===rnvVersion){
item.name=item.name+" <= RECOMMENDED";
recommendedVersion=item;
}
}
});
if(!recommendedVersion)recommendedVersion=validVersions[0];


validVersions.sort(function(a,b){
if(a.name>b.name)return 1;

return b.name>a.name?-1:0;
}).reverse();

validVersions.unshift(validVersions.splice(validVersions.indexOf(recommendedVersion),1)[0]);_context.next=17;return _regenerator.default.awrap(


_inquirer.default.prompt({
name:'inputTemplateVersion',
type:'list',
message:"What "+npmPackage+" version to use?",
default:recommendedVersion.name,
choices:validVersions}));case 17:_await$inquirer$promp=_context.sent;inputTemplateVersion=_await$inquirer$promp.inputTemplateVersion;return _context.abrupt("return",


inputTemplateVersion);case 20:case"end":return _context.stop();}}},null,null,null,Promise);};exports.listAndSelectNpmVersion=listAndSelectNpmVersion;


var checkIfProjectAndNodeModulesExists=function checkIfProjectAndNodeModulesExists(c){return _regenerator.default.async(function checkIfProjectAndNodeModulesExists$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('checkIfProjectAndNodeModulesExists');if(!(

c.paths.project.configExists&&!(0,_fileutils.fsExistsSync)(c.paths.project.nodeModulesDir))){_context2.next=6;break;}
c._requiresNpmInstall=false;
(0,_logger.logInfo)(
'node_modules folder is missing. INSTALLING...');_context2.next=6;return _regenerator.default.awrap(

installPackageDependencies(c));case 6:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.checkIfProjectAndNodeModulesExists=checkIfProjectAndNodeModulesExists;



var installPackageDependencies=function installPackageDependencies(c){var _c$buildConfig,_c$buildConfig$tasks,_c$buildConfig$tasks$;var failOnError,customScript,isYarnInstalled,yarnLockPath,npmLockPath,command,_await$inquirerPrompt,packageManager,_c$files$project$conf,_c$files$project$conf2,plats,_c$files$project$conf3,_args3=arguments;return _regenerator.default.async(function installPackageDependencies$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:failOnError=_args3.length>1&&_args3[1]!==undefined?_args3[1]:false;
customScript=(_c$buildConfig=c.buildConfig)==null?void 0:(_c$buildConfig$tasks=_c$buildConfig.tasks)==null?void 0:(_c$buildConfig$tasks$=_c$buildConfig$tasks.install)==null?void 0:_c$buildConfig$tasks$.script;if(!

customScript){_context3.next=8;break;}
(0,_logger.logTask)('installPackageDependencies');
(0,_logger.logInfo)("Found custom task for install: "+customScript+".");_context3.next=7;return _regenerator.default.awrap(
(0,_exec.executeAsync)(customScript));case 7:return _context3.abrupt("return",
true);case 8:


isYarnInstalled=(0,_exec.commandExistsSync)('yarn')||(0,_resolve.doResolve)('yarn',false);
yarnLockPath=_path.default.join(c.paths.project.dir,'yarn.lock');
npmLockPath=_path.default.join(c.paths.project.dir,'package-lock.json');
command='npm install';if(!
(0,_fileutils.fsExistsSync)(yarnLockPath)){_context3.next=16;break;}
command='yarn';_context3.next=26;break;case 16:if(!
(0,_fileutils.fsExistsSync)(npmLockPath)){_context3.next=20;break;}
command='npm install';_context3.next=26;break;case 20:if(!
isYarnInstalled){_context3.next=26;break;}_context3.next=23;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'list',
name:'packageManager',
message:'What package manager would you like to use?',
choices:['yarn','npm'],
default:'npm'}));case 23:_await$inquirerPrompt=_context3.sent;packageManager=_await$inquirerPrompt.packageManager;

if(packageManager==='yarn')command='yarn';case 26:

(0,_logger.logTask)('installPackageDependencies',"packageManager:("+command+")");_context3.prev=27;_context3.next=30;return _regenerator.default.awrap(


(0,_exec.executeAsync)(command));case 30:_context3.next=32;return _regenerator.default.awrap(
(0,_fileutils.invalidatePodsChecksum)(c));case 32:_context3.next=51;break;case 34:_context3.prev=34;_context3.t0=_context3["catch"](27);if(!

failOnError){_context3.next=39;break;}
(0,_logger.logError)(_context3.t0);return _context3.abrupt("return",
false);case 39:

(0,_logger.logWarning)(_context3.t0+"\n Seems like your node_modules is corrupted by other libs. ReNative will try to fix it for you");_context3.prev=40;_context3.next=43;return _regenerator.default.awrap(



cleanNodeModules(c));case 43:_context3.next=45;return _regenerator.default.awrap(
installPackageDependencies(c,true));case 45:_context3.next=51;break;case 47:_context3.prev=47;_context3.t1=_context3["catch"](40);

(0,_logger.logError)(_context3.t1);return _context3.abrupt("return",
false);case 51:_context3.prev=51;



plats=(_c$files$project$conf=c.files.project.config)==null?void 0:(_c$files$project$conf2=_c$files$project$conf.defaults)==null?void 0:_c$files$project$conf2.supportedPlatforms;
if(
Array.isArray(plats)&&(plats.includes(_constants.ANDROID)||
plats.includes(_constants.ANDROID_TV)||plats.includes(_constants.ANDROID_WEAR)))
{
if(!c.files.project.configLocal){
c.files.project.configLocal={};
}
if(!((_c$files$project$conf3=c.files.project.configLocal)==null?void 0:_c$files$project$conf3._meta)){
c.files.project.configLocal._meta={};
}
c.files.project.configLocal._meta.requiresJetify=true;
(0,_fileutils.writeFileSync)(c.paths.project.configLocal,c.files.project.configLocal);
}return _context3.abrupt("return",
true);case 57:_context3.prev=57;_context3.t2=_context3["catch"](51);

(0,_logger.logError)(_context3.t2);return _context3.abrupt("return",
false);case 61:case"end":return _context3.stop();}}},null,null,[[27,34],[40,47],[51,57]],Promise);};exports.installPackageDependencies=installPackageDependencies;



var jetifyIfRequired=function jetifyIfRequired(c){var _c$files$project$conf4,_c$files$project$conf5;return _regenerator.default.async(function jetifyIfRequired$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('jetifyIfRequired');if(!((_c$files$project$conf4=
c.files.project.configLocal)==null?void 0:(_c$files$project$conf5=_c$files$project$conf4._meta)==null?void 0:_c$files$project$conf5.requiresJetify)){_context4.next=5;break;}if(!
(0,_resolve.doResolve)('jetifier')){_context4.next=5;break;}_context4.next=5;return _regenerator.default.awrap(
(0,_exec.executeAsync)('npx jetify'));case 5:return _context4.abrupt("return",


true);case 6:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.jetifyIfRequired=jetifyIfRequired;


var cleanNodeModules=function cleanNodeModules(){return new Promise(function(resolve,reject){
(0,_logger.logTask)('cleanNodeModules');
var dirs=[
'react-native-safe-area-view/.git',
'@react-navigation/native/node_modules/react-native-safe-area-view/.git',
'react-navigation/node_modules/react-native-safe-area-view/.git',
'react-native-safe-area-view/.git',
'@react-navigation/native/node_modules/react-native-safe-area-view/.git',
'react-navigation/node_modules/react-native-safe-area-view/.git'].
reduce(function(acc,dir){var _dir$match=
dir.match(/([^/]+)\/(.*)/),_dir$match2=(0,_slicedToArray2.default)(_dir$match,3),_all=_dir$match2[0],aPackage=_dir$match2[1],aPath=_dir$match2[2];
(0,_logger.logDebug)("Cleaning: "+_all);
var resolved=(0,_resolve.doResolve)(aPackage,false);
if(resolved){
acc.push(resolved+"/"+aPath);
}
return acc;
},[]);
(0,_fileutils.removeDirs)(dirs).
then(function(){return resolve();}).
catch(function(e){return reject(e);});








});};exports.cleanNodeModules=cleanNodeModules;
//# sourceMappingURL=npmUtils.js.map