Today we are going to build a cli (command line interface) to search stack overflow 😎. I just love the simplicity of a character user interface. No bs, just gets the job done. Period.
To build a cli we will be using node js and some external node packages. This application will be simple one, so it’s your job to run wild with your imagination with constrains of character user interface.
So let the learning begin ⭐
First thought when it comes to cli is how do make your cli name appear everywhere? Well npm makes life easier here. I will come back to that later.
Step 1: Create a new npm project and install dependencies
npm init
The name of the project you give here will the name of the command :P. Yes! this is what you will be using.
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Dependencies
- colors
- commander
- request-promise$ npm install colors commander request-promise --save
colors: This package is for making the output on the console a bit nicer and cleaner than the standard output
commander: This package is the important package which will help us getting the input from the console, setting up flags and documentation for the cli tool
request-promise: This one we will be use it for making http calls to external service or api, in this case stackoverflow api
Step 2: Now we will import these packages into to our main index.js file
const program = require('commander');
const request = require('request-promise');
const colors = require('colors/safe');
Step 3: Using commander we ll set up our cli details and documentation
Here in this set up, command function takes in the template, alias is short form for the command, description will take a short description for the -help command and finally action takes a function which will be called and attribute contains the user input when user types the command.
Step 4: Ping the stackoverflow api.
In the above program we have written function to make a http get request to stackoverflow advanced search api. Make sure you add json:true and gzip:true as options so as to get the output in clean json format.
Once we get the response we ll parse the json. The key item contains the list of questions objects. We will loop through the list and get the title and link of each question object. We are doing a javascript reverse() because the once we print the result we would have to scroll to get the top results.
To print the result we just have to console.log() the title and link :P. Using colors we will add colour and styling to the output.
Step 5: Now that our program is ready we have to make our cli program be used with command name and be available universal.
On the top of the program insert this line
#!/usr/bin/env node.....
Next in your command line where your package.json file resides execute this command
npm link
Final code
Github repo
Demos
Well folks that’s it! Your cli app must be available globally and with your cli name ❤
Let me know what you think and what you are building using simple character user interface.