cli 工具 nodejs_如何在NodeJS中构建CLI工具?

cli 工具 nodejs

by Al-amin Nowshad

通过阿敏·诺沙德(Al-Amin Nowshad)

如何在NodeJS中构建CLI工具? (How to build a CLI tool in NodeJS ?)

As developers, we kind of live with CLI tools. From git to cloud shells — we are using these tools everywhere. So, it’s time to make your own. We’ll use Heroku’s great oclif framework in the process.

作为开发人员,我们可以使用CLI工具。 从gitcloud shells ,我们到处都在使用这些工具。 因此,是时候打造自己的了。 在此过程中,我们将使用Heroku出色的oclif框架。

什么是斜体? (What’s oclif?)

It’s an open framework to build CLI tools quickly, and it’s provided by the well-known Heroku.

这是一个快速构建CLI工具的开放框架 ,由著名的Heroku提供

将要建造什么? (What’s gonna be built?)

We’ll make a todo list command that can have four actions:

我们将创建一个todo list命令,该命令可以执行以下四个操作:

  • Add a new task

    添加新任务
  • View all tasks

    查看所有任务
  • Update task

    更新任务
  • Remove a task

    删除任务

初始化我们的项目 (Initialize our project)

Oclif can generate two types of projects —

Oclif可以生成两种类型的项目-

  1. Projects that have one command.

    具有一个命令的项目。
  2. Projects that might have multiple commands, including nested ones.

    项目可能具有多个命令,包括嵌套命令。

In this article, we’re gonna need a multiple commands project, so let’s generate it:

在本文中,我们将需要一个多命令项目,因此让我们生成它:

npx oclif multi todocli

Running this command and following the instructions will initialize a fresh project named todocli inside the current directory.

运行此命令并按照说明进行操作,将在当前目录中初始化一个名为todocli的新项目。

Now, let’s go inside the directory and run help:

现在,让我们进入目录并运行帮助:

cd todocli && ./bin/run --help

This will print the results below:

这将在下面打印结果:

> USAGE       $ todocli [COMMAND]    COMMANDS    hello       help   display help for todocli

This shows available commands and their documentation.

这显示了可用的命令及其文档。

项目结构 (Project Structure)

Inside src directory we can find a directory named commands. This directory holds all the commands with their relative file names. For example, if we have a command hello we’ll have a file named hello.js inside this directory and the command will work without any more setup. Let’s remove hello.js as we won’t be needing it.

src目录中,我们可以找到一个名为commands的目录。 该目录包含所有命令及其相对文件名。 例如,如果我们有一个命令hello我们将在此目录中有一个名为hello.js的文件,该命令无需任何其他设置即可工作。 让我们删除hello.js因为我们不需要它。

设定资料库 (Setup database)

To store our tasks we need a storage system. For simplicity, we’ll use lowdb which is a pretty simple json file storage system. Sweet for this project ?

为了存储我们的任务,我们需要一个存储系统。 为简单起见,我们将使用lowdb ,这是一个非常简单的json文件存储系统。 这个项目好吗?

Let’s install it:

让我们安装它:

npm install lowdb --save

Let’s create a db.json file inside our project root directory. This file will hold our data. Then we need to install lowdb. Now, we’ll create a file called db.js inside the src directory. This file will hold our database stuff.

让我们在项目根目录中创建一个db.json文件。 该文件将保存我们的数据。 然后我们需要安装lowdb 。 现在,我们将在src目录中创建一个名为db.js的文件。 该文件将保存我们的数据库资料。

Here, we’re simply loading the required library and file at first, then defining an empty todos array as our base collection (it’s like a table if you’re thinking like SQL databases).

在这里,我们首先只是简单地加载所需的库和文件,然后定义一个空的todos数组作为我们的基本集合(如果您想使用的是SQL数据库,则它就像一个表)。

添加任务 (Adding tasks)

oclif provides us with a smooth command generation functionality. Let’s run the following:

oclif为我们提供了平滑的命令生成功能。 让我们运行以下命令:

oclif command add

This will create a file named add.js inside src/commands directory. Let’s replace that file’s content with the code below:

这将在src/commands目录中创建一个名为add.js的文件。 让我们用以下代码替换该文件的内容:

The file has a few key components:

该文件包含一些关键组件:

  • a run function that executes the main functionality of this command,

    运行该命令的主要功能的运行功能,
  • a description, that’s the documentation for the command, and

    说明,即命令的文档,以及
  • flags, which describes the flags passed to the command.

    标志,描述传递给命令的标志。

Here, we have a flag named task which has a string type. We can run the command

在这里,我们有一个名为task的标志,它具有string类型。 我们可以运行命令

./bin/run add --task="welcome task"

This command will add a task to our database and print the response of that operation.

此命令会将一个任务添加到我们的数据库并打印该操作的响应。

显示任务 (Showing tasks)

Here inside show.js , we’re showing all the tasks in ascending order. We’ve added a little color with chalkjs to give our command results a better look.

show.js内部,我们以升序显示所有任务。 我们为chalkjs添加了一些颜色,以使命令结果更好看。

更新任务 (Updating Tasks)

For simplicity, we’re now just setting tasks as done for our update part. We’ve to just passed the task’s id as a flag .

为简单起见,我们现在仅将更新部分设置为done任务。 我们刚刚将任务的id作为flag传递了。

./bin/run update --id=1

This will set the done = true for the task with id = 1 .

这将为id = 1的任务设置done = true

删除任务 (Removing tasks)

Removing is pretty straightforward: we pass id as a flag, and then remove the related task from our database.

删除非常简单:我们将id作为标志传递,然后从数据库中删除相关任务。

快完成了! (Almost Done!)

And just like that, we’ve made our very simple todocli. Now if we want to use it like any other normal CLI tool or let our friends use it, we need to make it an npm package. So actually let’s publish it on npm.

就像这样,我们使我们的todocli非常简单。 现在,如果我们想像其他任何常规CLI工具一样使用它,或者让我们的朋友使用它,我们需要将其设置为npm软件包。 因此,实际上让我们在npm上发布它。

构建并发布到npm (Build and Publish to npm)

First of all, make sure you have a npm account. Then you need to login running the command

首先,请确保您有一个npm帐户。 然后您需要登录运行命令

npm login

Then inside the project directory run

然后在项目目录中运行

npm run prepack

This will pack the project and make it npm-ready with a generated readme from the descriptions and flags.

这将打包该项目,并使用描述和标志生成的自述文件使其变为npm-ready。

Now, publish it on npm:

现在,将其发布在npm上:

npm publish

If everything goes well, then the module has been published on npm successfully. If it doesn’t work, check the project name and version.

如果一切顺利,则该模块已成功在npm上发布。 如果不起作用,请检查项目名称和版本。

Now, we can use it like any other npm tool with the global installation:

现在,我们可以像在全局安装中使用任何其他npm工具一样使用它:

npm install -g todocli

And then anyone can just use these commands anytime, almost anywhere ?

然后任何人都可以随时随地使用这些命令?

> todocli add --task="Great task!!!"> todocli show> todocli update --id=1> todocli remove --id=1

If you’ve come this far following the whole article, congratulations ? You are awesome. Now, you can do a little task:

如果您在整篇文章中走得这么远,恭喜您? 你真棒。 现在,您可以执行以下任务:

任务 (Task)

The id assigning of the tasks is not a proper one, can you fix it? Let me know how you solve it in the response section.

任务的ID分配不正确,可以解决吗? 让我知道您如何在响应部分中解决它。

Good luck, and thanks for reading :)

祝你好运,并感谢您的阅读:)

oclif : https://oclif.io

oclif: https ://oclif.io

lowdb: https://github.com/typicode/lowdb

lowdb: https//github.com/typicode/lowdb

chalk: https://github.com/chalk/chalk

粉笔: https//github.com/chalk/chalk

todocli: https://www.npmjs.com/package/todocli-frombd

todocli: https ://www.npmjs.com/package/todocli-frombd

翻译自: https://www.freecodecamp.org/news/how-to-build-a-cli-tool-in-nodejs-bc4f67d898ec/

cli 工具 nodejs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值