nodejs交互工具库 -- chalk-pipe和chalk

nodejs交互工具库系列

作用
chalk-pipe 使用更简单的样式字符串创建粉笔样式方案
chalk 正确处理终端字符串样式
Commander.js 完整的 node.js 命令行解决方案
Inquirer.js 一组通用的交互式命令行用户界面。
slash 系统路径符处理
minimist 解析参数选项
dotenv 将环境变量从 .env文件加载到process.env中
dotenv-expand 扩展计算机上已经存在的环境变量
hash-sum 非常快的唯一哈希生成器
deepmerge 深度合并两个或多个对象的可枚举属性。
yaml-front-matter 解析yaml或json
resolve 实现node的 require.resolve()算法,这样就可以异步和同步地使用require.resolve()代表文件
semver npm的语义版本器
leven 测量两字符串之间的差异
最快的JS实现之一
lru cache 删除最近最少使用的项的缓存对象
portfinder 自动寻找 800065535内可用端口号
ora 优雅的终端转轮
envinfo 生成故障排除软件问题(如操作系统、二进制版本、浏览器、已安装语言等)时所需的通用详细信息的报告
memfs 内存文件系统与Node's fs API相同实现
execa 针对人类的流程执行
webpack-merge 用于连接数组和合并对象,从而创建一个新对象
webpack-chain 使用链式API去生成简化webpack版本配置的修改
strip-ansi 从字符串中去掉ANSI转义码
address 获取当前机器的IP, MAC和DNS服务器。
default-gateway 通过对OS路由接口的exec调用获得机器的默认网关
joi JavaScript最强大的模式描述语言和数据验证器。
fs-extra 添加了未包含在原生fs模块中的文件系统方法,并向fs方法添加了promise支持
Acorn 一个小而快速的JavaScript解析器,完全用JavaScript编写。
zlib.js ZLIB.js是ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952)和PKZIP在JavaScript实现。

nodejs交互工具库 -- chalk-pipe和chalk

nodejs交互工具库 -- commander和Inquirer

nodejs交互工具库 -- slash, minimist和dotenv, dotenv-expand

nodejs交互工具库 -- hash-sum, deepmerge和yaml-front-matter

nodejs交互工具库 -- resolve和semver

nodejs交互工具库 -- leven, lru cache和portfinder

nodejs交互工具库 -- ora和envinfo

nodejs交互工具库 -- memfs和execa

nodejs交互工具库 -- webpack-merge和webpack-chain

nodejs交互工具库 -- strip-ansi, address, default-gateway和joi

nodejs交互工具库 -- fs-extra, Acorn和zlib

chalk-pipe

使用更简单的样式字符串创建粉笔样式方案
nodejs交互工具库 -- chalk-pipe和chalk_第1张图片

Install

yarn add chalk-pipe

Usage

const chalkPipe = require('chalk-pipe');

console.log(chalkPipe('blue.bold')('Hello world!'));

使用点.区分多种样式:

const chalkPipe = require('chalk-pipe');

const link = chalkPipe('blue.underline');
const error = chalkPipe('bgRed.#cccccc');
const warning = chalkPipe('orange.bold');

console.log(link('Link!'));
console.log(error('Error!'));
console.log(warning('Warning!'));

image

chalkPipe is also chalk:

const chalkPipe = require('chalk-pipe');
const blue = chalkPipe('blue');
const link = blue.underline;

console.log(link('Link!'));

使用定制的chalk

const chalk = require('chalk');
const chalkPipe = require('chalk-pipe');
const text =  chalkPipe('underline', chalk.blue)('Link!');

console.log(text);

API

chalkPipe(styles)(text)

chalkPipe('blue.underline')('Link!');

chalkPipe(styles, chalk)(text)

const chalk = require('chalk');

chalk.enable = true;

chalkPipe('underline', chalk.blue)('Link!');

Valid styles

参考

基本常用的方法场景就这些了,更完整的用法可以直接查阅文档

chalk-pipe

如果需要更加细致的需求,就要使用下面的chalk了,毕竟这只是它简化版的库

chalk

nodejs交互工具库 -- chalk-pipe和chalk_第2张图片

正确处理终端字符串样式

image

亮点

  • 富有表现力的API
  • 高性能
  • 嵌套样式的能力
  • 256 /真彩颜色支持
  • 自动侦测颜色支持
  • 不延长String.prototype
  • 清洁和集中
  • 积极维护
  • 截至2020年1月1日,被约5万个软件包使用

Install

yarn add chalk

Usage

Chalk提供了一个易于使用的可组合API,您只需链嵌您想要的样式。

const chalk = require('chalk');
const log = console.log;

// Combine styled and normal strings
log(chalk.blue('Hello') + ' World' + chalk.red('!'));

// Compose multiple styles using the chainable API
log(chalk.blue.bgRed.bold('Hello world!'));

// Pass in multiple arguments
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));

// Nest styles
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));

// Nest styles of the same type even (color, underline, background)
log(chalk.green(
  'I am a green line ' +
  chalk.blue.underline.bold('with a blue substring') +
  ' that becomes green again!'
));

// ES2015 template literal
log(`
  CPU: ${chalk.red('90%')}
  RAM: ${chalk.green('40%')}
  DISK: ${chalk.yellow('70%')}
`);

// ES2015 tagged template literal
log(chalk`
  CPU: {red 20%}
  RAM: {green 30%}
  DISK: {rgb(255,131,0) 40%}
`);

// Use RGB colors in terminal emulators that support it.
log(chalk.keyword('orange')('Yay for orange colored text!'));
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
log(chalk.hex('#DEADED').bold('Bold gray!'));

nodejs交互工具库 -- chalk-pipe和chalk_第3张图片

API

chalk.