Compilation of Libraries Supporting CLI Application Development for Node.js

Compilation of Libraries Supporting CLI Application Development for Node.js

Daily short news for you
  • Deep Research - a web browsing tool for research that produces a summary in just a few minutes - compared to many hours of work for humans, according to their announcement.

    This feature is currently only available for Pro users. Although it has not been tested yet, many articles emphasize the impressive capabilities of this new tool. If you're still unsure what Deep Research can do, just imagine this: Tell it "I need research information on global coffee consumption from last year." That's it! Just sit back and wait a moment for it to search and compile the results, sending you a detailed report. Wow, that's pretty amazing!

    Immediately, huggingface published an article attempting to recreate this tool in their own way. Details at Open-source DeepResearch – Freeing our search agents. And it's no surprise that both have the flavor of AI Agents.

    » Read more
  • Living long enough in the Internet world, you can see that people here are quite eager to follow trends, and they spread rapidly at a dizzying pace.

    Just a few months ago, we were still astonished by the intelligence of large language models (LLMs) that could answer just like humans, and shortly after, they were updated with incredible thinking and reasoning capabilities. They are widely applied not only in programming fields. Recently, the term AI Agents has been creating a stir.

    So, what are AI Agents? In this short article, it is, of course, impossible to provide a brief yet comprehensive definition. Readers can refer to this very detailed article here Agents | Chip Huyen. To make it easier to visualize, AI Agents can be thought of as a person or some entity. The Agents themselves are equipped with all the necessary tools. From these, Agents can combine them to complete a task that we assign.

    Still a bit vague, right? A practical example is when you command the Agents to access Facebook every 8 PM, check for any prominent news from friends, and then send a summary to Telegram. That's it!"

    » Read more
  • I just discovered the idb-keyval library that helps implement a key-value database simply. As shared in the series of posts about the process of making OpenNotas, I was struggling to find a type of database for storage, and it seemed quite difficult; in the end, I settled on localForage.

    idb-keyval is quite similar to localForage, but it seems to be doing a little better. For example, it has an update function to update data, simply imagine it like this:

    update('counter', (val) => (val || 0) + 1);

    Unlike the set function, which completely replaces the data.

    » Read more

Issue

Hello readers of 2coffee.dev. I wonder if anyone here has been using command-line applications (CLI)? If so, why did you choose it over a graphical user interface (GUI)? If I remember correctly, I have written a few articles about the process of creating some applications for myself. Honestly, for me, there are many cases where CLI proves to be much more useful.

Recalling my early days of switching from Windows to Linux, specifically Ubuntu. I have cursed countless times about this damn operating system. The interface is ugly, hard to use, and everything requires typing commands; how can anyone remember all those lines? It’s not like Windows is good. The interface is intuitive and easy to use. You just need to look to know where to click, everything is visible.

True to the saying, "what you hate, fate gives you". In the end, I became "addicted" to this command-line typing without even noticing. There must be a reason for this addiction; it helps me approach problems faster. Instead of moving the mouse around, I can just type, which is much quicker. Not to mention that many software applications are provided in CLI form, which in turn leads to the creation of management tools, allowing us to easily install our favorite applications with just a single command.

That was also the inspiration for me to tackle problems using CLI. Most of it is to help manage my blog. In the early days of exploring, there were so many new things that I didn’t know. It took a lot of time to learn while doing. Understanding that psychology, today I will compile some libraries that I know from my previous learning process. These libraries not only help you create quality applications but also make them more appealing to users.

Oh! All of these are Node.js libraries. If you are using other platforms like Go, Rust… I’m sure there are similar libraries. You just need to search based on the ideas of the libraries below.

Let's get started!

Framework

First, we need to talk about the framework; this is where you structure your CLI application. Imagine the framework helps shape, structure the code, and features to fit the nature of the CLI.

Notice that we often see command-line applications using a format like this:

$ mycli image --resize 512:512 /path/to/image

With mycli being the application name, image is almost a function, and the flag --resize is typically used to specify additional attributes. Finally, /path/to/image points to the data that needs to be processed.

If you are familiar with Node.js, you will see that CLI is quite similar to the node command, while the following parameters can easily be captured through the process.argv variable.

$ node index.js image --resize 512:512 /path/to/image
// console.log(process.argv); ['node', 'index.js', 'image', '--resize', '512:512', '/path/to/image']

In other words, just parsing process.argv is sufficient to classify and call the corresponding functions with the used parameters. At that point, the application is no different from a CLI.

yargs and commander are two foundational libraries to help us parse parameters as mentioned above. They provide very basic functions to serve as a foundation for other libraries.

oclif is one example. This tool helps us create powerful CLI applications by optimizing the workflow. oclif defines everything, from the directory structure to the help commands generated for new features. All you need to do is focus on writing the logic. After a "build" step, oclif produces a complete CLI application, including usage instructions without requiring you to do many additional steps.

Configuration File

In an application, apart from the default configurations or configurations hardcoded in the code, sometimes user configuration is still needed.

For example, you provide the user with the ability to set the input/output paths for files after processing. This option needs to be saved somewhere for future use. The easiest way is to create a text file and store all the information in it. Or more professionally, you can use the cosmiconfig library.

cosmiconfig is a library that automatically fetches configuration files into your application. cosmiconfig supports many formats such as .json, .yaml, .yml... After loading, cosmiconfig puts all the values into a variable that you can use in your application.

Input

Input is an essential part of CLI applications; besides receiving user data through flags like --resize, there are many other optimized ways.

inquirer is a library that provides many ways to receive user data. For example, displaying a text input box, yes/no questions, or select options... each time a certain command is typed.

Output

Have you ever used a CLI application that shows beautiful "loading" effects during interaction? Or lines of text with various colors to highlight important information? There are many libraries to help us achieve this.

ora is a library that creates beautiful "loading" effects. These effects are often used to signal to users that processing is underway and they need to wait. ora provides many different "spinning" shapes. Additionally, you can combine it with text to create continuous messages on the screen, indicating the progress of the ongoing process.

progress helps you create "downloading" effects using a combination of ASCII characters.

Sometimes you will need to draw a table to display information for output, cli-table3 is very suitable for this. Or if you simply want to enclose content in "boxes", boxen is a perfect choice.

Additionally, you can combine with chalk to style the text. This is a great library to change colors, add effects like "bold", "dim", "italic"... to highlight content. Or if you prefer a "rainbow" effect, gradient-string is unbeatable.

Utils

In addition to the main libraries mentioned above, there are still many other utility libraries that CLI can use.

open helps us open something like images, web addresses, or even applications. This library proves useful when we need the assistance of an external application that the CLI cannot handle.

clipboardy helps read data from the clipboard or issue commands to copy anything you want.

shelljs is used to run any other command-line programs through the shell.

And many other libraries that I cannot list all here.

Conclusion

Above is a compilation of some libraries that assist in creating a CLI application for Node.js. As a CLI enthusiast, I have researched and applied quite a few useful libraries in my applications. What about you? Are you using any additional tools? Please leave a comment for me and everyone to know. Thank you!

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...
Scroll or click to go to the next page