var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.taskRnvNew=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _inquirer=_interopRequireDefault(require("inquirer"));
var _lodash=_interopRequireDefault(require("lodash.set"));
var _path=_interopRequireDefault(require("path"));
var _semver=_interopRequireDefault(require("semver"));
var _constants=require("../../core/constants");




var _projectManager=require("../../core/projectManager");
var _workspace=require("../../core/projectManager/workspace");
var _runtimeManager=require("../../core/runtimeManager");
var _analytics=_interopRequireDefault(require("../../core/systemManager/analytics"));
var _exec=require("../../core/systemManager/exec");
var _fileutils=require("../../core/systemManager/fileutils");








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











var _npmUtils=require("../../core/systemManager/npmUtils");
var _templateManager=require("../../core/templateManager");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly){symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});}keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(Object(source),true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}

var highlight=(0,_logger.chalk)().green;

var _prepareProjectOverview=function _prepareProjectOverview(c,data){
data.appTitle=data.inputAppTitle||data.defaultAppTitle;
data.teamID='';
data.appID=data.inputAppID?
data.inputAppID.replace(/\s+/g,'-').toLowerCase():
data.appID;
data.version=data.inputVersion||data.defaultVersion;
var tempString=data.optionTemplates.selectedOption+"@"+
data.optionTemplates.selectedVersion;


var str=(0,_logger.printBoxStart)('🚀  ReNative Project Generator');
str+=(0,_logger.printIntoBox)('');
str+=(0,_logger.printIntoBox)("Project Name (folder): "+
highlight(data.projectName),
1);

str+=(0,_logger.printIntoBox)("Workspace: "+
highlight(data.optionWorkspaces.selectedOption),
1);

str+=(0,_logger.printIntoBox)("Project Title: "+highlight(data.appTitle),1);
str+=(0,_logger.printIntoBox)("Project Version: "+highlight(data.version),1);
str+=(0,_logger.printIntoBox)("App ID: "+highlight(data.appID),1);
str+=(0,_logger.printIntoBox)("Project Template: "+highlight(tempString),1);
str+=(0,_logger.printIntoBox)("Git Enabled: "+highlight(data.gitEnabled),1);
str+=(0,_logger.printIntoBox)('');
str+=(0,_logger.printIntoBox)('Project Platforms:');
str+=(0,_logger.printArrIntoBox)(data.optionPlatforms.selectedOptions);
str+=(0,_logger.printIntoBox)('');
str+=(0,_logger.printIntoBox)('Project Structure:');
str+=(0,_logger.printIntoBox)('');
str+=(0,_logger.printIntoBox)(data.projectName);
str+=(0,_logger.chalk)().
gray("\u2502   \u251C\u2500\u2500 appConfigs            # Application flavour configuration files/assets \u2502\n\u2502   \u2502   \u2514\u2500\u2500 [APP_ID]          # Example application flavour                    \u2502\n\u2502   \u2502       \u251C\u2500\u2500 assets        # Platform assets injected to ./platformAssets   \u2502\n\u2502   \u2502       \u251C\u2500\u2500 builds        # Platform files injected to ./platformBuilds    \u2502\n\u2502   \u2502       \u251C\u2500\u2500 fonts             # Folder for all custom fonts                \u2502\n\u2502   \u2502       \u251C\u2500\u2500 plugins           # Multi-platform plugins injections          \u2502\n\u2502   \u2502       \u2514\u2500\u2500 renative.json # Application flavour config                     \u2502\n\u2502   \u251C\u2500\u2500 platformAssets        # Generated cross-platform assets                \u2502\n\u2502   \u251C\u2500\u2500 platformBuilds        # Generated platform app projects                \u2502\n\u2502   \u251C\u2500\u2500 src                   # Source code files                              \u2502\n\u2502   \u251C\u2500\u2500 index.*.js            # Entry files                                    \u2502\n\u2502   \u2514\u2500\u2500 renative.json         # ReNative project configuration                 \u2502\n");












str+=(0,_logger.printIntoBox)('');
str+=(0,_logger.printBoxEnd)();
str+='\n';

data.confirmString=str;
};

var interactiveQuestion=function interactiveQuestion(results,bootstrapQuestions){var key,_loop,i,_args2=arguments;return _regenerator.default.async(function interactiveQuestion$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:key=_args2.length>2&&_args2[2]!==undefined?_args2[2]:'';if(!(
bootstrapQuestions!=null&&bootstrapQuestions.length)){_context2.next=10;break;}_loop=function _loop(
i){var _choicesObj$result$qK,_choicesObj$result$qK2;var q,choicesObj,qKey,inqQuestion,result,val;return _regenerator.default.async(function _loop$(_context){while(1){switch(_context.prev=_context.next){case 0:
q=bootstrapQuestions[i];
choicesObj={};
if(q.options){
q.options.forEach(function(opt){
choicesObj[opt.title]=opt;
});
}
qKey=key+"q"+i;

inqQuestion={
name:qKey,
type:q.type,
message:q.title,
choices:Object.keys(choicesObj)};_context.next=7;return _regenerator.default.awrap(


_inquirer.default.prompt(inqQuestion));case 7:result=_context.sent;
val=q.type==='list'?(_choicesObj$result$qK=choicesObj[result[qKey]])==null?void 0:_choicesObj$result$qK.value:result[qKey];
results[qKey]={
answer:result[qKey],
configProp:q.configProp,
value:val};if(!((_choicesObj$result$qK2=

choicesObj[result[qKey]])!=null&&_choicesObj$result$qK2.bootstrapQuestions)){_context.next=13;break;}_context.next=13;return _regenerator.default.awrap(

interactiveQuestion(
results,
choicesObj[result[qKey]].bootstrapQuestions,
qKey));case 13:case"end":return _context.stop();}}},null,null,null,Promise);};i=0;case 4:if(!(i<bootstrapQuestions.length)){_context2.next=10;break;}_context2.next=7;return _regenerator.default.awrap(_loop(i));case 7:i++;_context2.next=4;break;case 10:case"end":return _context2.stop();}}},null,null,null,Promise);};






var taskRnvNew=function taskRnvNew(c){var _renativeTemplateConf,_renativeTemplateConf2;var _c$program,args,ci,_await$inquirer$promp,confirmInRnvProject,_await$inquirer$promp2,confirmDeleteNodeModules,data,inputProjectName,inputProjectNameObj,_await$inquirer$promp3,confirm,inputAppTitle,inputAppID,inputVersion,answers,inputWorkspace,answer,customTemplate,selectedInputTemplate,_await$inquirer$promp4,inputTemplate,_await$inquirer$promp5,inputTemplateCustom,inputTemplateVersion,_await$inquirer$promp6,confirmAddTemplate,_c$files$workspace$co,renativeTemplateConfig,supportedPlatforms,inputSupportedPlatforms,_answer,renativeTemplateConfigExt,bootstrapQuestions,results,_await$inquirer$promp7,gitEnabled,_await$inquirer$promp8,_confirm,templates,config;return _regenerator.default.async(function taskRnvNew$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('taskRnvNew');_c$program=
c.program,args=_c$program.args,ci=_c$program.ci;if(!
(0,_fileutils.fsExistsSync)(c.paths.project.config)){_context3.next=10;break;}
(0,_logger.logWarning)("You are in ReNative project. Found: "+c.paths.project.config);_context3.next=6;return _regenerator.default.awrap(
_inquirer.default.prompt({
name:'confirmInRnvProject',
type:'confirm',
message:'Are you sure you want to continue?'}));case 6:_await$inquirer$promp=_context3.sent;confirmInRnvProject=_await$inquirer$promp.confirmInRnvProject;if(

confirmInRnvProject){_context3.next=10;break;}return _context3.abrupt("return",
Promise.reject('Cancelled'));case 10:if(!



(0,_fileutils.fsExistsSync)(c.paths.project.nodeModulesDir)){_context3.next=19;break;}
(0,_logger.logWarning)("Found node_modules directory at your location. If you continue it will be deleted: "+

c.paths.project.nodeModulesDir);_context3.next=14;return _regenerator.default.awrap(


_inquirer.default.prompt({
name:'confirmDeleteNodeModules',
type:'confirm',
message:'Are you sure you want to continue?'}));case 14:_await$inquirer$promp2=_context3.sent;confirmDeleteNodeModules=_await$inquirer$promp2.confirmDeleteNodeModules;if(!

confirmDeleteNodeModules){_context3.next=19;break;}_context3.next=19;return _regenerator.default.awrap(
(0,_fileutils.removeDirs)([c.paths.project.nodeModulesDir]));case 19:



data={
defaultVersion:'0.1.0',
defaultTemplate:'renative-template-hello-world',
defaultProjectName:'helloRenative',
defaultAppTitle:'Hello Renative',
defaultWorkspace:'rnv'};

data.optionPlatforms={};
data.optionTemplates={};
data.optionWorkspaces=(0,_workspace.getWorkspaceOptions)(c);if(!(







args[1]&&args[1]!=='')){_context3.next=27;break;}
inputProjectName=args[1];_context3.next=31;break;case 27:_context3.next=29;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'inputProjectName',
type:'input',
validate:function validate(value){return!!value;},
message:
"What's your project Name? (no spaces, folder based on ID will be created in this directory)"}));case 29:inputProjectNameObj=_context3.sent;

inputProjectName=inputProjectNameObj==null?void 0:inputProjectNameObj.inputProjectName;case 31:


data.projectName=inputProjectName;
c.paths.project.dir=_path.default.join(
_constants.CURRENT_DIR,
data.projectName.replace(/(\s+)/g,'_'));if(!

(0,_fileutils.fsExistsSync)(c.paths.project.dir)){_context3.next=42;break;}_context3.next=36;return _regenerator.default.awrap(
_inquirer.default.prompt({
type:'confirm',
name:'confirm',
message:"Folder "+
c.paths.project.dir+" already exists. RNV will override it. Continue?"}));case 36:_await$inquirer$promp3=_context3.sent;confirm=_await$inquirer$promp3.confirm;if(



confirm){_context3.next=40;break;}return _context3.abrupt("return",
Promise.reject('Cancelled by user'));case 40:_context3.next=42;return _regenerator.default.awrap(

(0,_fileutils.cleanFolder)(c.paths.project.dir));case 42:


(0,_fileutils.mkdirSync)(c.paths.project.dir);if(!(








args[2]&&
args[2]!==''&&
args[3]&&
args[3]!==''&&
args[4]&&
args[4]!=='')){_context3.next=49;break;}

inputAppTitle=args[2];
inputAppID=args[3];
inputVersion=args[4];_context3.next=55;break;case 49:_context3.next=51;return _regenerator.default.awrap(

_inquirer.default.prompt([
{
name:'inputAppTitle',
type:'input',
default:data.defaultAppTitle,
validate:function validate(val){return!!val||'Please enter a title';},
message:"What's your project Title?"},

{
name:'inputAppID',
type:'input',
default:function _default(){
data.appID="com.mycompany."+inputProjectName.
replace(/\s+/g,'').
toLowerCase();
return data.appID;
},
validate:function validate(id){return!!id.match(/[a-z]+\.[a-z0-9]+\.[a-z0-9]+/)||
'Please enter a valid appID (com.test.app)';},
message:"What's your App ID?"},

{
name:'inputVersion',
type:'input',
default:data.defaultVersion,
validate:function validate(v){return!!_semver.default.valid(_semver.default.coerce(v))||
'Please enter a valid semver version (1.0.0, 42.6.7.9.3-alpha, etc.)';},
message:"What's your Version?"}]));case 51:answers=_context3.sent;


inputAppTitle=answers==null?void 0:answers.inputAppTitle;
inputAppID=answers==null?void 0:answers.inputAppID;
inputVersion=answers==null?void 0:answers.inputVersion;case 55:if(!(






args[5]&&args[5]!=='')){_context3.next=59;break;}
inputWorkspace=args[5];_context3.next=63;break;case 59:_context3.next=61;return _regenerator.default.awrap(

_inquirer.default.prompt([
{
name:'inputWorkspace',
type:'list',
message:'What workspace to use?',
default:data.defaultWorkspace,
choices:data.optionWorkspaces.keysAsArray}]));case 61:answer=_context3.sent;



inputWorkspace=answer==null?void 0:answer.inputWorkspace;case 63:

data.optionWorkspaces.selectedOption=inputWorkspace;
c.runtime.selectedWorkspace=inputWorkspace;_context3.next=67;return _regenerator.default.awrap(

(0,_runtimeManager.updateRenativeConfigs)(c));case 67:
data.optionTemplates=(0,_templateManager.getTemplateOptions)(c);




customTemplate='Custom Template ...';

data.optionTemplates.keysAsArray.push(customTemplate);if(!(

args[6]&&args[6]!=='')){_context3.next=74;break;}
selectedInputTemplate=args[6];_context3.next=87;break;case 74:_context3.next=76;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'inputTemplate',
type:'list',
message:'What template to use?',
default:data.defaultTemplate,
choices:data.optionTemplates.keysAsArray}));case 76:_await$inquirer$promp4=_context3.sent;inputTemplate=_await$inquirer$promp4.inputTemplate;if(!(



inputTemplate===customTemplate)){_context3.next=86;break;}_context3.next=81;return _regenerator.default.awrap(
_inquirer.default.prompt({
name:'inputTemplateCustom',
type:'input',
message:'Type exact name of your template NPM package.'}));case 81:_await$inquirer$promp5=_context3.sent;inputTemplateCustom=_await$inquirer$promp5.inputTemplateCustom;

selectedInputTemplate=inputTemplateCustom;_context3.next=87;break;case 86:

selectedInputTemplate=inputTemplate;case 87:



data.optionTemplates.selectedOption=selectedInputTemplate;if(!(


args[7]&&args[7]!=='')){_context3.next=92;break;}
inputTemplateVersion=args[7];_context3.next=95;break;case 92:_context3.next=94;return _regenerator.default.awrap(

(0,_npmUtils.listAndSelectNpmVersion)(
c,
data.optionTemplates.selectedOption,
Object.keys(c.files.rnv.projectTemplates.config.projectTemplates)));case 94:inputTemplateVersion=_context3.sent;case 95:



data.optionTemplates.selectedVersion=inputTemplateVersion;_context3.next=98;return _regenerator.default.awrap(

(0,_exec.executeAsync)("npm i "+
selectedInputTemplate+"@"+inputTemplateVersion+" --no-save",
{
cwd:c.paths.project.dir}));case 98:if(



data.optionTemplates.keysAsArray.includes(selectedInputTemplate)){_context3.next=110;break;}_context3.next=101;return _regenerator.default.awrap(
_inquirer.default.prompt({
name:'confirmAddTemplate',
type:'confirm',
message:"Would you like to add "+(0,_logger.chalk)().white(
selectedInputTemplate)+" to your "+
c.runtime.selectedWorkspace+" workspace template list?"}));case 101:_await$inquirer$promp6=_context3.sent;confirmAddTemplate=_await$inquirer$promp6.confirmAddTemplate;if(!


confirmAddTemplate){_context3.next=110;break;}
if(!((_c$files$workspace$co=c.files.workspace.config)!=null&&_c$files$workspace$co.projectTemplates)){
c.files.workspace.config.projectTemplates={};
}
c.files.workspace.config.projectTemplates[selectedInputTemplate]={};
(0,_fileutils.writeObjectSync)(c.paths.workspace.config,c.files.workspace.config);_context3.next=109;return _regenerator.default.awrap(
(0,_runtimeManager.updateRenativeConfigs)(c));case 109:

(0,_logger.logInfo)("Updating "+c.paths.workspace.config+"...DONE");case 110:



renativeTemplateConfig=(0,_fileutils.readObjectSync)(
_path.default.join(
c.paths.project.dir,
'node_modules',
selectedInputTemplate,
'renative.template.json'));







supportedPlatforms=(renativeTemplateConfig==null?void 0:(_renativeTemplateConf=renativeTemplateConfig.defaults)==null?void 0:_renativeTemplateConf.supportedPlatforms)||[];if(!(

args[8]&&args[8]!=='')){_context3.next=116;break;}
inputSupportedPlatforms=args[8].split(',');_context3.next=120;break;case 116:_context3.next=118;return _regenerator.default.awrap(

_inquirer.default.prompt({
name:'inputSupportedPlatforms',
type:'checkbox',
pageSize:20,
message:'What platforms would you like to use?',
validate:function validate(val){return!!val.length||'Please select at least a platform';},
default:supportedPlatforms,
choices:supportedPlatforms}));case 118:_answer=_context3.sent;

inputSupportedPlatforms=_answer==null?void 0:_answer.inputSupportedPlatforms;case 120:





renativeTemplateConfigExt={};
bootstrapQuestions=renativeTemplateConfig==null?void 0:(_renativeTemplateConf2=renativeTemplateConfig.templateConfig)==null?void 0:_renativeTemplateConf2.bootstrapQuestions;
results={};_context3.next=125;return _regenerator.default.awrap(
interactiveQuestion(results,bootstrapQuestions));case 125:

Object.keys(results).forEach(function(k){
var objValue=results[k].value;

var targetKey=results[k].configProp.key;

if(targetKey){
(0,_lodash.default)(renativeTemplateConfigExt,targetKey,objValue);
}
});_context3.next=128;return _regenerator.default.awrap(





_inquirer.default.prompt({
name:'gitEnabled',
type:'confirm',
message:'Do you want to set-up git in your new project?'}));case 128:_await$inquirer$promp7=_context3.sent;gitEnabled=_await$inquirer$promp7.gitEnabled;






data=_objectSpread(_objectSpread({},
data),{},{
inputProjectName:inputProjectName,
inputAppTitle:inputAppTitle,
inputAppID:inputAppID,
inputVersion:inputVersion,
inputTemplate:selectedInputTemplate,
inputSupportedPlatforms:inputSupportedPlatforms,
inputWorkspace:inputWorkspace,
gitEnabled:gitEnabled});

data.optionPlatforms.selectedOptions=inputSupportedPlatforms;

_prepareProjectOverview(c,data);if(
ci){_context3.next=140;break;}_context3.next=136;return _regenerator.default.awrap(
_inquirer.default.prompt({
type:'confirm',
name:'confirm',
message:"\n"+data.confirmString+"\nIs all this correct?"}));case 136:_await$inquirer$promp8=_context3.sent;_confirm=_await$inquirer$promp8.confirm;if(


_confirm){_context3.next=140;break;}return _context3.abrupt("return");case 140:_context3.prev=140;_context3.next=143;return _regenerator.default.awrap(









_analytics.default.captureEvent({
type:'newProject',
template:selectedInputTemplate,
platforms:inputSupportedPlatforms}));case 143:_context3.next=148;break;case 145:_context3.prev=145;_context3.t0=_context3["catch"](140);


(0,_logger.logDebug)(_context3.t0);case 148:


c.paths.project.package=_path.default.join(c.paths.project.dir,'package.json');
c.paths.project.config=_path.default.join(c.paths.project.dir,_constants.RENATIVE_CONFIG_NAME);

data.packageName=data.appTitle.replace(/\s+/g,'-').toLowerCase();

templates={};

(0,_logger.logTask)("_generateProject:"+
data.optionTemplates.selectedOption+":"+
data.optionTemplates.selectedVersion,

(0,_logger.chalk)().grey);


templates[data.optionTemplates.selectedOption]={
version:data.optionTemplates.selectedVersion};


config=_objectSpread(_objectSpread(_objectSpread({},
renativeTemplateConfig),
renativeTemplateConfigExt),{},{
projectName:data.projectName,
workspaceID:data.optionWorkspaces.selectedOption,
paths:{
appConfigsDir:'./appConfigs',
entryDir:'./',
platformAssetsDir:'./platformAssets',
platformBuildsDir:'./platformBuilds'},

defaults:{
title:data.appTitle,
id:data.appID,
supportedPlatforms:data.optionPlatforms.selectedOptions},

templates:templates,
currentTemplate:data.optionTemplates.selectedOption,
isNew:true,
isMonorepo:false});

delete config.templateConfig;

(0,_fileutils.writeFileSync)(c.paths.project.config,config);if(!

data.gitEnabled){_context3.next=162;break;}_context3.next=160;return _regenerator.default.awrap(
(0,_projectManager.checkAndCreateGitignore)(c));case 160:_context3.next=162;return _regenerator.default.awrap(
(0,_gitUtils.configureGit)(c));case 162:


(0,_logger.logSuccess)("Your project is ready! navigate to project "+
(0,_logger.chalk)().white("cd "+
data.projectName)+" and run "+
(0,_logger.chalk)().white('rnv run')+" to see magic happen!");case 163:case"end":return _context3.stop();}}},null,null,[[140,145]],Promise);};exports.taskRnvNew=taskRnvNew;var _default2=



{
description:'Create new ReNative project',
fn:taskRnvNew,
task:'new',
params:_constants.PARAMS.withBase(),
platforms:[],
isGlobalScope:true};exports.default=_default2;
//# sourceMappingURL=task.rnv.new.js.map