

Building a Node JS interactive CLI
source link: https://www.tuicool.com/articles/hit/7jAFBb3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Building a Node JS interactive CLI

NodeJS can be very useful when it comes to building Command-line Interfaces also known as CLI’s.
In this post, I’ll teach you how to build a CLI that asks some questions and creates a file, based on the answers.

Getting started
Let’s start by creating a brand new npm package
mkdir my-script cd my-script npm init
NPM will ask some questions. After that, we need to install some packages.
npm install --save chalk figlet inquirer shelljs
What these packages do:
- chalk — Terminal string styling done right
- figlet — Figlet is a program for making large letters out of ordinary text
- inquirer — A collection of common interactive command line user interfaces
- shelljs — Portable Unix shell commands for Node.js
index.js file
Now create a index.js
file with the following content:
Planning the CLI
it’s always good to plan what a CLI needs to do before writing any code.
This CLI will do just one thing: Create a file .
It should ask a couple of questions and after that, show a success message with the created file path.
The questions are: what is the filename and what is the extension.
The first function is the script introduction. Let’s use chalk
and figlet
to get the job done.
Now it’s time to write a function that asks questions.
Notice the constants FILENAME and EXTENSIONS that came from inquirer
.
The next step is to create the file.
And last but not least, show the success message along with the file path.
Let’s test the script by running node index.js
.

And here is the final code:
To execute this script anywhere add a bin section in your package.json
file and run npm link
$ npm link $ creator
Hope it helps :)
Recommend
-
33
Last week, I wrote an article about building a Node.js CLI using yargs . I introduced it saying we use cli tools everyday to simplify common t...
-
39
cli-arxiv CLI tool for exploring arXiv (inspired by karpathy's brilliant ArXiv Sanity Preserver) The script will create data/pdf/, data/txt/ and data/summary/ directories to hold files downloaded from arX...
-
20
云音乐大前端专栏Node CLI 工具的插件方案探索2020-04-28 本文...
-
4
安装node使用Homebrew安装# 查看node.js版本/本地是否安装node.js node -v # 指向homebrew安装node的目录 brew link node # 卸载node.js brew uninstall node # 安装node.js brew install node where node
-
62
Tailwind CSS Standalone CLI: Use Tailwind CSS without Node.js Tailwind just...
-
14
Writing an Extensible CLI Tool in 2022 with Node.jsMaking one is easy, making it so it can be extended without shooting yourself in the foot? That’s not so straightforward
-
5
9 Useful Interactive CLI Tools for Linux ...
-
4
a framework for building n...
-
2
February 13, 2023 Building a simple CLI tool with modern Node.js I’m a maintainer of several dozen open source libraries. One thing I’ve always done is maintain a hand-written changelog. Here’s an example f...
-
8
We are excited to announce that the Azure CLI team combined AI with CLI interactive mode (az interactive) to provide users with an intelligent interactive experience. It is primarily oriented to inexperienced users in interactive mode to he...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK