Create Custom CLI Tool with Node JS
Sep 1, 2024
Creating Command Line Tools (CLI) can be a great way to automate repetitive tasks, enhance productivity and minimize time spent on time consuming tasks. In this post we'll see how we can build a custom CLI Tool in Node js
-
Introduction
A CLI program basically is a program that runs in the terminal, allowing users to interact with it via commands and arguments.
-
Prerequisites
Before we start you need to have Node JS Installed (In your machine or containerized) check the official website.
I run the version
v22.2.0
you can check your version via the command:Make sure it is equivalent or higher than mine (recommended to avoid unexpected issues).
I will be using TypeScript in the following code. You can ignore the types if you want to stick with JavaScript.
You can install TypeScript globally on you system using the following command:
-
Setting Up The Project
In this project I want to create a code comments and trailing spaces remover. That means it will take a file as input and removes comments or trailing spaces on that file.
First I will create a directory for this project:
Note: I will use Built in modules. If you are lazy like me skip this section to the coming one where i use some npm modules.
Now Create a new project using NPM:
Install Dependencies
These dependencies are not required if you are using JavaScript:
Configure TypeScript
Create a
tsconfig.json
file to configure TypeScript:If you run this command a
tsconfig.json
file will appear Add the following code it is missing (or uncomment it if it is commented):Add NPM Scripts
Add the following scripts for building the project and for development too:
Create File Structure
I'll create an
index.ts
file (orindex.js
) file inside thesrc
directory as the entry point to the project. Run the following command:Test if Everything works
Let's add some code to the index.ts file:
Now let's run the
build
,start
,dev
commands.Let's try the first command:
If it builds with no errors you will see that there is a new
dist
directory. It has the compiled JavaScript code.I'll keep the rest for you to test them.
Now The File structure Will be something similar to this:
-
Create The CLI Logic
-
Add Shebang and Imports
First let's import the required packages in the
index.ts
file:The first line basically allows the script to be executed from the command line directly.
Here is an AI generated explanation to it if you are a Nerd:
Now if we run
build
command and then make compiled file executable via this command (Read the explanation):And then try to run the command via
It works (will not output anything because it is empty).
Congrats you created the first CLI Tool.
-
Basic CLI Structure
If we try to log the
process.argv
object inside the project like this:Then build and run the file:
We get nothing interesting:
These two paths mean the binary path for Node JS and the path for our JS file.
But if we add some text after the file path like this:
We get this output
Which means we can send arguments via writing them after the file name. Here is an AI generated text explaining the standard way to use arguments:
To make Things easier to work with we can slice the arguments from the two first paths like this:
-
Create Arguments Parser Function
First let's define what we need as an
interface
.We need the following flags:
--file
or--f
flag for file name.--spaces
to specify the removal or trailing spaces.--comments
for the comments removal and also for the comments syntax argument after it.--multiStart
for the multi line comments removal and also. For the multi line comments starting syntax argument after it.--multiEnd
for the multi line comments removal and also for the multi line comments ending syntax argument after it.
Now let's Create the parser function which will return
CleanOptions
type: -
Create The Business Logic
Let's create the functions that will handle the trailing spaces and comments removal:
Also let's create the functions responsible for files read/write:
Now let's create the function that puts all the pieces of the puzzle together:
Now just call the functions or wrap it in a
main
function for easier readability:
-
-
Create CLI Tools Using Commander
Commander is a JS library for argument parsing which means it can replace
parseArguments
functionFirst let's install the package:
Then let's use it to parse the arguments like this:
Also, We need to change the
main
function:Note:
You can do a lot more with this library check this GitHub repo.
Conclusion
Congratulations! You've built a fully functioning CLI tool with Node.js and TypeScript, complete with commands, options, error handling, and a polished user experience. This tool can now be used locally, shared with your team, or published for the world to use.
By following this guide, you've learned not just how to build a CLI tool, but also how to structure a Node.js project, handle command-line arguments, validate input, and enhance user experience. This foundation can be the starting point for more complex and powerful CLI tools.
Happy Hacking!