var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.cleanNodeModules=exports.jetifyIfRequired=exports.installPackageDependencies=exports.checkIfProjectAndNodeModulesExists=exports.listAndSelectNpmVersion=exports.areNodeModulesInstalled=exports.checkAndCreateProjectPackage=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 checkAndCreateProjectPackage=function checkAndCreateProjectPackage(c){var _c$files$project$conf,_c$files$project$conf2,_c$files$project$conf3,_c$files$project$conf4,_c$files$project$conf5,packageName,version,templateName,rnvVersion,pkgJson,pkgJsonStringClean;return _regenerator.default.async(function checkAndCreateProjectPackage$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('checkAndCreateProjectPackage');

if(!(0,_fileutils.fsExistsSync)(c.paths.project.package)){
(0,_logger.logInfo)("Your "+
c.paths.project.package+" is missing. CREATING...DONE");


packageName=c.files.project.config.projectName||
c.paths.project.dir.split('/').pop();
version=((_c$files$project$conf=c.files.project.config.defaults)==null?void 0:(_c$files$project$conf2=_c$files$project$conf.package)==null?void 0:_c$files$project$conf2.version)||'0.1.0';
templateName=(_c$files$project$conf3=c.files.project.config)==null?void 0:_c$files$project$conf3.currentTemplate;
if(!templateName){
(0,_logger.logWarning)('You are missing currentTemplate in your renative.json');
}
rnvVersion=c.files.rnv.package.version;

pkgJson={};
pkgJson.name=packageName;
pkgJson.version=version;
pkgJson.dependencies={
renative:rnvVersion};

pkgJson.devDependencies={
rnv:rnvVersion};

pkgJson.devDependencies[templateName]=(_c$files$project$conf4=c.files.project.config)==null?void 0:(_c$files$project$conf5=_c$files$project$conf4.templates[templateName])==null?void 0:_c$files$project$conf5.version;
pkgJsonStringClean=JSON.stringify(pkgJson,null,2);
(0,_fileutils.fsWriteFileSync)(c.paths.project.package,pkgJsonStringClean);
}

(0,_fileutils.loadFile)(c.files.project,c.paths.project,'package');return _context.abrupt("return",

true);case 4:case"end":return _context.stop();}}},null,null,null,Promise);};exports.checkAndCreateProjectPackage=checkAndCreateProjectPackage;


var areNodeModulesInstalled=function areNodeModulesInstalled(){return!!(0,_resolve.doResolve)('resolve',false);};exports.areNodeModulesInstalled=areNodeModulesInstalled;

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$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_context2.next=2;return _regenerator.default.awrap(
(0,_exec.executeAsync)(
c,"npm view "+
npmPackage+" versions"));case 2:templateVersionsStr=_context2.sent;

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

(0,_exec.executeAsync)(
c,"npm dist-tag ls "+
npmPackage));case 6:templateTagsStr=_context2.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&&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]);_context2.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=_context2.sent;inputTemplateVersion=_await$inquirer$promp.inputTemplateVersion;return _context2.abrupt("return",


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


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

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

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



var _getInstallScript=function _getInstallScript(c){var _c$buildConfig;
var tasks=(_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.tasks;
if(!tasks)return null;
if(Array.isArray(tasks)){
for(var i=0;i<tasks.length;i++){
if(tasks[i].name==='install'){
return tasks[i].script;
}
}
}else{var _tasks$install;
return(_tasks$install=tasks.install)==null?void 0:_tasks$install.script;
}
};

var installPackageDependencies=function installPackageDependencies(c){var failOnError,customScript,isYarnInstalled,yarnLockPath,npmLockPath,command,yarnLockExists,packageLockExists,_await$inquirerPrompt,packageManager,_c$files$project$conf6,_c$files$project$conf7,plats,_c$files$project$conf8,_args4=arguments;return _regenerator.default.async(function installPackageDependencies$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:failOnError=_args4.length>1&&_args4[1]!==undefined?_args4[1]:false;
c.runtime.forceBuildHookRebuild=true;
customScript=_getInstallScript(c);if(!

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


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';


yarnLockExists=(0,_fileutils.fsExistsSync)(yarnLockPath);
packageLockExists=(0,_fileutils.fsExistsSync)(npmLockPath);if(!(

yarnLockExists||packageLockExists)){_context4.next=21;break;}if(!(

yarnLockExists&&!isYarnInstalled)){_context4.next=18;break;}throw new Error('You have a yarn.lock file but you don\'t have yarn installed. Install it or delete yarn.lock');case 18:
command=yarnLockExists?'yarn':'npm install';_context4.next=36;break;case 21:if(!
c.program.packageManager){_context4.next=31;break;}if(!

['yarn','npm'].includes(c.program.packageManager)){_context4.next=28;break;}
command=c.program.packageManager==='yarn'?'yarn':'npm install';if(!(
command==='yarn'&&!isYarnInstalled)){_context4.next=26;break;}throw new Error('You specified yarn as packageManager but it\'s not installed');case 26:_context4.next=29;break;case 28:throw(

new Error("Unsupported package manager "+
c.program.packageManager+". Only yarn and npm are supported at the moment."));case 29:_context4.next=36;break;case 31:_context4.next=33;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 33:_await$inquirerPrompt=_context4.sent;packageManager=_await$inquirerPrompt.packageManager;

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


(0,_logger.logTask)('installPackageDependencies',"packageManager:("+command+")");_context4.prev=37;_context4.next=40;return _regenerator.default.awrap(


(0,_exec.executeAsync)(command));case 40:_context4.next=42;return _regenerator.default.awrap(
(0,_fileutils.invalidatePodsChecksum)(c));case 42:_context4.next=61;break;case 44:_context4.prev=44;_context4.t0=_context4["catch"](37);if(!

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

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



cleanNodeModules(c));case 53:_context4.next=55;return _regenerator.default.awrap(
installPackageDependencies(c,true));case 55:_context4.next=61;break;case 57:_context4.prev=57;_context4.t1=_context4["catch"](50);

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



plats=(_c$files$project$conf6=c.files.project.config)==null?void 0:(_c$files$project$conf7=_c$files$project$conf6.defaults)==null?void 0:_c$files$project$conf7.supportedPlatforms;
if(
Array.isArray(plats)&&(plats.includes(_constants.ANDROID)||
plats.includes(_constants.ANDROID_TV)||
plats.includes(_constants.FIRE_TV)||
plats.includes(_constants.ANDROID_WEAR)))
{
if(!c.files.project.configLocal){
c.files.project.configLocal={};
}
if(!((_c$files$project$conf8=c.files.project.configLocal)!=null&&_c$files$project$conf8._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 _context4.abrupt("return",
true);case 67:_context4.prev=67;_context4.t2=_context4["catch"](61);

(0,_logger.logError)(_context4.t2);return _context4.abrupt("return",
false);case 71:case"end":return _context4.stop();}}},null,null,[[37,44],[50,57],[61,67]],Promise);};exports.installPackageDependencies=installPackageDependencies;



var jetifyIfRequired=function jetifyIfRequired(c){var _c$files$project$conf9,_c$files$project$conf10;return _regenerator.default.async(function jetifyIfRequired$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('jetifyIfRequired');if(!((_c$files$project$conf9=
c.files.project.configLocal)!=null&&(_c$files$project$conf10=_c$files$project$conf9._meta)!=null&&_c$files$project$conf10.requiresJetify)){_context5.next=7;break;}if(!
(0,_resolve.doResolve)('jetifier')){_context5.next=7;break;}_context5.next=5;return _regenerator.default.awrap(
(0,_exec.executeAsync)('npx jetify'));case 5:
c.files.project.configLocal._meta.requiresJetify=false;
(0,_fileutils.writeFileSync)(c.paths.project.configLocal,c.files.project.configLocal);case 7:return _context5.abrupt("return",


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