var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.PIPES=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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 _platform=_interopRequireDefault(require("./platform"));
var _buildHooks=require("../projectTools/buildHooks");
var _logger=require("../systemTools/logger");
var _projectParser=require("../projectTools/projectParser");
var _configParser=require("../configTools/configParser");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(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}

var CONFIGURE='configure';
var SWITCH='switch';
var CREATE='create';
var NEW='new';

var PIPES={
APP_CONFIGURE_BEFORE:'configure:before',
APP_CONFIGURE_AFTER:'configure:after',
APP_SWITCH_BEFORE:'switch:before',
APP_SWITCH_AFTER:'switch:after'};exports.PIPES=PIPES;


var highlight=_chalk.default.green;





var run=function run(c){
(0,_common.logTask)('run');

switch(c.command){
case NEW:
return _runCreate(c);
case CONFIGURE:
return _runConfigure(c);
case SWITCH:
return _runSwitch(c);}


switch(c.subCommand){
case CONFIGURE:
(0,_common.logWarning)("$ "+_chalk.default.red('$ rnv app configure')+" is deprecated. Use "+_chalk.default.green('$ rnv configure')+" instead");
return _runConfigure(c);














default:
return Promise.reject("cli:app: Sub-Command "+c.subCommand+" not supported");}

};





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

(0,_buildHooks.executePipe)(c,PIPES.APP_CONFIGURE_BEFORE).
then(function(){return _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(0,_buildHooks.executePipe)(c,PIPES.APP_CONFIGURE_AFTER);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};

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

(0,_buildHooks.executePipe)(c,PIPES.APP_SWITCH_AFTER).

then(function(){return(0,_projectParser.copyRuntimeAssets)(c);}).
then(function(){return(0,_projectParser.copySharedPlatforms)(c);}).
then(function(){return(0,_configParser.generateRuntimeConfig)(c);}).
then(function(){return(0,_buildHooks.executePipe)(c,PIPES.APP_SWITCH_AFTER);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};

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 _runCreate=function _runCreate(c){var args,data,inputProjectName,inputProjectNameObj,_ref,inputAppTitle,inputAppID,inputVersion,inputTemplate,inputSupportedPlatforms,_ref2,confirm;return _regenerator.default.async(function _runCreate$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_common.logTask)('_runCreate');
args=c.program.args;

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

data.optionPlatforms=(0,_prompt.generateOptions)(_constants.SUPPORTED_PLATFORMS,true);
data.optionTemplates=(0,_templateTools.getTemplateOptions)(c);if(!(




args[1]&&args[1]!=='')){_context.next=9;break;}
inputProjectName=args[1];_context.next=13;break;case 9:_context.next=11;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 11:inputProjectNameObj=_context.sent;

inputProjectName=inputProjectNameObj.inputProjectName;case 13:_context.next=15;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?'},
{
name:'inputTemplate',
type:'list',
message:'What template to use?',
default:data.defaultTemplate,
choices:data.optionTemplates.keysAsArray},
{
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:data.optionPlatforms.keysAsArray,
choices:data.optionPlatforms.keysAsArray}]));case 15:_ref=_context.sent;inputAppTitle=_ref.inputAppTitle;inputAppID=_ref.inputAppID;inputVersion=_ref.inputVersion;inputTemplate=_ref.inputTemplate;inputSupportedPlatforms=_ref.inputSupportedPlatforms;



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


data.optionTemplates.selectedOption=inputTemplate;
data.optionPlatforms.selectedOptions=inputSupportedPlatforms;
_prepareProjectOverview(c,data);_context.next=27;return _regenerator.default.awrap(

_inquirer.default.prompt({
type:'confirm',
name:'confirm',
message:"\n"+data.confirmString+"\nIs all this correct?"}));case 27:_ref2=_context.sent;confirm=_ref2.confirm;if(!


confirm){_context.next=32;break;}_context.next=32;return _regenerator.default.awrap(
_generateProject(c,data));case 32:case"end":return _context.stop();}}});};



var _generateProject=function _generateProject(c,data){
(0,_common.logTask)('_generateProject');

var base=_path.default.resolve('.');

c.paths.project.dir=_path.default.join(base,data.projectName.replace(/(\s+)/g,'_'));
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();

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

var templates={};


return(0,_exec.executeAsync)(c,"npm show "+data.optionTemplates.selectedOption+" version").then(function(v){
(0,_common.logTask)("_generateProject:"+data.optionTemplates.selectedOption+":"+v,_chalk.default.grey);

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


var config={
projectName:data.projectName,
paths:{
globalConfigDir:'~/.rnv',
appConfigsDir:'./appConfigs',
platformTemplatesDir:'RNV_HOME/platformTemplates',
entryDir:'./',
platformAssetsDir:'./platformAssets',
platformBuildsDir:'./platformBuilds',
projectConfigDir:'./projectConfig'},

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

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


(0,_fileutils.writeObjectSync)(c.paths.project.config,config);

(0,_common.logSuccess)("Your project is ready! navigate to project "+
_chalk.default.white("cd "+data.projectName)+" and run "+_chalk.default.white(
'rnv run -p web')+" to see magic happen!");


});
};

var _prepareProjectOverview=function _prepareProjectOverview(c,data){
data.projectName=data.inputProjectName;
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 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)("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(data.optionTemplates.selectedOption),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+=_chalk.default.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       \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 projectConfigs        # Project configuration files/assets             \u2502\n\u2502   \u2502   \u251C\u2500\u2500 fonts             # Folder for all custom fonts                    \u2502\n\u2502   \u2502   \u251C\u2500\u2500 builds            # platformBuilds/* injections                    \u2502\n\u2502   \u2502   \u2514\u2500\u2500 plugins           # Multi-platform plugins injections              \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 _checkAndCreatePlatforms=function _checkAndCreatePlatforms(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("_checkAndCreatePlatforms:"+platform);

if(!_fs.default.existsSync(c.paths.project.builds.dir)){
(0,_common.logWarning)('Platforms not created yet. creating them for you...');
(0,_platform.default)((0,_common.spawnCommand)(c,{
subCommand:'configure',
program:{appConfig:c.runtime.appId,platform:platform}})).

then(function(){return resolve();}).
catch(function(e){return reject(e);});

return;
}
if(platform){
var appFolder=(0,_common.getAppFolder)(c,platform);
if(!_fs.default.existsSync(appFolder)){
(0,_common.logWarning)("Platform "+platform+" not created yet. creating them for you...");
(0,_platform.default)((0,_common.spawnCommand)(c,{
subCommand:'configure',
program:{appConfig:c.runtime.appId,platform:platform}})).

then(function(){return resolve();}).
catch(function(e){return reject(e);});

return;
}
}else{var
platforms=c.buildConfig.platforms;
var cmds=[];
if(!platforms){
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;
}

Object.keys(platforms).forEach(function(k){
if(!_fs.default.existsSync(k)){
(0,_common.logWarning)("Platform "+k+" not created yet. creating one for you...");
cmds.push((0,_platform.default)((0,_common.spawnCommand)(c,{
subCommand:'configure',
program:{appConfig:c.runtime.appId,platform:platform}})));

}
});

Promise.all(cmds).
then(function(){return resolve();}).
catch(function(e){return reject(e);});

return;
}
resolve();
});};

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();
});};var _default2=



run;exports.default=_default2;