var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.applyTemplate=exports.isTemplateInstalled=exports.getInstalledTemplateOptions=exports.getTemplateOptions=exports.configureEntryPoint=exports.configureTemplateFiles=exports.checkIfTemplateConfigured=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _inquirer=_interopRequireDefault(require("inquirer"));

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



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










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







var _prompt=require("../../cli/prompt");
var _common=require("../common");
var _configManager=require("../configManager");




var _resolve=require("../systemManager/resolve");
var _npmUtils=require("../systemManager/npmUtils");

var checkIfTemplateConfigured=function checkIfTemplateConfigured(c){return _regenerator.default.async(function checkIfTemplateConfigured$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('checkIfTemplateConfigured');if(!
c.program.skipDependencyCheck){_context.next=3;break;}return _context.abrupt("return",true);case 3:if(
c.buildConfig.templates){_context.next=6;break;}
(0,_logger.logWarning)("Your "+
(0,_logger.chalk)().white(
c.paths.project.config)+" does not contain "+
(0,_logger.chalk)().white(
'templates')+" object. ReNative will skip template generation");return _context.abrupt("return",


false);case 6:

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.logInfo)("Your "+
(0,_logger.chalk)().white(
k+"@"+obj.version)+" template is missing in renative.json. CONFIGURING...DONE");


c._requiresNpmInstall=true;
c.runtime.requiresBootstrap=true;
}
if(c.files.project.package.devDependencies){
if(c.files.project.package.devDependencies[k]!==obj.version){
(0,_logger.logInfo)("Updating template "+
(0,_logger.chalk)().white(""+
k)+" => "+
(0,_logger.chalk)().green(obj.version)+". ...DONE");

}

c.files.project.package.devDependencies[k]=obj.version;
}
});

_writeObjectSync(c,c.paths.project.package,c.files.project.package);return _context.abrupt("return",

true);case 9:case"end":return _context.stop();}}},null,null,null,Promise);};exports.checkIfTemplateConfigured=checkIfTemplateConfigured;


var _cleanProjectTemplateSync=function _cleanProjectTemplateSync(c){
(0,_logger.logTask)('_cleanProjectTemplateSync');
var dirsToRemove=[
_path.default.join(c.paths.project.appConfigBase.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$(_context2){while(1){switch(_context2.prev=_context2.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(


(0,_fileutils.fsExistsSync)(c.paths.template.configTemplate)){_context2.next=6;break;}
(0,_logger.logWarning)("Template file "+
(0,_logger.chalk)().white(
c.paths.template.configTemplate)+" does not exist. check your "+
(0,_logger.chalk)().white(
c.paths.template.dir)+". skipping");return _context2.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.appConfigBase.dir=_path.default.join(
c.paths.template.appConfigsDir,
'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;
}return _context2.abrupt("return",

true);case 12:case"end":return _context2.stop();}}},null,null,null,Promise);};


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

(0,_logger.logDebug)('configureProject:check src');
if(!(0,_fileutils.fsExistsSync)(c.paths.project.srcDir)){
(0,_logger.logInfo)("Your src folder "+
(0,_logger.chalk)().white(
c.paths.project.srcDir)+" is missing! CREATING...DONE");


(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$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:

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

if(!(0,_fileutils.fsExistsSync)(c.paths.project.appConfigsDir)){
(0,_logger.logInfo)("Your appConfig folder "+
(0,_logger.chalk)().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,_configManager.listAppConfigsFoldersSync)(c,true);


try{
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);
}
});
}catch(e){
(0,_logger.logError)(e);
}
}case 2:case"end":return _context3.stop();}}},null,null,null,Promise);};


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

(0,_logger.logDebug)('configureProject:check projectConfigs');
if(!(0,_fileutils.fsExistsSync)(c.paths.project.appConfigBase.dir)){
(0,_logger.logInfo)("Your projectConfig folder "+
(0,_logger.chalk)().white(
c.paths.project.appConfigBase.dir)+" is missing! CREATING...DONE");


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

}
resolve();
});};

var _configureRenativeConfig=function _configureRenativeConfig(c){var templateConfig,mergedObj;return _regenerator.default.async(function _configureRenativeConfig$(_context4){while(1){switch(_context4.prev=_context4.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.logInfo)("Your "+

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

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


mergedObj.defaults.supportedPlatforms=c.files.project.config.defaults.supportedPlatforms;

mergedObj.engines=c.files.project.config.engines;

mergedObj.currentTemplate=c.runtime.currentTemplate;
if(mergedObj.isNew){
c.runtime.isFirstRunAfterNew=true;
}
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'}}}};




c.files.project.configLocal=templateConfig;
(0,_configManager.generateLocalConfig)(c);

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


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

templateConfig=(0,_fileutils.readObjectSync)(c.paths.template.configTemplate);


includedPaths=templateConfig==null?void 0:(_templateConfig$templ=templateConfig.templateConfig)==null?void 0:_templateConfig$templ.includedPaths;
if(includedPaths){
includedPaths.forEach(function(name){
var sourcePath=_path.default.join(c.paths.template.dir,name);
var destPath=_path.default.join(c.paths.project.dir,name);
if(!(0,_fileutils.fsExistsSync)(destPath)&&(0,_fileutils.fsExistsSync)(sourcePath)){
try{
if((0,_fileutils.fsLstatSync)(sourcePath).isDirectory()){
(0,_logger.logInfo)("Missing directory "+
(0,_logger.chalk)().white(
destPath+".js")+". COPYING from TEMPATE...DONE");


(0,_fileutils.copyFolderContentsRecursiveSync)(
sourcePath,
destPath);

}else{
(0,_logger.logInfo)("Missing file "+
(0,_logger.chalk)().white(
destPath+".js")+". COPYING from TEMPATE...DONE");


(0,_fileutils.copyFileSync)(sourcePath,destPath);
}
}catch(e){

}
}
});
}case 4:case"end":return _context5.stop();}}},null,null,null,Promise);};exports.configureTemplateFiles=configureTemplateFiles;


var configureEntryPoint=function configureEntryPoint(c,platform){var entryFile,source,backupSource,dest;return _regenerator.default.async(function configureEntryPoint$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
(0,_logger.logTask)('configureEntryPoint');
entryFile=(0,_common.getConfigProp)(c,platform,'entryFile');_context6.prev=2;


source=_path.default.join(
c.paths.template.dir,
entryFile+".js");

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

dest=_path.default.join(c.paths.project.dir,entryFile+".js");
if(!(0,_fileutils.fsExistsSync)(dest)){
if(!entryFile){
(0,_logger.logWarning)("Missing entryFile key for "+
(0,_logger.chalk)().white(
c.platform)+" platform in your "+
(0,_logger.chalk)().white(
c.paths.appConfig.config)+".");


}else if(!(0,_fileutils.fsExistsSync)(source)){
(0,_logger.logInfo)("Missing entry file "+
(0,_logger.chalk)().white(
entryFile+".js")+". COPYING from RNV...DONE");


(0,_fileutils.copyFileSync)(backupSource,dest);
}else{
(0,_logger.logInfo)("Missing entry file "+
(0,_logger.chalk)().white(
entryFile+".js")+". COPYING from TEMPATE...DONE");


(0,_fileutils.copyFileSync)(source,dest);
}
}_context6.next=12;break;case 9:_context6.prev=9;_context6.t0=_context6["catch"](2);return _context6.abrupt("return",

Promise.reject(_context6.t0));case 12:return _context6.abrupt("return",


true);case 13:case"end":return _context6.stop();}}},null,null,[[2,9]],Promise);};exports.configureEntryPoint=configureEntryPoint;


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

var getTemplateOptions=function getTemplateOptions(c){
var defaultProjectTemplates=c.buildConfig.projectTemplates||{};
return(0,_prompt.generateOptions)(
defaultProjectTemplates,
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?(0,_logger.chalk)().yellow(' (installed)'):'';
return" ["+(0,_logger.chalk)().grey(i+1)+"]> "+(0,_logger.chalk)().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 isTemplateInstalled=function isTemplateInstalled(c){return(0,_resolve.doResolve)(c.buildConfig.currentTemplate);};exports.isTemplateInstalled=isTemplateInstalled;


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

c.runtime.selectedTemplate=selectedTemplate;if(

c.buildConfig.currentTemplate){_context7.next=12;break;}
(0,_logger.logWarning)("You don't have any current template selected");
opts=getInstalledTemplateOptions(c);_context7.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$promp=_context7.sent;template=_await$inquirer$promp.template;


c.buildConfig.currentTemplate=template;
c.files.project.config.currentTemplate=template;
_writeObjectSync(c,c.paths.project.config,c.files.project.config);case 12:


templateIsInstalled=(0,_resolve.doResolve)(c.buildConfig.currentTemplate);if(
templateIsInstalled){_context7.next=16;break;}_context7.next=16;return _regenerator.default.awrap(

(0,_npmUtils.checkIfProjectAndNodeModulesExists)(c));case 16:_context7.next=18;return _regenerator.default.awrap(


_applyTemplate(c));case 18:_context7.next=20;return _regenerator.default.awrap(
_configureSrc(c));case 20:_context7.next=22;return _regenerator.default.awrap(
_configureAppConfigs(c));case 22:_context7.next=24;return _regenerator.default.awrap(
_configureProjectConfig(c));case 24:_context7.next=26;return _regenerator.default.awrap(
_configureRenativeConfig(c));case 26:case"end":return _context7.stop();}}},null,null,null,Promise);};exports.applyTemplate=applyTemplate;
//# sourceMappingURL=index.js.map