Johann Savalle

@yasha.solutions

Product designer and full stack developper living in france
< Back to all articles

List of libraries to build cool NodeJS command lines apps

July 30, 2019

1. Chalk

Chalk is styling for the command line - add cool color and stuff. Chalk

const chalk = require('chalk');
const log = console.log;

log(chalk.blue('Hello') + ' World' + chalk.red('!'));

log(chalk.green(
  'I am a green line ' +
  chalk.blue.underline.bold('with a blue substring') +
  ' that becomes green again!'
));

2. Clear

Clear the terminal screen if possible

var clear = require('clear');
clear();

3. Commander (minimist/nomnom/etc.)

Argv on steroid - basically handle all the needs of of your cli parameters

var program = require('commander');

program
  .version('0.1.0')
  .option('-p, --peppers', 'Add peppers')
  .option('-P, --pineapple', 'Add pineapple')
  .option('-b, --bbq-sauce', 'Add bbq sauce')
  .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
  .parse(process.argv);

console.log('you ordered a pizza with:');
if (program.peppers) console.log('  - peppers');
if (program.pineapple) console.log('  - pineapple');
if (program.bbqSauce) console.log('  - bbq');
console.log('  - %s cheese', program.cheese);

Commander is probably the most popular (at least if measured in Github stars) but it is not the only one around.

minimist is also a widly used argument parser for command line stuff.

Also worth mentioning: yargs the pirate theme parser


clui

Clui is UI for command lines

Draw spinners, progress bars, etc.

Example:

var Sparkline = require('clui').Sparkline;
var reqsPerSec = [10,12,3,7,12,9,23,10,9,19,16,18,12,12];

console.log(Sparkline(reqsPerSec, 'reqs/sec'));

for the following result: result


Figlet

Figlet creates ASCII art from text

var figlet = require('figlet');

figlet('Hello World!!', function(err, data) {
    if (err) {
        console.log('Something went wrong...');
        console.dir(err);
        return;
    }
    console.log(data)
});

helloWorldwithFiglet


inquirer

inquirer Handle user interaction:

  • asking questions
  • parsing input
  • validating answers
  • etc.

General structure :

var inquirer = require('inquirer');
inquirer
  .prompt([
    /* Pass your questions in here */
  ])
  .then(answers => {
    // Use user feedback for... whatever!!
  });

Allow you to create some prompts like this one ;

[?] What do you want to do?
> Order a pizza
 Make a reservation
 --------
 Ask opening hours
 Talk to the receptionist

Configstore

Configstore is a configuration manager for your cli.

(It has a companion package electron-store)

const Configstore = require('configstore');
const packageJson = require('./package.json');
 
// Create a Configstore instance
const config = new Configstore(packageJson.name, {foo: 'bar'});
 
console.log(config.get('foo'));
//=> 'bar'
 
config.set('awesome', true);
console.log(config.get('awesome'));
//=> true
 
// Use dot-notation to access nested properties
config.set('bar.baz', true);
console.log(config.get('bar'));
//=> {baz: true}
 
config.delete('awesome');
console.log(config.get('awesome'));
//=> undefined
< Back to all articles