After adding commander. Js and Inquirer. Js, it should have been directly attached to colors.js, since we are now console output and the console is not flashy.

But the previous inquires.js post was pretty good and went straight to 1.7w, so this post is relatively short.

Reading tips: This article is short and easy to read

A directory

What’s the difference between a free front end and a salted fish

directory
A directory
The preface
Three colors. Js
4 rewrite the console log
V. References

The preface

Returns the directory

Colors.js is a 4.1K STAR (2021-06-16) warehouse made by Marak.

Adding colors.js can make your console more exciting and aesthetically pleasing.

  • Installation:npm i colors
  • Input code:

src/index.ts

import program from 'commander';
import common from './common';
import colors from 'colors';

program
  .version('0.0.1')
  .description('Library of Tools')

program
  .command('jsliang')
  .description('Jsliang help instruction')
  .action(() = > {
    common();
  });

program
  .command('test')
  .description('Test channel')
  .action(() = > {
    const text = ` _ _ _ ___ __ _ _____ _____ | | / ___ / | | | | / | | \ \ | | / ___ | | | | | ___ | | | | / / | | | \ \ | | | | _ | | \ \ ___ \ \ | | | | / / - | | | | \ \ | | | _ | | _ | | ___ | | | | ___ | | / / | | | | \ \ | | | _ | | \ \ / / / | _____ _____ _____ | | _ | / _ / _ - | | | _ | \ \ _ | \ \ / ` _____;
    console.log(colors.rainbow(text));
  });

program.parse(process.argv);
Copy the code

package.json

{
  "name": "jsliang"."version": "1.0.0"."description": "Fe-util, Node Library"."main": "index.js"."scripts": {
    "jsliang": "ts-node ./src/index.ts jsliang"."test": "ts-node ./src/index.ts test"
  },
  "keywords": [
    "jsliang"."Node Tool Library"."Node"]."author": "jsliang"."license": "ISC"."devDependencies": {
    "@types/inquirer": "^ 7.3.1"."@types/node": "^ 15.12.2"."@typescript-eslint/eslint-plugin": "^ 4.26.1"."@typescript-eslint/parser": "^ 4.26.1"."eslint": "^ 7.28.0"."ts-node": "^ 10.0.0"."typescript": "^ 4.3.2." "
  },
  "dependencies": {
    "colors": "^ 1.4.0." "."commander": "^ 7.2.0"."inquirer": "^ 8.1.0"."rxjs": "^ 5.5.12." "}}Copy the code
  • performnpm run test

The console looks pretty:

In the above code, we added the test-related instruction (we will insert the test content here, we can not add it, but jsliang will be used as an example).

As for this beautiful font, it is converted by the ASCII art word converter.

  • Kalvin online tools
  • ASCII character generator

Here freely recommend 2, more partners can dig by themselves.

Three colors. Js

Returns the directory

To do a good job, he must sharpen his tools.

We showed one of the colors in colors.js, colors.rainbow, so there must be other colors.

import colors from 'colors';

console.log(colors.rainbow('rainbow'));
console.log(colors.black('black'));
console.log(colors.red('red'));
console.log(colors.green('green'));
console.log(colors.yellow('yellow'));
console.log(colors.blue('blue'));
console.log(colors.magenta('magenta'));
console.log(colors.cyan('cyan'));
console.log(colors.white('white'));
console.log(colors.gray('gray'));
console.log(colors.grey('grey'));
console.log(colors.bgBlack('bgBlack'));
console.log(colors.bgRed('bgRed'));
console.log(colors.bgGreen('bgGreen'));
console.log(colors.bgYellow('bgYellow'));
console.log(colors.bgBlue('bgBlue'));
console.log(colors.bgMagenta('bgMagenta'));
console.log(colors.bgCyan('bgCyan'));
console.log(colors.bgWhite('bgWhite'));
console.log(colors.bgGrey('bgGrey'));
console.log(colors.reset('reset'));
console.log(colors.bold('bold'));
console.log(colors.dim('dim'));
console.log(colors.italic('italic'));
console.log(colors.underline('underline'));
console.log(colors.inverse('inverse'));
console.log(colors.hidden('hidden'));
console.log(colors.strikethrough('strikethrough'));
console.log(colors.rainbow('rainbow'));
console.log(colors.zebra('zebra'));
console.log(colors.america('america'));
console.log(colors.trap('trap'));
console.log(colors.random('random'));
Copy the code

Drop them into test and execute NPM run test to get a fancy print:

4 rewrite the console log

Returns the directory

OK, we’ve got a nice color up there, and it would be a little hard to justify referencing colors every time we print.

So let’s rewrite console.log so that whenever we use it anywhere we have rainbow colors!

base/getType.ts

/ * * *@name getType
 * @description Get type *@param {string|object} Param passes the variable */
export const getType = (param: string): string= > {
  return Object.prototype.toString.call(param).slice(8, -1);
};
Copy the code

base/console.ts

import colors from 'colors';
import { getType } from './getType';

// Prints the index
let consoleIndex = 1;

/ / rewrite the console log
const log = console.log;
console.log = (. args: any) = > {
  log(`\n---${consoleIndex++}- `);
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (['String'.'Number'.'Boolean'].includes(getType(arg))) {
      log(colors.rainbow(String(arg)));
    } else{ log(arg); }}};/ / rewrite the console. The error
const error = console.error;
console.error = (. args: any) = > {
  log(`\n---${consoleIndex++}- `);
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (['String'.'Number'.'Boolean'].includes(getType(arg))) {
      error(colors.red(String(arg)));
    } else{ error(arg); }}};Copy the code

Then reference the overridden console.ts in SRC /index.ts, so that the global is available:

src/index.ts

import program from 'commander';
import common from './common';
import './base/console';

program
  .version('0.0.1')
  .description('Library of Tools')

program
  .command('jsliang')
  .description('Jsliang help instruction')
  .action(() = > {
    common();
  });

program
  .command('test')
  .description('Test channel')
  .action(() = > {
    console.log('There is jsliang? '.true);
    console.error('A random error indicates that there is something wrong with it');
  });

program.parse(process.argv);
Copy the code

If you run NPM run test, the output will be:

In fact, rainbow color looks too fancy, but for the time being, you can change the color by yourself

So, flowery access is finished, although are API copy and paste engineers, but do a good look at the decoration or some ~

See you next!

V. References

Returns the directory

  • Making: Marak/colors. Js
  • Kalvin online tools
  • ASCII character generator

Do not toss the front end, and what is the difference between salted fish!

Jsliang’s document library is licensed by Junrong Liang under the Creative Commons Attribution – Non-commercial – Share alike 4.0 International License. Based on the github.com/LiangJunron… On the creation of works. Outside of this license agreement authorized access can be from creativecommons.org/licenses/by… Obtained.