当前位置: 首页 > 工具软件 > Yargs > 使用案例 >

nodeJS插件(yargs,inquirer,ora,chalk)-知识记录

牛景同
2023-12-01

NodeJS-插件

yargs命令行规范工具

用于node命令行参数解析

下载与文档地址

https://www.npmjs.com/package/yargs

基本用法

  • 以Object形式储存命令信息
  • args[’$0’]为运行的脚本
  • 命令的变量由空格区分,一个空格后跟随一个变量
  • --xx-xx为作为key后面用=连接值为value
  • key后面不接值,默认为布尔值true
  • key不需要---直接写参数,此参数保存在argv._
// node
// node ./watch/index.js --watch=./test --output ./write/write.js -b -x 4 helloWorld

const yargs = require('yargs')
console.log(yargs.argv)

// console.log
yargs.argv = {
  _: [ 'helloWorld' ],
  watch: './test',
  output: './write/write.js',
  b: true,
  x: 4,
  '$0': 'watch\\index.js'
}

inquirer命令互的工具

实现与命令行进行数据交互

下载与文档地址

https://www.npmjs.com/package/inquirer

基本用法

const inquirer = require('inquirer')
let options = [
  // 配置项
]
inquirer.prompt(options).then(res => {

}).catch(err => {

})

.prompt(options)

参数类型描述默认值
typeString表示提问的类型-
nameString存储当前问题回答的变量key-
messageString交互描述-
default*交互的默认值-
choices列表选项,在某些type下可用,并且包含一个分隔符-
validateFunction对用户的回答进行校验-
filterFunction对用户的回答进行过滤处理,返回处理后的值-
transformerFunction对用户回答的显示效果进行处理(如:修改回答的字体或背景颜色),但不会影响最终的答案的内容-
whenFunction(answers/交互过的内容)根据前面问题的回答,判断当前问题是否需要被答-
pageSizeNumber修改某些type类型下的渲染展示行数-
prefixString修改message默认前缀
suffixString修改message默认后缀
askAnsweredBoolean如果答案已经存在,则强制提示问题-

options.type

参数描述
type“input”输入框
type“confirm”确认框
type“list”无序列表选择
type“rawlist”有序列表选择
type“expand”模糊选择
type“checkbox”多选按钮
type“password”密文输入
type“editor”代码输入

options.choices

参数值类型描述
nameString名字,列表形式为name
checkedBoolean是否预先选中

new inquirer.Separator()

ora进度转轮

用于node的控制台进度美化

下载与文档地址

https://www.npmjs.com/package/ora

基本用法

const ora = require('ora')
const spinner = ora('loading...')
spinner.start()
setTimeout(() => {
  spinner.stop()
}, 2000)

属性

ora(options)

  • options为单个参数时为text值
参数类型描述默认值
textString转轮后方的文字
prefixTextString转轮前方的文字-暂时无效
spinnerString、Object转轮的动画,可自定义:{interval: 80, frames: [’-’, ‘+’, ‘-’]}‘dots’
colorString转轮的颜色,提供值:‘black’、‘red’、‘green’、‘yellow’、‘blue’、‘magenta’、‘cyan’、‘white’、‘gray’‘cyan’
hideCursorBoolean隐藏鼠标指针true
indentNumber调整转轮的缩进-暂时无效0
intervalNumber ,ms转轮动画每帧之间的时间间隔自定的间隔进行、 100
streamstream.Writable流式信息输出process.stderr
isEnabledBoolean是否将转轮信息写入日志false
discardStdinBoolean暂时不懂,Discard stdin input (except Ctrl+C) while running if it’s TTY. This prevents the spinner from twitching on input, outputting broken lines on Enter key presses, and prevents buffering of input while the spinner is running.This has no effect on Windows as there’s no good way to implement discarding stdin properly there.true

方法

  • 所有的属性在运行的时候可以访问更改
方法名参数用法返回值
start()text运行转轮,有text则显示text返回当前实例
stop()-停止转轮并清除返回当前实例
succeed()text转轮成功状态返回当前实例
fail()text转轮失败状态返回当前实例
warn()text转轮警告状态返回当前实例
info()text转轮提示状态返回当前实例
isSpinning-判断转轮当前是否在转Boolean
stopAndPersist()options暂停转轮,替换设置返回当前实例
clear()-清空转轮-跟stop很像返回当前实例
render()-渲染帧返回当前实例
frame()-获取下一帧返回当前实例
ora.promise()action(Promise), options转轮对promise处理,resolve->succeed,reject->fail返回当前实例

chalk控制台打印

用于控制台输出美化

下载与文档地址

https://www.npmjs.com/package/chalk

基本用法

const chalk = require('chalk')
console.log(chalk.blue('Hello world!'))

属性

参数类型描述默认值
textString打印文字-

方法

颜色色域

方法名参数描述返回值
Instance(){level: 0/1/2/3}更改打印颜色色域范围-
Level描述
0所有颜色无法使用
1基础的16中颜色
2256中颜色
3一千六百万颜色

文字效果

方法名参数描述返回值
reset()text重置颜色返回当前实例
bold()text文字加粗返回当前实例
dim()text文字少量发光返回当前实例
italic()text文本倾斜返回当前实例
underline()text文字加下划线返回当前实例
invers()text反转背景色和前景色返回当前实例
hidden()text文字影藏返回当前实例
strikethrough()text文字删除线返回当前实例
visible()text当instance.level > 0是打印文字返回当前实例

文字颜色

方法名参数描述返回值
black()text返回当前实例
red()text返回当前实例
green()text返回当前实例
yellow()text返回当前实例
blue()text返回当前实例
magenta()text返回当前实例
cyan()text返回当前实例
white()text返回当前实例
blackBright()text(alias: bgGray, bgGrey)返回当前实例
redBright()text返回当前实例
greenBright()text返回当前实例
yellowBright()text返回当前实例
blueBright()text返回当前实例
magentaBright()text返回当前实例
cyanBright()text返回当前实例
whiteBright()text返回当前实例

文字背景色

方法名参数描述返回值
black()text返回当前实例
bgBlack()text返回当前实例
bgRed()text返回当前实例
bgGreen()text返回当前实例
bgYellow()text返回当前实例
bgBlue()text返回当前实例
bgMagenta()text返回当前实例
bgCyan()text返回当前实例
bgWhite()text返回当前实例
bgBlackBright()text(alias: bgGray, bgGrey)返回当前实例
bgRedBright()text返回当前实例
bgGreenBright()text返回当前实例
bgYellowBright()text返回当前实例
bgBlueBright()text返回当前实例
bgMagentaBright()text返回当前实例
bgCyanBright()text返回当前实例
bgWhiteBright()text返回当前实例
  • instance >= 2时的颜色描述
  • 加bg变小驼峰改背景颜色
方法名参数用法返回值
rgbrgb色值chalk.rgb(255, 136, 0)返回当前实例
hexhex色值 Stringchalk.hex(’#FF8800’)返回当前实例
keywordcss色值chalk.keyword(‘orange’)返回当前实例
hslhsl色值chalk.hsl(32, 100, 50)返回当前实例
hsvhsv色值chalk.hsv(32, 100, 100)返回当前实例
hwbhwb色值chalk.hwb(32, 0, 50)返回当前实例
ansiansi色值chalk.ansi(31).bgAnsi(93)返回当前实例
ansi256ansi256色值chalk.ansi256(194)返回当前实例
 类似资料: