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
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » 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...