mycustom-cli-alerts
Version:
cross platform cli alerts with colors & colored symbols for success , info, warning, Error.
63 lines (48 loc) • 1.84 kB
JavaScript
import chalk from 'chalk';
import logSymbols from 'log-symbols';
console.clear();
export const notify = options => {
//define options
const defaultOptions = {
emojiType: 'default',
notifyType: '',
msg: 'Please provide a message'
};
// merge the defualt options object with user given options object
const opts = {...defaultOptions, ...options};
// destructure the merged object to have access to all the properties
const {emojiType, notifyType, msg} = opts;
// fallback assignment
const printName = notifyType ? notifyType : emojiType;
// Symbol mapping (uses logSymbols only for known types)
const symbolMapping = {
success: logSymbols.success,
error: logSymbols.error,
warning: logSymbols.warning,
info: logSymbols.info,
default: 'ℹ️' // Fallback emoji for unknown types
};
// Color mapping for text styling
const colorMapping = {
success: chalk.green,
error: chalk.red.bold,
warning: chalk.yellow.bold,
info: chalk.blue.bold,
default: chalk.magenta // Custom/default styling
};
// Get corresponding symbol & color (or default fallback)
const symbol = symbolMapping[emojiType] || symbolMapping.default;
const color = colorMapping[emojiType] || colorMapping.default;
// Print the message dynamically
console.log(`\n${symbol} ${color.inverse(` ${printName.toUpperCase()} `)} ${color(msg)}\n`);
};
/*
define a function that takes an object
inside the function:
define an defualt object with properties
merge the defualt object with user given object into a new object using spread operator
if user inputs an object , it will overwrite the inside object
if user doent give an input,inside object's values will be used to log messages
destructure the merged object to have all the properties
*/