# 命令行工具

# readline-sync

同步 Readline,以交互方式运行,通过控制台(TTY)与用户进行对话。

var readlineSync = require("readline-sync");

// Wait for user's response.
var userName = readlineSync.question("May I have your name? ");
console.log("Hi " + userName + "!");

// Handle the secret text (e.g. password).
var favFood = readlineSync.question("What is your favorite food? ", {
  hideEchoBack: true, // The typed text on screen is hidden by `*` (default).
});
console.log("Oh, " + userName + " loves " + favFood + "!");

# inquirer

交互命令,询问

const { bump, customVersion } = await inquirer.prompt([
  {
    name: "bump",
    message: "Select release type:",
    type: "list",
    choices: [...bumpChoices, { name: "custom", value: "custom" }],
  },
  {
    name: "customVersion",
    message: "Input version:",
    type: "input",
    when: (answers) => answers.bump === "custom",
  },
]);

# execa

调用系统命令

await execa("git", ["add", "-A"], { stdio: "inherit" });

# chalk

终端输出样式

console.log(chalk.blue.bgRed.bold("Hello world!"));

# colors

终端输出样式

// 方法一
console.log(colors.red.bold("hello world"));
// 方法二
console.log("hello world".red.bold);

# minimist

var argv = require("minimist")(process.argv.slice(2));
console.dir(argv);

命令参数

$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }

# commander

node.js 命令行接口的完整解决方案,灵感来自 Ruby 的 commander。

const program = require("commander");

program
  .option("-d, --debug", "output extra debugging")
  .option("-s, --small", "small pizza size")
  .option("-p, --pizza-type <type>", "flavour of pizza");

program.parse(process.argv);

if (program.debug) console.log(program.opts());
console.log("pizza details:");
if (program.small) console.log("- small pizza size");
if (program.pizzaType) console.log(`- ${program.pizzaType}`);

# slash

接受一个 Windows 反斜杠路径并返回一个带有正斜杠的路径

const path = require("path");
const slash = require("slash");

const string = path.join("foo", "bar");
// Unix    => foo/bar
// Windows => foo\\bar

slash(string);
// Unix    => foo/bar
// Windows => foo/bar

# chokidar

监听文件变化

const chokidar = require("chokidar");

// One-liner for current directory
chokidar.watch(".").on("all", (event, path) => {
  console.log(event, path);
});

# cross-env

{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
  }
}

配置多个环境变量

{
  "scripts": {
    "build": "cross-env NODE_ENV=production TEST_ENV='jest hello' webpack --config build/webpack.config.js"
  }
}

# serve

静态服务器

serve folder_name

# nodemon

nodemon 是一个帮助开发基于 node.js 的应用程序的工具,它可以在检测到目录中的文件更改时自动重新启动节点应用程序。

安装

npm install -g nodemon

运行

nodemon app.js

# pm2

nodejs 的进程守护管理

# figlet

var figlet = require("figlet");

figlet("Hello World!!", function (err, data) {
  if (err) {
    console.log("Something went wrong...");
    console.dir(err);
    return;
  }
  console.log(data);
});
  _   _      _ _        __        __         _     _ _ _
 | | | | ___| | | ___   \ \      / /__  _ __| | __| | | |
 | |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` | | |
 |  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |_|_|
 |_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_(_|_)

# cross-spawn

跨平台 spawn 函数

const spawn = require("cross-spawn");

// Spawn NPM asynchronously
const child = spawn("npm", ["list", "-g", "-depth", "0"], { stdio: "inherit" });

// Spawn NPM synchronously
const result = spawn.sync("npm", ["list", "-g", "-depth", "0"], {
  stdio: "inherit",
});

# detect-port-alt

端口检测

detect(port, (err, _port) => {
  if (err) {
    console.log(err);
  }

  if (port == _port) {
    console.log(`port: ${port} was not occupied`);
  } else {
    console.log(`port: ${port} was occupied, try port: ${_port}`);
  }
});

# ora

小菊花

# word-table

表格