Colorize Console Output

Adds special characters to text to print in color in the console (combined with console.log()).

  • Use template literals and special characters to add the appropriate color code to the string output.
  • For background colors, add a special character that resets the background color at the end of the string.
const colorize \= (...args) \=> ({
  black: \`\\x1b\[30m${args.join(' ')}\`,
  red: \`\\x1b\[31m${args.join(' ')}\`,
  green: \`\\x1b\[32m${args.join(' ')}\`,
  yellow: \`\\x1b\[33m${args.join(' ')}\`,
  blue: \`\\x1b\[34m${args.join(' ')}\`,
  magenta: \`\\x1b\[35m${args.join(' ')}\`,
  cyan: \`\\x1b\[36m${args.join(' ')}\`,
  white: \`\\x1b\[37m${args.join(' ')}\`,
  bgBlack: \`\\x1b\[40m${args.join(' ')}\\x1b\[0m\`,
  bgRed: \`\\x1b\[41m${args.join(' ')}\\x1b\[0m\`,
  bgGreen: \`\\x1b\[42m${args.join(' ')}\\x1b\[0m\`,
  bgYellow: \`\\x1b\[43m${args.join(' ')}\\x1b\[0m\`,
  bgBlue: \`\\x1b\[44m${args.join(' ')}\\x1b\[0m\`,
  bgMagenta: \`\\x1b\[45m${args.join(' ')}\\x1b\[0m\`,
  bgCyan: \`\\x1b\[46m${args.join(' ')}\\x1b\[0m\`,
  bgWhite: \`\\x1b\[47m${args.join(' ')}\\x1b\[0m\`
});

console.log(colorize('foo').red); // 'foo' (red letters)
console.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background)
console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite);
// 'foo bar' (first word in yellow letters, second word in green letters, white background for both)
Colors.ts
A helper file with a few functions to make it easier to color console output.
export enum Colors {
  black = '\x1b[30m',
  red = '\x1b[31m',
  green = '\x1b[32m',
  yellow = '\x1b[33m',
  blue = '\x1b[34m',
  magenta = '\x1b[35m',
  cyan = '\x1b[36m',
  white = '\x1b[37m',
  bgBlack = '\x1b[40m',
  bgRed = '\x1b[41m',
  bgGreen = '\x1b[42m',
  bgYellow = '\x1b[43m',
  bgBlue = '\x1b[44m',
  bgMagenta = '\x1b[45m',
  bgCyan = '\x1b[46m',
  bgWhite = '\x1b[47m'
}

export const fg = {
  black: (...args: string[]) => `${Colors.black}${args.join(' ')}`,
  red: (...args: string[]) => `${Colors.red}${args.join(' ')}`,
  green: (...args: string[]) => `${Colors.green}${args.join(' ')}`,
  yellow: (...args: string[]) => `${Colors.yellow}${args.join(' ')}`,
  blue: (...args: string[]) => `${Colors.blue}${args.join(' ')}`,
  magenta: (...args: string[]) => `${Colors.magenta}${args.join(' ')}`,
  cyan: (...args: string[]) => `${Colors.cyan}${args.join(' ')}`,
  white: (...args: string[]) => `${Colors.white}${args.join(' ')}`
}

export const bg = {
  black: (...args: string[]) => `${Colors.bgBlack}${args.join(' ')}\x1b[0m`,
  red: (...args: string[]) => `${Colors.bgRed}${args.join(' ')}\x1b[0m`,
  green: (...args: string[]) => `${Colors.bgGreen}${args.join(' ')}\x1b[0m`,
  yellow: (...args: string[]) => `${Colors.bgYellow}${args.join(' ')}\x1b[0m`,
  blue: (...args: string[]) => `${Colors.bgBlue}${args.join(' ')}\x1b[0m`,
  magenta: (...args: string[]) => `${Colors.bgMagenta}${args.join(' ')}\x1b[0m`,
  cyan: (...args: string[]) => `${Colors.bgCyan}${args.join(' ')}\x1b[0m`,
  white: (...args: string[]) => `${Colors.bgWhite}${args.join(' ')}\x1b[0m`
}
Create Directory if not Found

Creates a directory, if it does not exist.

Use fs.existsSync() to check if the directory exists, fs.mkdirSync() to create it.

const fs = require('fs')

const createDirIfNotExists = dir =>
  !fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined

createDirIfNotExists('test')
// creates the directory 'test', if it doesn't exist
import { existsSync, mkdirSync } from 'fs'

const createDirIfNotExists = (dir: string) =>
  !existsSync(dir) ? mkdirSync(dir) : undefined

createDirIfNotExists('test')
// creates the directory 'test', if it doesn't exist
Get All Files

Gets all files, directories, sub-files, and sub-directories.

I personally prefer to use fs-extra for my disk operations, but regular ol’ fs works as well; you just need to change the import and refactor the async/await calls into callbacks.

With fs-extra

import { readdir, stat } from 'fs-extra'
import { join } from 'path'

/**
 * Gets all of the files from the given directory by recursively calling itself
 * should one of the files be a directory.
 *
 * @param dirPath The root directory to retrieve files from.
 * @param files An array of files that have been found so far.
 * @returns A string array of all files & folders in the directory.
 */
async function getAllFiles(
  dirPath: string,
  files: string[] = []
): Promise<string[]> {
  // Iterate through all files in the directory.
  for (const file of await readdir(dirPath)) {
    // Check if the file is a directory.
    const { isDirectory } = await stat(join(dirPath, file))

    // If it _is_ a directory, recursively call this function to resolve the
    // nested files.
    if (isDirectory()) files = await getAllFiles(join(dirPath, file), files)
    // Else, push the file to the returned array.
    else files.push(join(dirPath, file))
  }

  return files
}
Razer Green Color
I’ve always been fond of the green that Razer uses for their products so the following list is the color in various formats.
  • Hex — #47E10C
  • RGB — rgb(71, 225, 12)
  • HSL — hsl(103, 90%, 46%)
  • HWB — hwb(103, 5%, 12%)
  • CMYX — cmyk(68%, 0%, 95%, 12%)
  • NCOL — Y72, 5%, 12%
Standard

This section is specifically for badges that should be added to any and all of my repositories. I outline/describe as many badges as possible, that would actually provide some benefit, in the sections after this.

  • Code Style
    • Displays the code style used (prettier).
    • code style: prettier
    • [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
  • Discord
    • Provides an invite link to my Discord server.
    • Discord
    • [![Discord](https://img.shields.io/discord/325504841541746688?color=7289DA&style=flat-square)](https://discord.gg/W72x4Ks)
  • NPM License
    • Displays the package license.
    • NPM
    • [![NPM](https://img.shields.io/npm/l/@4is-cronicle/plugin-tools)](https://npmjs.com/package/@4is-cronicle/plugin-tools)
  • NPM Bundle Size
    • Displays the size of the minified package tar.
    • NPM Bundle Size
    • [![NPM Bundle Size](https://img.shields.io/bundlephobia/min/@4is-cronicle/plugin-tools?style=flat-square)](https://bundlephobia.com/package/@4is-cronicle/plugin-tools)
  • NPM Package Version
    • Displays the latest deployed version of the package.
    • npm (scoped)
    • ![npm (scoped)](https://img.shields.io/npm/v/@4is-cronicle/plugin-tools?style=flat-square)
UUID Generation

Generates a UUID in Node.JS.

  • Use crypto.randomBytes() to generate a UUID, compliant with RFC4122 version 4.
  • Use Number.prototype.toString(16) to convert it to a proper UUID.
const crypto = require('crypto')

const UUIDGeneratorNode = () =>
  ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
    (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
  )

UUIDGeneratorNode() // '79c7c136-60ee-40a2-beb2-856f1feabefc'
import { randomBytes } from 'crypto'

const UUIDGeneratorNode = (): string =>
  ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
    (c ^ (randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
  )

UUIDGeneratorNode() // '79c7c136-60ee-40a2-beb2-856f1feabefc'
Chat
  • Discord
    • Discord
    • [![Discord](https://img.shields.io/discord/325504841541746688?color=7289DA&style=flat-square)](https://discord.gg/W72x4Ks)
Code Style
  • Prettier
    • code style: prettier
    • [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
Countly

For sites that utilize Countly for analytics.

  • Dark-Theme
    • [![Dark-Theme](https://count.ly/badges/dark.svg)](https://count.ly/f/badge)
  • Light-Theme
    • [![Light-Theme](https://count.ly/badges/light.svg)](https://count.ly/f/badge)