"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.runCli=runCli;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _scripts=require("./scripts");
var _utils=require("@corenode/utils");
var _genCli=_interopRequireDefault(require("./genCli"));const helpers=process.runtime.helpers;

let optionsMap=[
{
option:"clearBefore",
description:"Clear console before print",
alias:["cb"],
type:"boolean",
exec:()=>{
console.clear();
}}];



let commandMap=[
{
command:"modules [action] [module]",
description:"Manage modules & plugins",
exec:argv=>{
switch(argv.action){
case"install":{
break;
}
case"remove":{
break;
}
default:{
const allModules=process.runtime.modules.getLoadedModules();
const pt=new _utils.prettyTable;

let headers=["module","_runtimed","directory"];
let rows=[];

(0,_utils.objectToArrayMap)(allModules).forEach(_module=>{var _module$value$interna;
const isRuntimed=(_module$value$interna=_module.value.internal)!==null&&_module$value$interna!==void 0?_module$value$interna:false;
const key=_module.key;
const cwd=_module.value.loader;

rows.push([`${isRuntimed?`⚙️ `:`📦 `} ${key}`,`${isRuntimed}`,cwd]);
});

pt.create(headers,rows);
pt.print();
break;
}}

}},

{
command:"add [package] [dir]",
description:"Install an package",
exec:argv=>{
console.log(`⛔️ Nothing to install!`);
}},

{
command:"version",
description:"Manage project version",
exec:argv=>{
let bumps=[];
const types=["bump-mayor","bump-minor","bump-patch"];
types.forEach(bump=>{
const parsedBump=bump.split("-")[1];
if(argv[bump]){
if(!parsedBump){
return bumps.push(bump);
}
bumps.push(parsedBump);
}
});

if(bumps.length>0){
helpers.bumpVersion(bumps);
if(argv.sync){
helpers.syncAllPackagesVersions();
}
}else{var _projectPkg$name;
const engineVersion=helpers.getVersion({engine:true});
const proyectVersion=helpers.getVersion();

const projectPkg=helpers.getRootPackage();
const pt=new _utils.prettyTable;

let headers=["","\uD83C\uDFF7  Version","\uD83C\uDFE0  Directory"];
let rows=[];

if(argv.engine){
rows.push(["Corenode\u2122",`v${engineVersion}${helpers.isCorenodeProject()?"@local":""}`,__dirname]);
}

proyectVersion?rows.push([`📦  ${(_projectPkg$name=projectPkg.name)!==null&&_projectPkg$name!==void 0?_projectPkg$name:"Unnamed"}`,`v${proyectVersion}`,process.cwd()]):console.log("\uD83C\uDFF7  Version not available");

if(rows.length>0){
pt.create(headers,rows);
pt.print();
}
}
}},

{
command:"publish",
description:"Publish this current project",
exec:argv=>{
(0,_scripts.publishProject)(argv);
}},

{
command:"build",
description:"Build project with builtin builder",
exec:argv=>{
require("@corenode/builder").default({
cliui:argv.silent?false:true}).

then(()=>{
console.log(`✅  DONE`);
});
}},

{
command:"bootstrap",
description:"Bootstrap all packages",
exec:argv=>{
(0,_scripts.bootstrapProject)(argv).
then(res=>{
console.log(`\n✅ DONE\nAll packages bootstraped > ${res}\n`);
});
}},

{
command:"sync [package]",
description:"Sync project versions",
exec:argv=>{
console.log(`🔄 Syncing versions...`);
if(!argv.package){
return helpers.syncAllPackagesVersions();
}
return helpers.syncPackageVersionFromName(argv.package,argv.write);
}},

{
command:"changelogs",
description:"Show the changelogs of this project from last tag",
exec:function(){var _exec=(0,_asyncToGenerator2.default)(function*(argv){
const changes=yield(0,_scripts.getChangelogs)(helpers.getGit(),argv.to,argv.from);
console.log(changes);
});function exec(_x){return _exec.apply(this,arguments)}return exec}()},

{
command:"exec",
description:"Run builtIn functions on cli mode",
exec:argv=>{
const helpers=require("corenode/dist/helpers");const _require=
require("@corenode/utils"),verbosity=_require.verbosity;

const args=argv["_"];
const fn=args[1];
const context=args.slice(1,args.length);

try{
if(typeof helpers[fn]=="function"){
const _fn=helpers[fn](...context);
verbosity.options({method:`CLI > [${fn}()]`}).log(`>> ${JSON.stringify(_fn)}`);
}else{
console.error(`${fn}() is not an function`);
}
}catch(error){
console.error(error);
}
}},

{
command:"env",
description:"Show current runtime enviroment",
exec:()=>{
console.log(global._env);
}}];



function runCli(){
(0,_genCli.default)({
options:optionsMap,
commands:commandMap});

}

runCli();