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.