var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.rnvTemplateApply=exports.applyTemplate=exports.rnvTemplateAdd=exports.rnvTemplateList=exports.getInstalledTemplateOptions=exports.getTemplateOptions=exports.configureEntryPoints=exports.checkIfTemplateInstalled=exports.addTemplate=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));
var _chalk=_interopRequireDefault(require("chalk"));
var _fs=_interopRequireWildcard(require("fs"));
var _path=_interopRequireDefault(require("path"));
var _inquirer=_interopRequireDefault(require("inquirer"));

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



var _fileutils=require("../systemTools/fileutils");








var _logger=require("../systemTools/logger");







var _prompt=require("../systemTools/prompt");
var _configParser=require("../configTools/configParser");







var _resolve=require("../resolve");
var _engineTools=require("../engineTools");










var addTemplate=function addTemplate(c,template){
(0,_logger.logTask)('addTemplate');

c.files.project.config.templates=c.files.project.config.templates||{};

if(!c.files.project.config.templates[template]){
c.files.project.config.templates[template]={
version:'latest'};

}

_writeObjectSync(c,c.paths.project.config,c.files.project.config);
};exports.addTemplate=addTemplate;

var checkIfTemplateInstalled=function checkIfTemplateInstalled(c){return new Promise(function(resolve){
(0,_logger.logTask)('checkIfTemplateInstalled');
if(!c.buildConfig.templates){
(0,_logger.logWarning)("Your "+
_chalk.default.white(
c.paths.project.config)+" does not contain "+
_chalk.default.white(
'templates')+" object. ReNative will skip template generation");


resolve();
return;
}
Object.keys(c.buildConfig.templates).forEach(function(k){
var obj=c.buildConfig.templates[k];
if(
!(0,_resolve.doResolve)(k.version,false,{basedir:'../'})&&
!(0,_resolve.doResolve)(k,false))
{
(0,_logger.logWarning)("Your "+
_chalk.default.white(
k.version)+" template is not installed. ReNative will install it for you");


c._requiresNpmInstall=true;
}
if(c.files.project.package.devDependencies){
c.files.project.package.devDependencies[k]=obj.version;
}
});

_writeObjectSync(c,c.paths.project.package,c.files.project.package);

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

var _cleanProjectTemplateSync=function _cleanProjectTemplateSync(c){
(0,_logger.logTask)('_cleanProjectTemplateSync');
var dirsToRemove=[
_path.default.join(c.paths.project.projectConfig.dir),
_path.default.join(c.paths.project.srcDir),
_path.default.join(c.paths.project.appConfigsDir)];


var filesToRemove=c.buildConfig.defaults.supportedPlatforms.map(
function(p){return _path.default.join(c.paths.project.dir,"index."+p+".js");});


(0,_fileutils.removeDirsSync)(dirsToRemove);

(0,_fileutils.removeFilesSync)(filesToRemove);
};

var _applyTemplate=function _applyTemplate(c){return _regenerator.default.async(function _applyTemplate$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)("_applyTemplate | current:"+c.buildConfig.currentTemplate+" | selected:"+c.runtime.selectedTemplate);

if(c.runtime.selectedTemplate){
_cleanProjectTemplateSync(c);








c.paths.template.dir=(0,_resolve.doResolve)(c.runtime.selectedTemplate);


}else{
c.paths.template.dir=(0,_resolve.doResolve)(c.buildConfig.currentTemplate);

}

c.paths.template.configTemplate=_path.default.join(
c.paths.template.dir,
_constants.RENATIVE_CONFIG_TEMPLATE_NAME);if(


_fs.default.existsSync(c.paths.template.configTemplate)){_context.next=6;break;}
(0,_logger.logWarning)("Template file "+
_chalk.default.white(
c.paths.template.configTemplate)+" does not exist. check your "+
_chalk.default.white(
c.paths.template.dir)+". skipping");return _context.abrupt("return",


true);case 6:


(0,_logger.logDebug)("_applyTemplate:"+
c.runtime.selectedTemplate+":"+c.paths.template.dir);


c.paths.template.appConfigsDir=_path.default.join(
c.paths.template.dir,
'appConfigs');

c.paths.template.projectConfigDir=_path.default.join(
c.paths.template.dir,
'base');

c.runtime.currentTemplate=c.files.project.config.currentTemplate;
if(!c.runtime.currentTemplate){
c.runtime.currentTemplate=Object.keys(
c.files.project.config.templates)[
0];
c.runtime.requiresForcedTemplateApply=true;
}_context.next=13;return _regenerator.default.awrap(

(0,_configParser.setAppConfig)(c,c.runtime.appId));case 13:
(0,_configParser.generateLocalConfig)(c,!!c.runtime.selectedTemplate);return _context.abrupt("return",

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


var _configureSrc=function _configureSrc(c){return new Promise(function(resolve){

(0,_logger.logDebug)('configureProject:check src');
if(!_fs.default.existsSync(c.paths.project.srcDir)){
(0,_logger.logInfo)("Looks like your src folder "+
_chalk.default.white(
c.paths.project.srcDir)+" is missing! Let's create one for you.");


(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(c.paths.template.dir,'src'),
c.paths.project.srcDir);

}
resolve();
});};

var _configureAppConfigs=function _configureAppConfigs(c){var appConfigIds,_c$files$project,_c$files$project$conf,_c$files$project$conf2,supPlats;return _regenerator.default.async(function _configureAppConfigs$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:

(0,_logger.logDebug)('configureProject:check appConfigs');if(

_fs.default.existsSync(c.paths.project.appConfigsDir)){_context2.next=15;break;}
(0,_logger.logInfo)("Looks like your appConfig folder "+
_chalk.default.white(
c.paths.project.appConfigsDir)+" is missing! ReNative will create one from template.");




(0,_fileutils.copyFolderContentsRecursiveSync)(
c.paths.template.appConfigsDir,
c.paths.project.appConfigsDir);


appConfigIds=(0,_configParser.listAppConfigsFoldersSync)(c,true);_context2.prev=5;



supPlats=(_c$files$project=c.files.project)==null?void 0:(_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;
appConfigIds.forEach(function(v){
var appConfigPath=_path.default.join(
c.paths.project.appConfigsDir,
v,
_constants.RENATIVE_CONFIG_NAME);

var appConfig=(0,_fileutils.readObjectSync)(appConfigPath);
if(appConfig){
appConfig.common=appConfig.common||{};
if(!c.runtime.isWrapper){var _c$files$project$conf3,_c$files$project$conf4,_c$files$project$conf5,_c$files$project$conf6;
appConfig.common.title=(_c$files$project$conf3=c.files.project.config)==null?void 0:(_c$files$project$conf4=_c$files$project$conf3.defaults)==null?void 0:_c$files$project$conf4.title;
appConfig.common.id=(_c$files$project$conf5=c.files.project.config)==null?void 0:(_c$files$project$conf6=_c$files$project$conf5.defaults)==null?void 0:_c$files$project$conf6.id;
}

if(supPlats){
Object.keys(appConfig.platforms).forEach(function(pk){
if(!supPlats.includes(pk)){
delete appConfig.platforms[pk];
}
});
}

_writeObjectSync(c,appConfigPath,appConfig);
}
});_context2.next=10;return _regenerator.default.awrap(

(0,_configParser.updateConfig)(c,true));case 10:_context2.next=15;break;case 12:_context2.prev=12;_context2.t0=_context2["catch"](5);

(0,_logger.logError)(_context2.t0);case 15:case"end":return _context2.stop();}}},null,null,[[5,12]],Promise);};




var _configureProjectConfig=function _configureProjectConfig(c){return new Promise(function(resolve){

(0,_logger.logDebug)('configureProject:check projectConfigs');
if(!_fs.default.existsSync(c.paths.project.projectConfig.dir)){
(0,_logger.logInfo)("Looks like your projectConfig folder "+
_chalk.default.white(
c.paths.project.projectConfig.dir)+" is missing! Let's create one for you.");


(0,_fileutils.copyFolderContentsRecursiveSync)(
c.paths.template.projectConfigDir,
c.paths.project.projectConfig.dir);

}
resolve();
});};

var _configureRenativeConfig=function _configureRenativeConfig(c){var templateConfig,mergedObj;return _regenerator.default.async(function _configureRenativeConfig$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:

templateConfig=(0,_fileutils.readObjectSync)(c.paths.template.configTemplate);
(0,_logger.logDebug)('configureProject:check renative.json');


































if(!c.runtime.isWrapper){
if(
c.runtime.selectedTemplate||
c.runtime.requiresForcedTemplateApply||
c.files.project.config.isNew)
{
(0,_logger.logWarning)("Looks like your "+

c.paths.project.config+" need to be updated with "+
c.paths.template.configTemplate);

mergedObj=(0,_fileutils.mergeObjects)(
c,
c.files.project.config,
templateConfig,
false,
true);

mergedObj.currentTemplate=c.runtime.currentTemplate;
mergedObj.isNew=null;
delete mergedObj.isNew;
c.files.project.config=mergedObj;
_writeObjectSync(c,c.paths.project.config,mergedObj);
}
}else{



templateConfig.plugins.renative={
webpack:{
modulePaths:[
{
projectPath:'../../packages/renative'}],


moduleAliases:{
renative:{
projectPath:'../../packages/renative'}}}};





_writeObjectSync(c,c.paths.project.configLocal,templateConfig);
}return _context3.abrupt("return",
true);case 4:case"end":return _context3.stop();}}},null,null,null,Promise);};


var _parseSupportedPlatforms=function _parseSupportedPlatforms(c,callback){var _c$buildConfig$defaul;var p,pLen,supportedPlatforms,i,k,plat,platKeysNum;return _regenerator.default.async(function _parseSupportedPlatforms$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:if(
c.buildConfig.platforms){_context4.next=3;break;}_context4.next=3;return _regenerator.default.awrap(
(0,_configParser.parseRenativeConfigs)(c));case 3:

p=Object.keys(c.buildConfig.platforms);
pLen=p.length;
supportedPlatforms=(_c$buildConfig$defaul=c.buildConfig.defaults)==null?void 0:_c$buildConfig$defaul.supportedPlatforms;
for(i=0;i<pLen;i++){
k=p[i];

plat=c.buildConfig.platforms[k];
platKeysNum=plat!==undefined?Object.keys(plat).length:0;

if(supportedPlatforms&&supportedPlatforms.includes(k)||!supportedPlatforms){
callback(k,plat);
}else if(platKeysNum>1){

(0,_logger.logWarning)("Extra platform "+
_chalk.default.white(
k)+" will be ignored because it's not configured in your "+
_chalk.default.white(
'./renative.json: { defaults.supportedPlatforms }')+" object.");


}
}return _context4.abrupt("return",
true);case 8:case"end":return _context4.stop();}}},null,null,null,Promise);};


var configureEntryPoints=function configureEntryPoints(c){return _regenerator.default.async(function configureEntryPoints$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('configureEntryPoints');





(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(c.paths.rnv.dir,'entry'),
c.paths.entryDir);_context5.prev=2;if(




_fs.default.existsSync(c.paths.appConfig.config)){_context5.next=6;break;}
(0,_logger.logWarning)("c.paths.appConfig.config at path: "+

c.paths.appConfig.config+" does not exist. ReNative will regenerate renative.local.json");return _context5.abrupt("return",


true);case 6:_context5.next=8;return _regenerator.default.awrap(

_parseSupportedPlatforms(c,function(platform,plat){
var source=_path.default.join(
c.paths.template.dir,
plat.entryFile+".js");

var backupSource=_path.default.join(
c.paths.rnv.projectTemplate.dir,
'entry',
plat.entryFile+".js");

var dest=_path.default.join(c.paths.project.dir,plat.entryFile+".js");
if(!_fs.default.existsSync(dest)){
if(!plat.entryFile){
(0,_logger.logWarning)("You missing entryFile key for "+
_chalk.default.white(
platform)+" platform in your "+
_chalk.default.white(
c.paths.appConfig.config)+".");


}else if(!_fs.default.existsSync(source)){
(0,_logger.logInfo)("You missing entry file "+
_chalk.default.white(
source)+" in your template. ReNative Will use default backup entry from "+
_chalk.default.white(
backupSource)+"!");


(0,_fileutils.copyFileSync)(backupSource,dest);
}else{
(0,_logger.logInfo)("You missing entry file "+
_chalk.default.white(
plat.entryFile)+" in your project. let's create one for you!");


(0,_fileutils.copyFileSync)(source,dest);
}
}
}));case 8:_context5.next=13;break;case 10:_context5.prev=10;_context5.t0=_context5["catch"](2);return _context5.abrupt("return",

Promise.reject(_context5.t0));case 13:return _context5.abrupt("return",


true);case 14:case"end":return _context5.stop();}}},null,null,[[2,10]],Promise);};exports.configureEntryPoints=configureEntryPoints;


var _configureMetroConfigs=function _configureMetroConfigs(c,platform){var configDir,dest;return _regenerator.default.async(function _configureMetroConfigs$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
configDir=_path.default.join(c.paths.project.dir,'configs');
if(!_fs.default.existsSync(configDir)){
(0,_fs.mkdirSync)(configDir);
}
dest=_path.default.join(configDir,"metro.config."+platform+".js");
if(!_fs.default.existsSync(dest)){
(0,_fileutils.writeFileSync)(
dest,"const { EXTENSIONS } = require('rnv/dist/constants');\nconst config = require('../metro.config');\n\nconfig.resolver.sourceExts = EXTENSIONS."+



platform+";\nmodule.exports = config;\n");



}case 4:case"end":return _context6.stop();}}},null,null,null,Promise);};


var _writeObjectSync=function _writeObjectSync(c,p,s){
(0,_fileutils.writeFileSync)(p,s);
(0,_configParser.generateBuildConfig)(c);
};

var getTemplateOptions=function getTemplateOptions(c){return(0,_prompt.generateOptions)(
c.buildConfig.projectTemplates,
false,
null,
function(i,obj,mapping,defaultVal){var _c$buildConfig$templa;
var exists=(_c$buildConfig$templa=c.buildConfig.templates)==null?void 0:_c$buildConfig$templa[defaultVal];
var installed=exists?_chalk.default.yellow(' (installed)'):'';
return" ["+_chalk.default.grey(i+1)+"]> "+_chalk.default.bold(
defaultVal)+
installed+" \n";
});};exports.getTemplateOptions=getTemplateOptions;


var getInstalledTemplateOptions=function getInstalledTemplateOptions(c){
if(c.buildConfig.templates){
return(0,_prompt.generateOptions)(c.buildConfig.templates);
}
(0,_logger.logError)("You don't have any local templates installed",false,true);
return[];
};exports.getInstalledTemplateOptions=getInstalledTemplateOptions;

var rnvTemplateList=function rnvTemplateList(c){return new Promise(function(resolve){
(0,_logger.logTask)('rnvTemplateList');
var opts=getTemplateOptions(c);
(0,_logger.logToSummary)("Templates:\n\n"+opts.asString);
resolve();
});};exports.rnvTemplateList=rnvTemplateList;

var rnvTemplateAdd=function rnvTemplateAdd(c){var opts,_await$inquirer$promp,template;return _regenerator.default.async(function rnvTemplateAdd$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:
(0,_logger.logTask)('rnvTemplateAdd');

opts=getTemplateOptions(c);_context7.next=4;return _regenerator.default.awrap(

_inquirer.default.prompt({
type:'list',
message:'Pick which template to install',
name:'template',
choices:opts.keysAsArray}));case 4:_await$inquirer$promp=_context7.sent;template=_await$inquirer$promp.template;


addTemplate(c,template);case 7:case"end":return _context7.stop();}}},null,null,null,Promise);};exports.rnvTemplateAdd=rnvTemplateAdd;


var applyTemplate=function applyTemplate(c,selectedTemplate){var opts,_await$inquirer$promp2,template,engine;return _regenerator.default.async(function applyTemplate$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:
(0,_logger.logTask)("applyTemplate:"+
c.buildConfig.currentTemplate+"=>"+selectedTemplate+":");

c.runtime.selectedTemplate=selectedTemplate;if(

c.buildConfig.currentTemplate){_context8.next=12;break;}
(0,_logger.logWarning)("You don't have any current template selected");
opts=getInstalledTemplateOptions(c);_context8.next=7;return _regenerator.default.awrap(

_inquirer.default.prompt({
type:'list',
name:'template',
message:'Pick which template to apply',
choices:opts.keysAsArray}));case 7:_await$inquirer$promp2=_context8.sent;template=_await$inquirer$promp2.template;


c.buildConfig.currentTemplate=template;
c.files.project.config.currentTemplate=template;
_writeObjectSync(c,c.paths.project.config,c.files.project.config);case 12:_context8.next=14;return _regenerator.default.awrap(


_applyTemplate(c));case 14:_context8.next=16;return _regenerator.default.awrap(
_configureSrc(c));case 16:_context8.next=18;return _regenerator.default.awrap(
_configureAppConfigs(c));case 18:_context8.next=20;return _regenerator.default.awrap(
_configureProjectConfig(c));case 20:_context8.next=22;return _regenerator.default.awrap(
_configureRenativeConfig(c));case 22:_context8.next=24;return _regenerator.default.awrap(
configureEntryPoints(c));case 24:


engine=(0,_engineTools.getEngineByPlatform)(c,c.platform);if(!(
engine==null?void 0:engine.requiresMetroConfig)){_context8.next=28;break;}_context8.next=28;return _regenerator.default.awrap(
_configureMetroConfigs(c,c.platform));case 28:case"end":return _context8.stop();}}},null,null,null,Promise);};exports.applyTemplate=applyTemplate;



var rnvTemplateApply=function rnvTemplateApply(c){var opts,_await$inquirer$promp3,template;return _regenerator.default.async(function rnvTemplateApply$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:
(0,_logger.logTask)("rnvTemplateApply:"+c.program.template);if(!

c.program.template){_context9.next=3;break;}return _context9.abrupt("return",
applyTemplate(c,c.program.template));case 3:

opts=getInstalledTemplateOptions(c);_context9.next=6;return _regenerator.default.awrap(

_inquirer.default.prompt({
type:'list',
message:'Pick which template to install',
name:'template',
choices:opts.keysAsArray}));case 6:_await$inquirer$promp3=_context9.sent;template=_await$inquirer$promp3.template;


applyTemplate(c,template);case 9:case"end":return _context9.stop();}}},null,null,null,Promise);};exports.rnvTemplateApply=rnvTemplateApply;
//# sourceMappingURL=index.js.map