paint-brush
Learn to create custom CLI tool with Node and 5 sticky notes.by@nupoor_neha
2,652 reads
2,652 reads

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

by neha nupoorMarch 13th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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 <a href="https://hackernoon.com/tagged/cli-tools" target="_blank">CLI tools</a> like <a href="https://github.com/vuejs/vue-cli/" target="_blank">Vue-Cli</a>, <a href="https://hackernoon.com/tagged/create-react-app" target="_blank">create-react-app</a>. So I looked it up, and it is very easy to create a basic CLI tool for yourself.
featured image - Learn to create custom CLI tool with Node and 5 sticky notes.
neha nupoor HackerNoon profile picture

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 node to 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.