var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.rnvLink=exports.rnvSwitch=exports.rnvConfigure=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));

var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _common=require("../common");






var _constants=require("../constants");
















var _apple=require("../platformTools/apple");
var _android=require("../platformTools/android");
var _tizen=require("../platformTools/tizen");
var _webos=require("../platformTools/webos");
var _electron=require("../platformTools/electron");
var _firefox=require("../platformTools/firefox");
var _web=require("../platformTools/web");
var _fileutils=require("../systemTools/fileutils");
var _cli=_interopRequireDefault(require("../cli"));
var _projectParser=require("./projectParser");
var _configParser=require("../configTools/configParser");
var _config=_interopRequireDefault(require("../config"));

var rnvConfigure=function rnvConfigure(c){var p,ptDirs,i;return _regenerator.default.async(function rnvConfigure$(_context){while(1){switch(_context.prev=_context.next){case 0:
p=c.program.platform||'all';
(0,_common.logTask)("rnvConfigure:"+p);if(!(


p!=='all')){_context.next=5;break;}_context.next=5;return _regenerator.default.awrap(_config.default.injectPlatformDependencies(p));case 5:_context.next=7;return _regenerator.default.awrap(

_checkAndCreatePlatforms(c,c.program.platform));case 7:_context.next=9;return _regenerator.default.awrap(
(0,_projectParser.copyRuntimeAssets)(c));case 9:_context.next=11;return _regenerator.default.awrap(
(0,_projectParser.copySharedPlatforms)(c));case 11:_context.next=13;return _regenerator.default.awrap(
(0,_configParser.generateRuntimeConfig)(c));case 13:
ptDirs=c.paths.rnv.pluginTemplates.dirs;
i=0;case 15:if(!(i<ptDirs.length)){_context.next=21;break;}_context.next=18;return _regenerator.default.awrap(
overridePlugins(c,ptDirs[i]));case 18:i++;_context.next=15;break;case 21:_context.next=23;return _regenerator.default.awrap(


overridePlugins(c,c.paths.project.projectConfig.pluginsDir));case 23:if(!
_isOK(c,p,[_constants.ANDROID])){_context.next=26;break;}_context.next=26;return _regenerator.default.awrap((0,_android.configureGradleProject)(c,_constants.ANDROID));case 26:if(!
_isOK(c,p,[_constants.ANDROID_TV])){_context.next=29;break;}_context.next=29;return _regenerator.default.awrap((0,_android.configureGradleProject)(c,_constants.ANDROID_TV));case 29:if(!
_isOK(c,p,[_constants.ANDROID_WEAR])){_context.next=32;break;}_context.next=32;return _regenerator.default.awrap((0,_android.configureGradleProject)(c,_constants.ANDROID_WEAR));case 32:if(!
_isOK(c,p,[_constants.TIZEN])){_context.next=35;break;}_context.next=35;return _regenerator.default.awrap((0,_tizen.configureTizenGlobal)(c,_constants.TIZEN));case 35:if(!
_isOK(c,p,[_constants.TIZEN])){_context.next=38;break;}_context.next=38;return _regenerator.default.awrap((0,_tizen.configureTizenProject)(c,_constants.TIZEN));case 38:if(!
_isOK(c,p,[_constants.TIZEN_WATCH])){_context.next=41;break;}_context.next=41;return _regenerator.default.awrap((0,_tizen.configureTizenProject)(c,_constants.TIZEN_WATCH));case 41:if(!
_isOK(c,p,[_constants.TIZEN_MOBILE])){_context.next=44;break;}_context.next=44;return _regenerator.default.awrap((0,_tizen.configureTizenProject)(c,_constants.TIZEN_MOBILE));case 44:if(!
_isOK(c,p,[_constants.WEBOS])){_context.next=47;break;}_context.next=47;return _regenerator.default.awrap((0,_webos.configureWebOSProject)(c,_constants.WEBOS));case 47:if(!
_isOK(c,p,[_constants.WEB])){_context.next=50;break;}_context.next=50;return _regenerator.default.awrap((0,_web.configureWebProject)(c,_constants.WEB));case 50:if(!
_isOK(c,p,[_constants.MACOS])){_context.next=53;break;}_context.next=53;return _regenerator.default.awrap((0,_electron.configureElectronProject)(c,_constants.MACOS));case 53:if(!
_isOK(c,p,[_constants.WINDOWS])){_context.next=56;break;}_context.next=56;return _regenerator.default.awrap((0,_electron.configureElectronProject)(c,_constants.WINDOWS));case 56:if(!
_isOK(c,p,[_constants.KAIOS])){_context.next=59;break;}_context.next=59;return _regenerator.default.awrap((0,_firefox.configureKaiOSProject)(c,_constants.KAIOS));case 59:if(!
_isOK(c,p,[_constants.FIREFOX_OS])){_context.next=62;break;}_context.next=62;return _regenerator.default.awrap((0,_firefox.configureKaiOSProject)(c,_constants.FIREFOX_OS));case 62:if(!
_isOK(c,p,[_constants.FIREFOX_TV])){_context.next=65;break;}_context.next=65;return _regenerator.default.awrap((0,_firefox.configureKaiOSProject)(c,_constants.FIREFOX_TV));case 65:if(!
_isOK(c,p,[_constants.IOS])){_context.next=68;break;}_context.next=68;return _regenerator.default.awrap((0,_apple.configureXcodeProject)(c,_constants.IOS));case 68:if(!
_isOK(c,p,[_constants.TVOS])){_context.next=71;break;}_context.next=71;return _regenerator.default.awrap((0,_apple.configureXcodeProject)(c,_constants.TVOS));case 71:case"end":return _context.stop();}}});};exports.rnvConfigure=rnvConfigure;


var rnvSwitch=function rnvSwitch(c){return new Promise(function(resolve,reject){
var p=c.program.platform||'all';
(0,_common.logTask)("rnvSwitch:"+p);


(0,_projectParser.copyRuntimeAssets)(c).
then(function(){return(0,_projectParser.copySharedPlatforms)(c);}).
then(function(){return(0,_configParser.generateRuntimeConfig)(c);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};exports.rnvSwitch=rnvSwitch;

var rnvLink=function rnvLink(c){return new Promise(function(resolve){
if(_fs.default.existsSync(c.paths.project.npmLinkPolyfill)){
var l=JSON.parse(_fs.default.readFileSync(c.paths.project.npmLinkPolyfill).toString());
Object.keys(l).forEach(function(key){

var source=_path.default.resolve(l[key]);
var nm=_path.default.join(source,'node_modules');
var dest=_path.default.join(c.paths.project.nodeModulesDir,key);
if(_fs.default.existsSync(source)){
(0,_fileutils.copyFolderContentsRecursiveSync)(source,dest,false,[nm]);
}else{
(0,_common.logWarning)("Source: "+source+" doesn't exists!");
}
});
}else{
(0,_common.logWarning)(c.paths.project.npmLinkPolyfill+" file not found. nothing to link!");
resolve();
}
});};exports.rnvLink=rnvLink;

var _isOK=function _isOK(c,p,list){
var result=false;
list.forEach(function(v){
if((0,_common.isPlatformActive)(c,v)&&(p===v||p==='all'))result=true;
});
return result;
};


var _checkAndCreatePlatforms=function _checkAndCreatePlatforms(c,platform){var appFolder,platforms,ks,i,k,_appFolder;return _regenerator.default.async(function _checkAndCreatePlatforms$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_common.logTask)("_checkAndCreatePlatforms:"+platform);if(

_fs.default.existsSync(c.paths.project.builds.dir)){_context2.next=6;break;}
(0,_common.logWarning)('Platforms not created yet. creating them for you...');_context2.next=5;return _regenerator.default.awrap(
(0,_cli.default)(c,{
command:'platform',
subCommand:'configure',
program:{appConfig:c.runtime.appId,platform:platform}}));case 5:return _context2.abrupt("return");case 6:if(!



platform){_context2.next=14;break;}
appFolder=(0,_common.getAppFolder)(c,platform);if(
_fs.default.existsSync(appFolder)){_context2.next=12;break;}
(0,_common.logWarning)("Platform "+platform+" not created yet. creating them for you at "+appFolder);_context2.next=12;return _regenerator.default.awrap(
(0,_cli.default)(c,{
command:'platform',
subCommand:'configure',
program:{appConfig:c.runtime.appId,platform:platform}}));case 12:_context2.next=30;break;case 14:



platforms=c.buildConfig.platforms;if(
platforms){_context2.next=18;break;}
reject("Your "+_chalk.default.white(c.paths.appConfig.config)+" is missconfigured. (Maybe you have older version?). Missing "+_chalk.default.white('{ platforms: {} }')+" object at root");return _context2.abrupt("return");case 18:


ks=Object.keys(platforms);
i=0;case 20:if(!(i<ks.length)){_context2.next=30;break;}
k=ks[i];
_appFolder=(0,_common.getAppFolder)(c,k);if(
_fs.default.existsSync(_appFolder)){_context2.next=27;break;}
(0,_common.logWarning)("Platform "+k+" not created yet. creating one for you at "+_appFolder);_context2.next=27;return _regenerator.default.awrap(
(0,_cli.default)(c,{
command:'platform',
subCommand:'configure',
platform:k,
program:{appConfig:c.runtime.appId,platform:k}}));case 27:i++;_context2.next=20;break;case 30:case"end":return _context2.stop();}}});};






var overridePlugins=function overridePlugins(c,pluginsPath){return new Promise(function(resolve){
(0,_common.logTask)("overridePlugins:"+pluginsPath,_chalk.default.grey);

if(!_fs.default.existsSync(pluginsPath)){
(0,_common.logInfo)("Your project plugin folder "+_chalk.default.white(pluginsPath)+" does not exists. skipping plugin configuration");
resolve();
return;
}

_fs.default.readdirSync(pluginsPath).forEach(function(dir){
if(dir.startsWith('@')){
var pluginsPathNested=_path.default.join(pluginsPath,dir);
_fs.default.readdirSync(pluginsPathNested).forEach(function(subDir){
_overridePlugins(c,pluginsPath,dir+"/"+subDir);
});
}else{
_overridePlugins(c,pluginsPath,dir);
}
});

resolve();
});};

var _overridePlugins=function _overridePlugins(c,pluginsPath,dir){
var source=_path.default.resolve(pluginsPath,dir,'overrides');
var dest=_path.default.resolve(c.paths.project.dir,'node_modules',dir);

if(_fs.default.existsSync(source)){
(0,_fileutils.copyFolderContentsRecursiveSync)(source,dest,false);



}else{
(0,_common.logInfo)("Your plugin configuration has no override path "+_chalk.default.white(source)+". skipping folder override action");
}

var overrideConfig=(0,_fileutils.readObjectSync)(_path.default.resolve(pluginsPath,dir,'overrides.json'));
if(overrideConfig==null?void 0:overrideConfig.overrides){
for(var k in overrideConfig.overrides){
var override=overrideConfig.overrides[k];
ovDir=_path.default.join(dest,k);
if(_fs.default.existsSync(ovDir)){
if(_fs.default.lstatSync(ovDir).isDirectory()){
(0,_common.logWarning)('overrides.json: Directories not supported yet. specify path to actual file');
}else{
var fileToFix=_fs.default.readFileSync(ovDir).toString();
for(var fk in override){
fileToFix=fileToFix.replace(new RegExp(fk,'g'),override[fk]);
}
_fs.default.writeFileSync(ovDir,fileToFix);
}
}
}
}
};
//# sourceMappingURL=index.js.map