var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.ejectPlatform=exports.copySharedPlatforms=exports.isPlatformActive=exports.isPlatformSupportedSync=exports.isPlatformSupported=exports.createPlatformBuild=exports.cleanPlatformBuild=exports.generatePlatformChoices=exports.updateProjectPlatforms=exports.logErrorPlatform=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _path=_interopRequireDefault(require("path"));
var _logger=require("../systemManager/logger");
var _prompt=require("../../cli/prompt");
var _fileutils=require("../systemManager/fileutils");




var _installer=require("../sdkManager/installer");
var _common=require("../common");


var logErrorPlatform=function logErrorPlatform(c){
(0,_logger.logError)("Platform: "+
(0,_logger.chalk)().white(
c.platform)+" doesn't support command: "+
(0,_logger.chalk)().white(c.command),
true);

return false;
};exports.logErrorPlatform=logErrorPlatform;

var updateProjectPlatforms=function updateProjectPlatforms(c,platforms){
var
config=
c.paths.project.config;
var currentConfig=c.files.project.config;
currentConfig.defaults=currentConfig.defaults||{};
currentConfig.defaults.supportedPlatforms=platforms;
(0,_fileutils.writeFileSync)(config,currentConfig);
};exports.updateProjectPlatforms=updateProjectPlatforms;


var generatePlatformChoices=function generatePlatformChoices(c){
var options=c.runtime.supportedPlatforms.map(function(v){var _v$engine,_v$engine$config;return{
name:v.platform+" - "+(v.isConnected?(0,_logger.chalk)().green('(connected)'):(0,_logger.chalk)().yellow('(ejected)'))+" ["+(0,_logger.chalk)().cyan((_v$engine=v.engine)==null?void 0:(_v$engine$config=_v$engine.config)==null?void 0:_v$engine$config.id)+"]",
value:v.platform,
isConnected:v.isConnected};});

return options;
};exports.generatePlatformChoices=generatePlatformChoices;

var cleanPlatformBuild=function cleanPlatformBuild(c,platform){return new Promise(function(resolve){
(0,_logger.logTask)('cleanPlatformBuild');

var cleanTasks=[];

if(platform==='all'){
Object.keys(c.buildConfig.platforms).forEach(function(k){
if(isPlatformSupportedSync(c,k)){
var pPath=_path.default.join(
c.paths.project.builds.dir,
c.runtime.appId+"_"+k);

cleanTasks.push((0,_fileutils.cleanFolder)(pPath));
}
});
}else if(isPlatformSupportedSync(c,platform)){
var pPath=_path.default.join(
c.paths.project.builds.dir,
c.runtime.appId+"_"+platform);

cleanTasks.push((0,_fileutils.cleanFolder)(pPath));
}

Promise.all(cleanTasks).then(function(){
resolve();
});
});};exports.cleanPlatformBuild=cleanPlatformBuild;

var createPlatformBuild=function createPlatformBuild(c,platform){return new Promise(function(resolve,reject){
(0,_logger.logTask)('createPlatformBuild');

if(!isPlatformSupportedSync(c,platform,null,reject))return;

var pPath=_path.default.join(
c.paths.project.builds.dir,
c.runtime.appId+"_"+platform);

var ptPath=_path.default.join(
c.paths.project.platformTemplatesDirs[platform],""+
platform);


(0,_fileutils.copyFolderContentsRecursiveSync)(ptPath,pPath,false,[
_path.default.join(ptPath,'_privateConfig')],
false,{},(0,_common.getTimestampPathsConfig)(c,platform),c);

resolve();
});};exports.createPlatformBuild=createPlatformBuild;

var isPlatformSupported=function isPlatformSupported(c){var _c$files$project$conf,_c$files$project$conf2;var platformsAsObj,opts,_await$inquirerPrompt,platform,configuredPlatforms,_await$inquirerPrompt2,confirm,newPlatforms;return _regenerator.default.async(function isPlatformSupported$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('isPlatformSupported');
platformsAsObj=c.buildConfig?
c.buildConfig.platforms:
c.supportedPlatforms;
if(!platformsAsObj)platformsAsObj=c.runtime.availablePlatforms;
opts=(0,_prompt.generateOptions)(platformsAsObj);if(!(


!c.platform||
c.platform===true||
!c.runtime.availablePlatforms.includes(c.platform))){_context.next=10;break;}_context.next=7;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
name:'platform',
type:'list',
message:'Pick one of available platforms',
choices:opts.keysAsArray,
logMessage:'You need to specify platform'}));case 7:_await$inquirerPrompt=_context.sent;platform=_await$inquirerPrompt.platform;


c.platform=platform;case 10:


configuredPlatforms=(_c$files$project$conf=c.files.project.config)==null?void 0:(_c$files$project$conf2=_c$files$project$conf.defaults)==null?void 0:_c$files$project$conf2.supportedPlatforms;if(!(

Array.isArray(configuredPlatforms)&&
!configuredPlatforms.includes(c.platform))){_context.next=23;break;}_context.next=14;return _regenerator.default.awrap(

(0,_prompt.inquirerPrompt)({
type:'confirm',
message:"Platform "+
c.platform+" is not supported by your project. Would you like to enable it?"}));case 14:_await$inquirerPrompt2=_context.sent;confirm=_await$inquirerPrompt2.confirm;if(!



confirm){_context.next=22;break;}
newPlatforms=[].concat((0,_toConsumableArray2.default)(configuredPlatforms),[c.platform]);
updateProjectPlatforms(c,newPlatforms);
c.buildConfig.defaults.supportedPlatforms=newPlatforms;_context.next=23;break;case 22:throw(


new Error('User canceled'));case 23:_context.next=25;return _regenerator.default.awrap(




(0,_installer.checkAndConfigureSdks)(c));case 25:return _context.abrupt("return",
c.platform);case 26:case"end":return _context.stop();}}},null,null,null,Promise);};exports.isPlatformSupported=isPlatformSupported;


var isPlatformSupportedSync=function isPlatformSupportedSync(c,platform,resolve,reject){
if(!platform){
if(reject){
reject(
(0,_logger.chalk)().red("You didn't specify platform. make sure you add \""+
(0,_logger.chalk)().white.bold(
'-p <PLATFORM>')+"\" option to your command!"));



}
return false;
}
if(!c.runtime.availablePlatforms.includes(platform)){
if(reject){
reject(
(0,_logger.chalk)().red("Platform "+
platform+" is not supported. Use one of the following: "+(0,_logger.chalk)().white(
c.runtime.availablePlatforms.join(', '))+" ."));



}
return false;
}
if(resolve)resolve();
return true;
};exports.isPlatformSupportedSync=isPlatformSupportedSync;

var isPlatformActive=function isPlatformActive(c,platform,resolve){
if(!c.buildConfig||!c.buildConfig.platforms){
(0,_logger.logError)("Your appConfigFile is not configured properly! check "+
(0,_logger.chalk)().white(
c.paths.appConfig.config)+" location.");


if(resolve)resolve();
return false;
}
if(!c.buildConfig.platforms[platform]){
(0,_logger.logWarning)("Platform "+
platform+" not configured for "+c.runtime.appId+". skipping.");

if(resolve)resolve();
return false;
}
return true;
};exports.isPlatformActive=isPlatformActive;
var copySharedPlatforms=function copySharedPlatforms(c){return new Promise(function(resolve){
(0,_logger.logTask)('copySharedPlatforms');

if(c.platform){
(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.resolve(
c.paths.project.platformTemplatesDirs[c.platform],
'_shared'),

(0,_common.getPlatformBuildDir)(c));

}

resolve();
});};exports.copySharedPlatforms=copySharedPlatforms;

var ejectPlatform=function ejectPlatform(c,platform){
var engine=c.runtime.enginesByPlatform[platform];
var destDir=_path.default.join(c.paths.project.dir,'platformTemplates',platform);
var sourcePlatformDir=engine.originalTemplatePlatformsDir;
(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(sourcePlatformDir,platform),
destDir);


(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(sourcePlatformDir,'_shared'),
destDir);

};exports.ejectPlatform=ejectPlatform;
//# sourceMappingURL=index.js.map