diff --git a/lib/helper.js b/lib/helper.js index 4269c36..cc96bc5 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -3,6 +3,8 @@ const fs = require("fs"); const figlet = require("figlet"); const chalk = require("chalk"); const path = "./.nc.config.js"; +const Table = require("cli-table2"); +pretty = require('pretty-cli')(); /** * Display text as CLI logo @@ -21,8 +23,8 @@ let logofied = text => { logger(err, true); return; } - console.log(chalk.blue(data)); - console.log( + pretty.log(chalk.blue(data)); + pretty.log( chalk.green("Sustainable Computing Research Group") + chalk.blue(" (SCoRe)") ); @@ -30,16 +32,124 @@ let logofied = text => { ); }; +let tablerForArray = data => { + if (!Array.isArray(data)) { + return ""; + } + let finalTable = new Table({ + chars: { + top: "═", + "top-mid": "╤", + "top-left": "╔", + "top-right": "╗", + bottom: "═", + "bottom-mid": "╧", + "bottom-left": "╚", + "bottom-right": "╝", + left: "║", + "left-mid": "╟", + mid: "─", + "mid-mid": "┼", + right: "║", + "right-mid": "╢", + middle: "│" + } + }); + + let firstItem = data[0]; + let head = ["#"]; + if (typeof firstItem == "object") { + for (prop in firstItem) head.push(prop); + finalTable.push(head); + // making the body + data.forEach((item, index) => { + let temp = [index]; + for (prop in item) { + temp.push(item[prop]); + } + finalTable.push(temp); + }); + } else { + head.push(""); + finalTable.push(head); + data.forEach((item, index) => { + finalTable.push([index, item]); + }); + } + // console.log(finalTable); + return finalTable.toString(); +}; + +let tablerForObject = data => { + let finalTable = new Table({ + chars: { + top: "═", + "top-mid": "╤", + "top-left": "╔", + "top-right": "╗", + bottom: "═", + "bottom-mid": "╧", + "bottom-left": "╚", + "bottom-right": "╝", + left: "║", + "left-mid": "╟", + mid: "─", + "mid-mid": "┼", + right: "║", + "right-mid": "╢", + middle: "│" + } + }); + let head = []; + let body = []; + + // making the head of the table + for (prop in data) head.push(prop); + finalTable.push(head); + + // making the body of the table + for (prop in data) { + if (typeof data[prop] == "object") { + if (Array.isArray(data[prop])) body.push(tablerForArray(data[prop])); + else { + body.push(tablerForObject(data[prop])); + } + } else { + body.push(data[prop]); + } + } + finalTable.push(body); + return finalTable.toString(); +}; + +let tabler = data => { + if (Array.isArray(data)) { + return tablerForArray(data); + } else if (typeof data == "object") { + return tablerForObject(data); + } else { + return data; + } +}; + /** * Logs the values with colors according to the type * @param {*} data * @param {*} error */ -let logger = (data, error) => { + +let logger = (data, error, table) => { + let message = JSON.parse(data); + if (table) { + message = tabler(message); + } else { + message = JSON.stringify(message); + } + if (error) { - console.log(chalk.red(`${JSON.stringify(data, null, 2)}`)); + pretty.error(chalk.red(chalk.bgWhite(message))); } else { - console.log(chalk.green(`${JSON.stringify(data, null, 2)}`)); + pretty.log(chalk.green(chalk.bgWhite(message))); } }; @@ -52,8 +162,9 @@ let checkPlugin = plugin => { detectInstalled(plugin).then(exists => { if (exists) { resolve(true); + } else { + resolve(false); } - resolve(false); }); }); }; diff --git a/lib/services.js b/lib/services.js index 499f371..72c7cae 100644 --- a/lib/services.js +++ b/lib/services.js @@ -1,6 +1,8 @@ + const Core = require("./core"); const Init = require("./init"); const { logger, logofied } = require("./helper"); +const chalk = require("chalk"); //import services const Compute = require("./compute"); @@ -45,19 +47,13 @@ function Services(program, spinner) { } }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; case "list": compute.listInstances({}, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "stop": @@ -66,10 +62,7 @@ function Services(program, spinner) { InstanceIds: [program.id] }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -79,10 +72,7 @@ function Services(program, spinner) { InstanceIds: [program.id] }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -92,10 +82,7 @@ function Services(program, spinner) { InstanceIds: [program.id] }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -105,16 +92,17 @@ function Services(program, spinner) { InstanceIds: [program.id] }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; default: - throw new Error("Please select a valid command for Compute"); + throw new Error( + chalk.bgYellow( + chalk.black("Please select a valid command for Compute") + ) + ); break; } } @@ -129,24 +117,18 @@ function Services(program, spinner) { ACL: "public-read" }, (error, data) => { - logger(data, null); + logger(data, error, program.table); } ); break; case "list": storage.listStorage({}, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "upload": storage.uploadToStorage({}, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "delete": @@ -155,16 +137,17 @@ function Services(program, spinner) { Bucket: program.name }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; default: - throw new Error("Please select a valid command for Storage"); + throw new Error( + chalk.bgYellow( + chalk.black("Please select a valid command for Storage") + ) + ); break; } } @@ -174,39 +157,31 @@ function Services(program, spinner) { switch (program.database) { case "create": database.createNewItem(option, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "query": database.query(option, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "delete": database.delete(option, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "update": database.updateItem(option, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; default: - throw new Error("Please select a valid command for Database"); + throw new Error( + chalk.bgYellow( + chalk.black("Please select a valid command for Database") + ) + ); break; } } @@ -220,10 +195,7 @@ function Services(program, spinner) { GroupName: program.name }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -233,10 +205,7 @@ function Services(program, spinner) { GroupName: program.name }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -247,10 +216,7 @@ function Services(program, spinner) { PolicyArn: `arn:aws:iam::aws:policy/${program.arName}` }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -261,16 +227,15 @@ function Services(program, spinner) { PolicyArn: `arn:aws:iam::aws:policy/${program.arName}` }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; default: - throw new Error("Please select a valid command for IAM"); + throw new Error( + chalk.bgYellow(chalk.black("Please select a valid command for IAM")) + ); break; } } @@ -293,19 +258,13 @@ function Services(program, spinner) { LoadBalancerName: program.name }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; case "list": network.list({}, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "delete": @@ -314,10 +273,7 @@ function Services(program, spinner) { LoadBalancerName: program.name }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -333,10 +289,7 @@ function Services(program, spinner) { ] }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -351,10 +304,7 @@ function Services(program, spinner) { ] }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; @@ -362,42 +312,34 @@ function Services(program, spinner) { network.createZone( { CallerReference: program.cr, Name: program.name }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; case "listz": network.listZones({}, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; case "deletez": network.deleteZone( { Id: `/hostedzone/${program.id}` }, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); } ); break; case "record": network.changeRecordSets({}, (error, data) => { - if (error) { - logger(error, true); - } - logger(data, null); + logger(data, error, program.table); }); break; default: - throw new Error("Please select a valid command for Database"); + throw new Error( + chalk.bgYellow( + chalk.black("Please select a valid command for Database") + ) + ); break; } } diff --git a/package.json b/package.json index 3644bfd..7962019 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "devDependencies": { "husky": "^2.3.0", "jest": "^24.8.0", - "pretty-quick": "^1.11.0" + "pretty-quick": "^1.11.0", + "pretty-cli":"^0.0.14" }, "husky": { "hooks": {