⚡️ Build your own CLI power tool using NodeJS

Dennis Bruijn / @0x1ad2
Devjam
Published in
4 min readJul 28, 2019

Hopefully, some of you can relate to the following problem. You buy yourself a new machine or get a new device from a client to work with. You go to the process of installing, updating, and configuring the device. Even worse when you’re done store your work in a safe place, sign off across the system and format your SSD.

What are we using

NodeJS
NodeJS is a cross-platform JavaScript run-time that executes JavaScript code outside of the browser. NodeJS lets developers use JavaScript to write command-line tools and server-side applications.

Enquirer
Enquirer is fast, easy to use, and lightweight system to build stylish, intuitive, and user-friendly prompts.
https://github.com/enquirer/enquirer

What are we building

For this use case, we can make ourselves a powerful command-line interface to automate some of these tasks. Since NodeJS allows us to write and execute code that primarily interacts with the system disk and network, tasks like downloading and installing binaries will become a breeze.

Setup a new project

Make sure you’ve got a terminal of choice set up (I use Zsh with oh-my-zsh). Also, get homebrew for Mac so we can fetch binaries and install them via CLI commands.

  1. ) Create a new directory called “node-power-cli”
$ mkdir ~/Projects/node-power-cli

2.) Traverse into that directory

$ cd ~/Projects/node-power-cli

3.) Grab the MIT license and add it to the project

$ npx license mit > LICENSE

4.) Add a .gitignore file to the project

$ npx gitignore node

5.) Generate a new package.json file

$ npm init -y

6.) Create a new file

$ touch power-cli.js

7.) Allow the file to be executed

$ chmod u+x power-cli.js

8.) Edit the new file

$ code power-cli.js
To execute your NodeJS script, you can use the following command $ ./power-cli.js

If you’ve not seen npx before, it searches locally to see if there is a command to run and executes it. If there is no local command, it will try to download, install the command from npm, and run it. This is useful when generating new projects and saves you from globally installing a lot of stuff.

Add and use Enquirer

1.) Install Enquirer

$ npm install enquirer — save

2.) Define your list of frequently used binaries

3.) Add multi-select prompt

To execute your NodeJS script, you can use the following command $ ./power-cli.js

Execute CLI commands

Now we get an answer from the multi-select prompt, and we can parse that to a function to generate a command-line string. After that use brew to install all the selected binaries.

Run it!

Now we’re done and we can run the program and see if it works the way we expected.

Select the binaries that you want and hit ENTER

That’s it. There’s your first “friendly & powerful” command-line tool that you can extend with all the possibilities you’ve got in mind — E.G., a feature that sets your default user preferences for macOS.

If you have any questions? Either comment on this story or tweet me at @0x1ad2 If you want to check out the project yourself here’s the Github repository.

Thank you for taking the time to read this story! If you enjoyed reading this story, clap for me by clicking the below so other people can see this here on Medium.

I work at Sytac.io; We are a consulting company in the Netherlands, we employ around ~100 developers across the country at A-grade companies like KLM, ING, ABN-AMRO, TMG, Ahold Delhaize, and KPMG. Together with the community, we run DevJam check it out and subscribe if you want to read more stories like this one. Alternatively, look at our job offers if you are seeking a great job!

--

--

Dennis Bruijn / @0x1ad2
Devjam

👾 01111001 01101111 ~ 🪁 Founder at KITE ~ 👨🏻‍💻 Full-stack consultancy (web & mobile) ~ 💛 Continuously betting on JS