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 _inquirer=_interopRequireDefault(require("inquirer"));
var _semver=_interopRequireDefault(require("semver"));
var _common=require("../common");







var _prompt=require("../systemTools/prompt");
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 _templateTools=require("../templateTools");
var _fileutils=require("../systemTools/fileutils");
var _exec=require("../systemTools/exec");
var _cli=_interopRequireDefault(require("../cli"));
var _buildHooks=require("./buildHooks");
var _logger=require("../systemTools/logger");
var _projectParser=require("./projectParser");
var _workspace=require("./workspace");
var _configParser=require("../configTools/configParser");

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

_checkAndCreatePlatforms(c,c.program.platform).
then(function(){return(0,_projectParser.copyRuntimeAssets)(c);}).
then(function(){return(0,_projectParser.copySharedPlatforms)(c);}).
then(function(){return(0,_configParser.generateRuntimeConfig)(c);}).
then(function(){return _runPlugins(c,c.paths.rnv.plugins.dir);}).
then(function(){return _runPlugins(c,c.paths.project.projectConfig.pluginsDir);}).
then(function(){return _isOK(c,p,[_constants.ANDROID])?(0,_android.configureGradleProject)(c,_constants.ANDROID):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.ANDROID_TV])?(0,_android.configureGradleProject)(c,_constants.ANDROID_TV):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.ANDROID_WEAR])?(0,_android.configureGradleProject)(c,_constants.ANDROID_WEAR):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.TIZEN])?(0,_tizen.configureTizenGlobal)(c,_constants.TIZEN):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.TIZEN])?(0,_tizen.configureTizenProject)(c,_constants.TIZEN):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.TIZEN_WATCH])?(0,_tizen.configureTizenProject)(c,_constants.TIZEN_WATCH):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.TIZEN_MOBILE])?(0,_tizen.configureTizenProject)(c,_constants.TIZEN_MOBILE):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.WEBOS])?(0,_webos.configureWebOSProject)(c,_constants.WEBOS):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.WEB])?(0,_web.configureWebProject)(c,_constants.WEB):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.MACOS])?(0,_electron.configureElectronProject)(c,_constants.MACOS):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.WINDOWS])?(0,_electron.configureElectronProject)(c,_constants.WINDOWS):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.KAIOS])?(0,_firefox.configureKaiOSProject)(c,_constants.KAIOS):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.FIREFOX_OS])?(0,_firefox.configureKaiOSProject)(c,_constants.FIREFOX_OS):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.FIREFOX_TV])?(0,_firefox.configureKaiOSProject)(c,_constants.FIREFOX_TV):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.IOS])?(0,_apple.configureXcodeProject)(c,_constants.IOS):Promise.resolve();}).
then(function(){return _isOK(c,p,[_constants.TVOS])?(0,_apple.configureXcodeProject)(c,_constants.TVOS):Promise.resolve();}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};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$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_common.logTask)("_checkAndCreatePlatforms:"+platform);if(

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



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



platforms=c.buildConfig.platforms;if(
platforms){_context.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 _context.abrupt("return");case 18:


ks=Object.keys(platforms);
i=0;case 20:if(!(i<ks.length)){_context.next=30;break;}
k=ks[i];
_appFolder=(0,_common.getAppFolder)(c,k);if(
_fs.default.existsSync(_appFolder)){_context.next=27;break;}
(0,_common.logWarning)("Platform "+k+" not created yet. creating one for you at "+_appFolder);_context.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++;_context.next=20;break;case 30:case"end":return _context.stop();}}});};






var _runPlugins=function _runPlugins(c,pluginsPath){return new Promise(function(resolve){
(0,_common.logTask)("_runPlugins:"+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){
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 override action");
}
});

resolve();
});};