a blog for those who code

Friday, 5 August 2016

How to create Simple JavaScript CLI with Node.js

In this post we will be discussing about how to create JavaScript Command Line Interface (JavaScript CLI) with Node.js. According to Wikipedia, A command line interface or command language interpreter, also known as command-line user interface, console user interface and character user interface, is a means of interacting with a computer program where the user issues commands to the program in the form of successive line fo text (command lines).

To build CLI with Node.js we will be using the below modules

1. Inquirer : A collection of common interactive command line user interfaces.
2. Chalk : Terminal string styling.

We will be creating a new project using npm init and and install the above dependencies. Our package.json will look like below :


In order to demonstrate Chalk we will display name of the package that is "Javascript CLI" as shown below

var chalk = require('chalk');
var inquirer = require('inquirer');

console.log(chalk.blue('JavaScript CLI'));


Prompting User in CLI


We will be using inquirer which will prompt the user to get some information as shown below

var chalk = require('chalk');
var inquirer = require('inquirer');

var questions = [
{
  name: 'name',
  type: 'input',
  message: 'Enter the name of the application',
  validate: function(value) {
    if(value.length) {
      return true;
    } else {
      return 'please enter the name of the application'
    }
  }
},
{
  name: 'description',
  type: 'input',
  message: 'Enter the name of the description',
  validate: function(value) {
    if(value.length) {
      return true;
    } else {
      return 'please enter the description of the application'
    }
  }
},
{
  name: 'visibility',
  type: 'list',
  message: 'Public or private',
  choices: ['public', 'private'],
  default: 'public'
}
];

var lastQuestion = [{
  name: 'confirm',
  type: 'list',
  message: 'Yes or no',
  choices: ['yes', 'no'],
  default: 'yes'
}];

inquirer.prompt(questions).then(function (answers) {
  console.log(chalk.yellow('You have written below answers'));
  console.log(chalk.yellow('Name of the Application' ),chalk.green(answers.name));
  console.log(chalk.yellow('Description of the application'),chalk.green(answers.description));
  console.log(chalk.yellow('Visibility of the application'),chalk.green(answers.visibility));
  inquirer.prompt(lastQuestion).then(function(lastanswer) {
    console.log(chalk.yellow('Thanks for the answers'));
  });
}); 

As you can see in the above code we have called prompt function of inquirer where we have passed an array of question which will be displayed to the user as shown below. We have also added one more question at the end once done will give us the answers.


There are number of possibility you can do with the above approach. Same like npm init we can get user information before initializing. This approach can also be used to create a git initialization.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

Best Hosting Plans

CC License