Node JS 命令行工具系列

作者: Bougie 创建于: 2019-04-22 | 神器

terminalizer - 记录终端操作并生成 Gif

npm install terminalizer -g
terminalizer record demo
# ... you actions
terminalizer render demo

yargs - CLI 参数解析

#!/usr/bin/env node
const argv = require('yargs').argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

commander - 出自 tj 大神

#!/usr/bin/env node
var program = require('commander')

program.option('--no-sauce', 'Remove sauce').parse(process.argv)

console.log('you ordered a pizza')
if (program.sauce) console.log('  with sauce')
else console.log(' without sauce')

chalk - 多彩 log

const chalk = require('chalk')

console.log(chalk.blue('Hello world!'))

colors - 多彩 log

var colors = require('colors')

console.log('hello'.green) // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse) // inverses the color
console.log('OMG Rainbows!'.rainbow) // rainbow
console.log('Run the trap'.trap) // Drops the bass

inquirer - 交互式 CLI

var inquirer = require('inquirer')
inquirer
  .prompt([
    /* Pass your questions in here */
  ])
  .then((answers) => {
    // Use user feedback for... whatever!!
  })

oclif - 命令行工具脚手架

$ npx oclif single mynewcli
? npm package name (mynewcli): mynewcli
$ cd mynewcli
$ ./bin/run
hello world from ./src/index.js!
上次更新: 2019-6-2 12:13:03