var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.injectPlatformDependencies=exports.checkRequiredPackage=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _semver=_interopRequireDefault(require("semver"));
var _deepmerge=_interopRequireDefault(require("deepmerge"));

var _exec=require("../systemManager/exec");
var _npmUtils=require("../systemManager/npmUtils");
var _logger=require("../systemManager/logger");
var _prompt=require("../../cli/prompt");
var _engineManager=require("../engineManager");
var _index=require("./index");
var _pluginManager=require("../pluginManager");


var injectProjectDependency=function injectProjectDependency(c,
dependency,
version,
type){var skipInstall,currentPackage,existingPath,_args=arguments;return _regenerator.default.async(function injectProjectDependency$(_context){while(1){switch(_context.prev=_context.next){case 0:
skipInstall=_args.length>4&&_args[4]!==undefined?_args[4]:false;

(0,_logger.logTask)('injectProjectDependency');

currentPackage=c.files.project.package;
existingPath=c.paths.project.package;
if(!currentPackage[type])currentPackage[type]={};
currentPackage[type][dependency]=version;
(0,_index.writeRenativeConfigFile)(c,existingPath,currentPackage);if(
skipInstall){_context.next=12;break;}_context.next=10;return _regenerator.default.awrap(
(0,_npmUtils.installPackageDependencies)(c));case 10:_context.next=12;return _regenerator.default.awrap(
(0,_pluginManager.overrideTemplatePlugins)(c));case 12:return _context.abrupt("return",

true);case 13:case"end":return _context.stop();}}},null,null,null,Promise);};


var checkRequiredPackage=function checkRequiredPackage(c,pkg){var _projectConfig$packag;var version,type,skipAsking,skipInstall,skipVersionCheck,projectConfig,confirm,resp,latestVersion,currentVersion,_latestVersion,updateAvailable,_confirm,_resp,_args2=arguments;return _regenerator.default.async(function checkRequiredPackage$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:version=_args2.length>2&&_args2[2]!==undefined?_args2[2]:false;type=_args2.length>3?_args2[3]:undefined;skipAsking=_args2.length>4&&_args2[4]!==undefined?_args2[4]:false;
skipInstall=_args2.length>5&&_args2[5]!==undefined?_args2[5]:false;skipVersionCheck=_args2.length>6&&_args2[6]!==undefined?_args2[6]:false;
(0,_logger.logDebug)('checkRequiredPackage');if(
pkg){_context2.next=8;break;}return _context2.abrupt("return",false);case 8:
projectConfig=c.files.project;if((_projectConfig$packag=

projectConfig.package[type])!=null&&_projectConfig$packag[pkg]){_context2.next=30;break;}

confirm=skipAsking;if(
confirm){_context2.next=16;break;}_context2.next=14;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'confirm',
message:"You do not have "+pkg+" installed. Do you want to add it now?"}));case 14:resp=_context2.sent;


confirm=resp.confirm;case 16:if(!


confirm){_context2.next=28;break;}
latestVersion='latest';if(!(
!version&&!skipVersionCheck)){_context2.next=27;break;}_context2.prev=19;_context2.next=22;return _regenerator.default.awrap(

(0,_exec.executeAsync)("npm show "+
pkg+" version"));case 22:latestVersion=_context2.sent;_context2.next=27;break;case 25:_context2.prev=25;_context2.t0=_context2["catch"](19);case 27:return _context2.abrupt("return",




injectProjectDependency(c,
pkg,
version||latestVersion,
type,
skipInstall));case 28:_context2.next=53;break;case 30:if(

version){_context2.next=53;break;}

currentVersion=projectConfig.package[type][pkg];
_latestVersion=false;_context2.prev=33;_context2.next=36;return _regenerator.default.awrap(

(0,_exec.executeAsync)("npm show "+pkg+" version"));case 36:_latestVersion=_context2.sent;_context2.next=41;break;case 39:_context2.prev=39;_context2.t1=_context2["catch"](33);case 41:if(!


_latestVersion){_context2.next=53;break;}
updateAvailable=false;

try{

updateAvailable=_semver.default.lt(currentVersion,_latestVersion);

}catch(e){}if(!

updateAvailable){_context2.next=53;break;}
_confirm=skipAsking;if(
_confirm){_context2.next=51;break;}_context2.next=49;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'confirm',
message:"Seems like "+pkg+"@"+
currentVersion+" is installed while there is a newer version, "+

pkg+"@"+
_latestVersion+". Do you want to upgrade?"}));case 49:_resp=_context2.sent;


_confirm=_resp.confirm;case 51:if(!


_confirm){_context2.next=53;break;}return _context2.abrupt("return",
injectProjectDependency(c,
pkg,
_latestVersion,
type,
skipInstall));case 53:return _context2.abrupt("return",





false);case 54:case"end":return _context2.stop();}}},null,null,[[19,25],[33,39]],Promise);};exports.checkRequiredPackage=checkRequiredPackage;



var injectPlatformDependencies=function injectPlatformDependencies(c){var _engine$config,_engine$config2,_engine$config2$platf;var platform,engine,npmDepsBase,npmDepsExt,npmDeps,promises,installed;return _regenerator.default.async(function injectPlatformDependencies$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('injectPlatformDependencies');
platform=c.platform;
engine=(0,_engineManager.getEngineRunnerByPlatform)(c,platform);
npmDepsBase=(engine==null?void 0:(_engine$config=engine.config)==null?void 0:_engine$config.npm)||{};
npmDepsExt=(engine==null?void 0:(_engine$config2=engine.config)==null?void 0:(_engine$config2$platf=_engine$config2.platforms[platform])==null?void 0:_engine$config2$platf.npm)||{};

npmDeps=(0,_deepmerge.default)(npmDepsBase,npmDepsExt);if(!

npmDeps){_context3.next=17;break;}
promises=Object.keys(npmDeps).reduce(function(acc,type){

Object.keys(npmDeps[type]).forEach(function(dep){

acc.push(
checkRequiredPackage(c,
dep,
npmDeps[type][dep],
type,
true,
true));

});
return acc;
},[]);_context3.next=10;return _regenerator.default.awrap(

Promise.all(promises));case 10:installed=_context3.sent;if(!

installed.some(function(i){return i===true;})){_context3.next=17;break;}

(0,_logger.logInfo)("Found extra npm dependencies required by "+
(0,_logger.chalk)().white(engine.config.id)+" engine. ADDING...DONE");_context3.next=15;return _regenerator.default.awrap(

(0,_npmUtils.installPackageDependencies)(c));case 15:_context3.next=17;return _regenerator.default.awrap(
(0,_pluginManager.overrideTemplatePlugins)(c));case 17:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.injectPlatformDependencies=injectPlatformDependencies;
//# sourceMappingURL=packageParser.js.map