Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a Chalk Extra (LOGS) #10

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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");
});
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});

Expand Down
55 changes: 55 additions & 0 deletions src/util/chalkExtra.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion src/util/crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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'));
}

/**
Expand Down
27 changes: 15 additions & 12 deletions src/util/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const chalk = require('chalk');

const { Log } = require('./chalkExtra');

const log = console.log;
const title = chalk.yellow;
const body = chalk.green;
Expand All @@ -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');
}

/**
Expand All @@ -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');
});
}

Expand All @@ -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');
});
}

Expand All @@ -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')
});
}

Expand All @@ -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');
});
}

Expand Down
12 changes: 7 additions & 5 deletions src/util/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Native import
const inquirer = require('inquirer');

const { Log } = require('./chalkExtra');

inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));

/**
Expand All @@ -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) => {
Expand All @@ -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
}])
Expand All @@ -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
}])
Expand All @@ -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
}]);
}
Expand Down