neha nupoor

@nupoor_neha

Learn to create custom CLI tool with Node and 5 sticky notes.

It’s easy!

I was going through some React article and saw create-react-app command. That made me realise I never bothered checking how to create your own CLI tools like Vue-Cli, create-react-app. So I looked it up, and it is very easy to create a basic CLI tool for yourself.

You can create a minimal one with just one configuration in your package.json. But I wanted a basic tool, with input from the user as well as multiple custom commands. So I came across inquirer-js and commander-js to do just that.

We will create our magical command “Cthulhu”, and then we would “wake” him up.

Below is the Inquirer, that takes user’s input to pass to the command. Here I’ve created inquirer in a “prompt” function, and the input prompts for the user are in “ip” array.

Take input from the user

Once we know what questions we are going to ask the user via inquirer, we define our custom commands. Here we create command “wake” to wake up our Cthulhu.

Define your own commands

Now the last and the most important steps:

Convert your own commands to shell commands

1. We add #!/usr/bin/env nodeto the top of the js file where we defined our command using commander-js.
2. Add the following line to our package.json file.

"bin": {"cthulhu" : "path to file in point 1"}

3. Run the following command to build the symlink:

yarn link / npm link

And that’s it! That’s all is needed to create your own command that might just wake up Cthulhu. Go ahead, give it a try. Run your own command as below:

cthulhu wake 1200

Vue-CLI uses the same pattern here and defines the main vue command here.

I am open to learning and looking for constructive feedback. If you liked the article, do provide feedback!

P.S. Thanks to Ray Dalio’s Principles for providing background for my sticky notes. Next step is to learn to take better photos.

Topics of interest

More Related Stories