如何使用node.js制作Commead Line应用程序
#javascript #网络开发人员 #node #cli

使用node.js创建命令行界面应用程序是构建用户可以从其终端运行的实用程序的好方法。

下面,我将指导您使用Node.js

创建简单的CLI工具的步骤
  1. 设置项目: 首先,为您的CLI工具创建一个新目录,并使用NPM初始化一个新的Node.js项目
mkdir my-cli-tool
cd my-cli-tool
npm init -y

2.安装必要的依赖项:
在此示例中,我们将使用“ Yargs”软件包来处理命令行输入和“粉笔”软件包,以在输出中添加颜色。安装这些依赖项:
npm install yargs chalk

  1. 设置Yargs Pakage: 导入我们将创建的Yargs和Notes文件。以下, 添加所有命令来处理输入并使用注释脚本到 处理每个命令。
const yargs = require("yargs");
const notes = require("./notes.js");

//creat add command
yargs.command({
  command: "add",
  describe: "Add a new note",
  builder: {
    title: {
      describe: "note title ",
      demandOption: true,
      type: "string",
    },
    body: {
      describe: "note body",
      demandOption: true,
      type: "string",
    },
  },
  handler(argv) {
    notes.addNote(argv.title, argv.body);
  },
});
// remove command
yargs.command({
  command: "remove",
  describe: "remove a  note",
  builder: {
    title: {
      describe: "note title",
      demandOption: true,
      type: "string",
    },
  },
  handler(argv) {
    notes.removeNote(argv.title);
  },
});
//creat a read command
yargs.command({
  command: "read",
  describe: "read a note",
  builder: {
    title: {
      command: "title",
      describe: "note title",
      demandOption: true,
      type: "string",
    },
  },
  handler(argv) {
    notes.readNote(argv.title);
  },
});

//creat a list command
yargs.command({
  command: "list",
  describe: "list your notes",
  handler() {
    notes.listNotes();
  },
});
// for yargs work
yargs.parse();

4.请注意处理所有命令:
使用文件系统来存储笔记和粉笔
样式输出

const fs = require("fs");
const chalk = require("chalk");

const saveNotes = (notes) => {
  let notesJSON = JSON.stringify(notes);
  fs.writeFileSync("notes.json", notesJSON);
};

const loadNotes = () => {
  try {
    return JSON.parse(fs.readFileSync("notes.json").toString());
  } catch (e) {
    return [];
  }
};

let notes = loadNotes();

const addNote = (title, body) => {
  let note = {
    title: title,
    body: body,
  };
  const duplicateNote = notes.find((note) => note.title === title);
  if (!duplicateNote) {
    notes.push(note);
    saveNotes(notes);
    console.log(chalk.green.inverse("New note added!"));
  } else {
    console.log(chalk.red.inverse("note title taken!"));
  }
};

const removeNote = (title) => {
  let newNotes = notes.filter((note) => note.title !== title);
  if (notes.length > newNotes.length) {
    console.log(chalk.green.inverse("Note removed!"));
    saveNotes(newNotes);
  } else {
    console.log(chalk.red.inverse("No note found! or it removed"));
  }
};

const listNotes = () => {
  if (notes.length == 0) {
    console.log(chalk.inverse("no note found add one"));
    return;
  }
  console.log(chalk.inverse("Your notes"));
  notes.forEach((note) => {
    console.log(chalk.blue(note.title));
  });
};

const readNote = (title) => {
  const note = notes.find((note) => note.title === title);
  if (note) {
    console.log("title: " + chalk.blue.inverse(note.title));
    console.log("body: " + chalk.blue(note.body));
  } else {
    console.log(chalk.red.inverse("note not found!"));
  }
};

module.exports = {
  addNote,
  removeNote,
  listNotes,
  readNote,
};

5.测试应用:
添加命令

node .\app.js add  --title="test app" --body="test"

删除命令

node .\app.js remove  --title="test app"

阅读注释命令

node .\app.js read  --title="test app"

列表命令

node .\app.js list

就是这样!现在,您已经使用Node.js创建了一个基本的CLI工具,该工具可以由他人共享和使用。您可以根据特定用例将其扩展更多命令和功能。