var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.getLocalRenativePlugin=exports.parsePlugins=exports.getMergedPlugin=exports.configurePlugins=void 0;var _chalk=_interopRequireDefault(require("chalk"));
var _fileutils=require("../systemTools/fileutils");
var _common=require("../common");
var _prompt=require("../systemTools/prompt");
var _configParser=require("../configTools/configParser");

var getMergedPlugin=function getMergedPlugin(c,key,plugins){var noMerge=arguments.length>3&&arguments[3]!==undefined?arguments[3]:false;
var plugin=plugins[key];
var origPlugin=c.files.rnv.pluginTemplates.config.plugins[key];
if(typeof plugin==='string'||plugin instanceof String){
if(plugin==='source:rnv'){
return origPlugin;
}

(0,_common.logWarning)("Plugin "+key+" is not recognized RNV plugin");
return null;
}


if(origPlugin){
var mergedPlugin=(0,_fileutils.mergeObjects)(c,origPlugin,plugin);
return mergedPlugin;
}

return plugin;
};exports.getMergedPlugin=getMergedPlugin;


var configurePlugins=function configurePlugins(c){return new Promise(function(resolve,reject){
(0,_common.logTask)('configurePlugins');

if(!c.files.project.package.dependencies){
c.files.project.package.dependencies={};
}

var hasPackageChanged=false;

for(var k in c.buildConfig.plugins){var
dependencies=c.files.project.package.dependencies;var
devDependencies=c.files.project.package.devDependencies;
var plugin=getMergedPlugin(c,k,c.buildConfig.plugins);

if(!plugin){
(0,_common.logWarning)("Plugin with name "+
_chalk.default.white(k)+" does not exists in ReNative source:rnv scope. you need to define it manually here: "+
_chalk.default.white(c.paths.project.builds.config));
}else if(dependencies&&dependencies[k]){
if(plugin['no-active']!==true&&plugin['no-npm']!==true&&dependencies[k]!==plugin.version){
if(k==='renative'&&c.runtime.isWrapper){
(0,_common.logWarning)('You\'re in ReNative wrapper mode. plugin renative will stay as local dep!');
}else{
(0,_common.logWarning)("Version mismatch of dependency "+
_chalk.default.white(k)+" between:\n  "+
_chalk.default.white(c.paths.project.package)+": v("+_chalk.default.red(dependencies[k])+") and\n  "+
_chalk.default.white(c.paths.project.builds.config)+": v("+_chalk.default.green(plugin.version)+").\n  package.json will be overriden");



hasPackageChanged=true;
dependencies[k]=plugin.version;
}
}
}else if(devDependencies&&devDependencies[k]){
if(plugin['no-active']!==true&&plugin['no-npm']!==true&&devDependencies[k]!==plugin.version){
(0,_common.logWarning)("Version mismatch of devDependency "+
_chalk.default.white(k)+" between package.json: v("+_chalk.default.red(
devDependencies[k])+") and plugins.json: v("+
_chalk.default.red(plugin.version)+"). package.json will be overriden");

hasPackageChanged=true;
devDependencies[k]=plugin.version;
}
}else if(plugin['no-active']!==true&&plugin['no-npm']!==true){

(0,_common.logWarning)("Missing dependency "+
_chalk.default.white(k)+" v("+_chalk.default.red(
plugin.version)+") in package.json. package.json will be overriden");



hasPackageChanged=true;
dependencies[k]=plugin.version;
}

if(plugin&&plugin.npm){
for(var npmKey in plugin.npm){
var npmDep=plugin.npm[npmKey];
if(dependencies[npmKey]!==npmDep){
(0,_common.logWarning)("Plugin "+_chalk.default.white(k)+" requires npm dependency "+_chalk.default.white(npmKey)+" .Adding missing npm dependency to you package.json");
dependencies[npmKey]=npmDep;
hasPackageChanged=true;
}
}
}
}

(0,_common.logTask)("configurePlugins:"+hasPackageChanged,_chalk.default.grey);
(0,_configParser.versionCheck)(c).
then(function(){
if(hasPackageChanged){
(0,_fileutils.writeObjectSync)(c.paths.project.package,c.files.project.package);
c._requiresNpmInstall=true;
}
resolve();
}).catch(function(e){return reject(e);});
});};exports.configurePlugins=configurePlugins;

var parsePlugins=function parsePlugins(c,platform,pluginCallback){
(0,_common.logTask)("parsePlugins:"+platform);

if(c.buildConfig&&c.buildConfig){
var includedPlugins=(0,_common.getConfigProp)(c,platform,'includedPlugins',[]);
var excludedPlugins=(0,_common.getConfigProp)(c,platform,'excludedPlugins',[]);
if(includedPlugins){var
plugins=c.buildConfig.plugins;
if(plugins){
Object.keys(plugins).forEach(function(key){
if((includedPlugins.includes('*')||includedPlugins.includes(key))&&!excludedPlugins.includes(key)){
var plugin=getMergedPlugin(c,key,plugins);
if(plugin){
var pluginPlat=plugin[platform];
if(pluginPlat){
if(plugin['no-active']!==true&&plugin.enabled!==false&&pluginPlat.enabled!==false){
if(pluginCallback)pluginCallback(plugin,pluginPlat,key);
}else{
(0,_common.logWarning)("Plugin "+key+" is marked disabled. skipping.");
}
}
}
}
});
}else{
(0,_common.logError)("You have no plugins defined in "+_chalk.default.white(c.paths.project.builds.config));
}
}else{
(0,_common.logWarning)("You haven't included any "+_chalk.default.white('{ common: { includedPlugins: [] }}')+" in your "+_chalk.default.white(c.paths.appConfig.config)+". Your app might not work correctly");
}
}
};exports.parsePlugins=parsePlugins;

var getLocalRenativePlugin=function getLocalRenativePlugin(){return{
version:'file:./packages/renative',
webpack:{
modulePaths:[],
moduleAliases:{
renative:{
projectPath:'packages/renative'}}}};};exports.getLocalRenativePlugin=getLocalRenativePlugin;var _default=







{getMergedPlugin:getMergedPlugin,parsePlugins:parsePlugins,getLocalRenativePlugin:getLocalRenativePlugin};exports.default=_default;