diff --git a/src/index.js b/src/index.js index 752d2bc..364e23a 100755 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,8 @@ const prompt = require('./util/prompt'); const showProfile = require('./util/profile'); const localStorage = require('./util/localStorage'); +const { Log } = require('./util/chalkExtra'); + const BOOKMARK_TAG = ' ![BOOKMARK]'; let articles; @@ -66,7 +68,7 @@ const postPrompt = () => { } openLink(answers); }).catch(err => { - console.log('unexpected error occurred :( - ', err); + Log(('unexpected error occurred :( - ', err), "error"); }); }else{ // remove the ![BOOKMARK] tag from title @@ -93,11 +95,11 @@ const postBookmarkPrompt = () => { opn(bookmark.find(data => data.title === answer.title).link); process.exit(); }).catch(err => { - console.log(err); + Log(err, "error"); }); }).catch(err => { - console.log(err); + Log(err, "error"); }); } @@ -142,7 +144,7 @@ const showAuthorProfile = (username) => { crawler.fetchAuthorProfile(username).then((profileInfo) => { countdown.stop(); if (!profileInfo.name) { - console.error("😱 User not found. Please try again."); + Log("😱 User not found. Please try again.", "error"); process.exit(1); } showProfile(profileInfo); @@ -234,8 +236,8 @@ program }) // error on unknown commands -program.on('command:*', function () { - console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' ')); +program.on('command:*', function () { + Log('Invalid command: %s\nSee --help for a list of available commands. ' + program.args.join(' ') ,"error"); process.exit(1); }); diff --git a/src/util/chalkExtra.js b/src/util/chalkExtra.js new file mode 100644 index 0000000..c0fb3fa --- /dev/null +++ b/src/util/chalkExtra.js @@ -0,0 +1,55 @@ +/* + * This File will Handle a set of functions for remove the complexity or verbosity of + ** Console.log( chalk.background.foreground( "Long String Text" ) ); + */ + +'use strict'; + +const chalk = require('chalk'); + +// Bootstrap Colors Template +const primary = "#007bff", secondary = '#6c757d', success = '#28a745', info = '#17a2b8', warning = '#ffc107', danger = '#dc3545', light = '#f8f9fa', dark = '#343a40'; + +// Types of Messages +const primaryMsg = chalk.bold.bgHex(primary).hex(light); +const secondaryMsg = chalk.bold.bgHex(secondary).hex(light); +const successMsg = chalk.bold.bgHex(success).hex(light); +const infoMsg = chalk.bold.bgHex(info).hex(light); +const errorMsg = chalk.bold.bgHex(danger).hex(light); +const warningMsg = chalk.bgHex(warning).hex(dark); +const darkMsg = chalk.bold.bgHex(dark).hex(light); + +/** + * This is a function to display logs in terminal. + * @param {string} text - Text to display in the terminal + * @param {string} type - Define the type of log + * @returns {null} null + */ + +function Log(text, type) { + switch(type.toLowerCase()) { + case "primary": + console.log(primaryMsg(text)); + break; + case "secondary": + console.log(secondaryMsg(text)); + break; + case "success": + console.log(successMsg("Success:", text)); + break; + case "info": + console.info(infoMsg("Info:", text)); + break; + case "error": + console.error(errorMsg("Error:", text)); + break; + case "warning": + console.log(warningMsg("Warning:", text)); + break; + case "dark": + console.log(darkMsg(text)); + break; + } +} + +exports.Log = Log; \ No newline at end of file diff --git a/src/util/crawler.js b/src/util/crawler.js index 4197e1b..e72b0a6 100644 --- a/src/util/crawler.js +++ b/src/util/crawler.js @@ -9,6 +9,8 @@ const ProxyAgent = require('proxy-agent'); const http = require('http'); const https = require('https'); +const { Log } = require('./chalkExtra'); + //Global Variable const xray = Xray({ filters: { @@ -144,7 +146,7 @@ const fetchAuthorProfile = (username) => { */ const fetchArticle = (url) => { - return xray(url, '#article-body | trim').then(data => console.log(data)); + return xray(url, '#article-body | trim').then(data => Log(data, 'dark')); } /** diff --git a/src/util/profile.js b/src/util/profile.js index fc6425e..47a74e1 100644 --- a/src/util/profile.js +++ b/src/util/profile.js @@ -2,6 +2,8 @@ const chalk = require('chalk'); +const { Log } = require('./chalkExtra'); + const log = console.log; const title = chalk.yellow; const body = chalk.green; @@ -13,10 +15,10 @@ const body = chalk.green; */ const showNameAndDesc = (profile) => { - log(title('name')) - log(body(profile.name)); - log(title('\ndescription')) - log(body(`${profile.desc}\n`)); + Log('name', 'primary'); + Log(profile.name, 'secondary'); + Log('\ndescription', 'primary'); + Log(`${profile.desc}\n`, 'secondary'); } /** @@ -27,8 +29,8 @@ const showNameAndDesc = (profile) => { const showAuthorInfo = (profile) => { profile.field.forEach((field, index) => { - log(title(field)); - log(body(`${profile.value[index]}\n`)); + Log(field, 'primary'); + Log(`${profile.value[index]}\n`, 'secondary'); }); } @@ -39,9 +41,9 @@ const showAuthorInfo = (profile) => { */ const showLinks = (profile) => { - log(title('links')); + Log('links', 'primary'); profile.links.forEach(link => { - log(body(link)); + Log(link, 'secondary'); }); } @@ -53,9 +55,10 @@ const showLinks = (profile) => { const showSidebarDetails = (profile) => { log(''); + Log('', 'dark'); profile.sidebarHeader.forEach((header, index) => { - log(title(header)); - log(body(`${profile.sidebarBody[index]}\n`)); + Log(header, 'primary'); + Log(`${profile.sidebarBody[index]}\n`, 'secondary') }); } @@ -66,9 +69,9 @@ const showSidebarDetails = (profile) => { */ const showStats = (profile) => { - log(title('stats')); + Log('stats', 'primary'); profile.stats.forEach(stat => { - log(body(stat)); + Log(stat, 'secondary'); }); } diff --git a/src/util/prompt.js b/src/util/prompt.js index 0cd2af9..85deec3 100644 --- a/src/util/prompt.js +++ b/src/util/prompt.js @@ -3,6 +3,8 @@ // Native import const inquirer = require('inquirer'); +const { Log } = require('./chalkExtra'); + inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt')); /** @@ -15,7 +17,7 @@ const searchTags = (tags) => { return inquirer.prompt([{ type: 'autocomplete', name: 'tag', - message: '🕵🏻‍♂️ Search popular tags:', + message: Log('🕵🏻‍♂️ Search popular tags:', 'info'), pageSize: 4, source: function (answers, input) { return new Promise((resolve) => { @@ -34,14 +36,14 @@ const searchTags = (tags) => { const showPosts = (titles) => { if(titles.length === 0){ - console.error("😱 No posts found. Please try again."); + Log("😱 No posts found. Please try again.", "error"); process.exit(1); } return inquirer.prompt([{ type: 'rawlist', name: 'title', - message: '📚 Here are your posts:', + message: Log('📚 Here are your posts:', 'success'), choices: titles, paginated: true }]) @@ -57,7 +59,7 @@ const selectTimline = () => { return inquirer.prompt([{ type: 'list', name: 'timeline', - message: '📆 Please choose the timeline:', + message: Log('📆 Please choose the timeline:', "info"), choices: ["week","month","year","infinity"], paginated: true }]) @@ -73,7 +75,7 @@ const postOperation = (choices) => { return inquirer.prompt([{ type: 'list', name: 'postOperation', - message: 'What do we do with this post : ', + message: Log('What do we do with this post : ', "info"), choices: choices }]); }