Black Sheep Code

This article is a part of the series "Tools I like"

Tools I like: @inquirer/prompts

Published:

I love @inquirer/prompts.

It Allows you to very easily create an interactive CLI menu like this:

Screenshot showing an interactive CLI menu asking the user to fill their firstname, lastname and phone number.

Creating such prompts is dead easy with a nice async interface:

import { input } from '@inquirer/prompts';

function isValidPhoneNumber(phone) {
  // Simple validation: must be 10-15 digits, can include spaces, dashes, parentheses, and plus sign
  const cleaned = phone.replace(/[\s\-().+]/g, '');
  return /^\d{10,15}$/.test(cleaned);
}


async function main() {
    const firstName = await input({ message: 'What is your first name' });
    const lastName =  await input({ message: 'What is your last name' });
    const phoneNumber = await input({message: "What is your phone number", 
        validate: (value) => {
            if(isValidPhoneNumber(value)){
                return true;
            }
            return "Must be a valid phone number."
        }
    })
}

main();

Brilliant.

This make for nice convenient utilty tools for creating test data or doing migration tasks etc.



Questions? Comments? Criticisms? Get in the comments! 👇

Spotted an error? Edit this page with Github