Skip to content

Latest commit

 

History

History
109 lines (89 loc) · 3.54 KB

08-design-beautiful-intuitive-and-secure-node-js-cli-user-input-experiences-with-enquirer.md

File metadata and controls

109 lines (89 loc) · 3.54 KB

08. Design Beautiful, Intuitive and Secure Node.js CLI User Input Experiences with Enquirer

Notes

Enquirer is a great foundation to build Interactive CLIs that don't leak tokens and passwords.

These are the built in prompts you get:

Installing:

yarn add enquirer

A basic enquirer prompt looks like this:

const { prompt } = require("enquirer");

// assuming you are in an async block
const response = await prompt({
  type: "input",
  name: "username",
  message: "What is your username?"
});

console.log(response); // { username: 'jonschlinkert' }

Once you're familiar with the prompt options API you can use them pretty much everywhere:

{
  // required
  type: string | function,
  name: string | function,
  message: string | function | async function,

  // optional
  initial: string | function | async function, // The default value to return if the user does not supply a value.
  format: function | async function, // Function to format user input in the terminal.
  result: function | async function, // Function to format the final submitted value before it's returned.
  validate: function | async function, // Function to validate the submitted value before it's returned. This function may return a boolean or a string. If a string is returned it will be used as the validation error message.

  // each command will also recognize additional options
  // e.g. `limit`, `choices`, `hint`, `fields`
}

Adding AutoComplete prompt:

const { prompt } = require("enquirer");

// inside async block
const response = await prompt({
  type: "autocomplete",
  name: "flavor",
  message: "Pick your favorite flavor",
  limit: 10,
  choices: [
    "Almond",
    "Apple",
    "Banana",
    "Blackberry",
    "Blueberry",
    "Cherry",
    "Chocolate",
    "Cinnamon",
    "Coconut",
    "Cranberry",
    "Grape",
    "Nougat",
    "Orange",
    "Pear",
    "Pineapple",
    "Raspberry",
    "Strawberry",
    "Vanilla",
    "Watermelon",
    "Wintergreen"
  ]
});

Personal Take

Principles to help your CLI be CI friendly:

  • Every prompt should have a corresponding flag that skips the prompt
  • Every prompt should inform the user about what flag they can use to skip the prompt (nice to have)