npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rasch/prompt

v0.1.1

Published

an interactive prompt for JavaScript CLI applications

Downloads

109

Readme

prompt

an interactive prompt for JavaScript CLI applications

Install

pnpm add @rasch/prompt
npm install @rasch/prompt
yarn add @rasch/prompt

Quick Start

import { prompt } from "@rasch/prompt"

const name = await prompt("What's your name? ")
const password = await prompt("Please enter your password: ", { silent: true })

console.log(`Welcome back, ${name}!`)

There are additional examples available!

API

Options :: { silent: Boolean, completions: [String], repl: Boolean, handleInput: HandleInput }
HandleInput :: (String, Interface, Options) => Undefined

prompt :: (String, Options) -> Promise(String)
prompt(query, options)

Import the Module

import { prompt } from "@rasch/prompt"

Simple Example

The original reason for this module is to hide the user input during password authentication. The input is not printed to the console (like unix system passwords) when the silent option is set to true and it is not saved to history.

const password = await prompt("Enter Password: ", { silent: true })

This prompt module can work for simple use cases...

const answer = await prompt("Which way do you want to go? ")

...but is not really needed here, since the built-in rl.question does nearly the same thing.

Completions

Tab autocomplete is available by providing an array of strings to the completions option. Completion is performed by simply filtering the array based on what the current input starts with. Sorry, there is no fuzzy filtering (currently).

const answer = await prompt(
  "Which way do you want to go? ",
  { completions: "north south east west up down left right".split(" ") }
)

REPL

A READ-EVAL-PRINT Loop is available by setting the repl option to true. (Please use your favorite robot voice when reading the italic text in the above sentence. Actually, that is the correct way to read ALL italicized text.)

const answer = await prompt(
  "Which way do you want to go? ", {
    repl: true,
    completions: "north south east west up down left right".split(" "),
  }
)

The REPL above is useless since it doesn't handle the user input. The option handleInput should be provided with a callback function. The callback function accepts up to three arguments:

  • input: the trimmed string provided by the user
  • rl: the Readline Interface which provides access to methods such as rl.history, rl.setPrompt, and rl.write.
  • opts: the object that was provided to the prompt module. This allows the options, including handleInput itself, to be dynamically modified.
const answer = await prompt(
  "Which way do you want to go? ", {
    repl: true,
    completions: "north south east west up down left right".split(" "),
    handleInput(input, rl, opts = {}) {
      switch (input) {
        case "north":
        case "up":
          opts.repl = false
          enterCastle()
          break
        case "east":
        case "right":
          console.log(
            "You entered the infinite Forest and somehow ended\n" +
            "up back where you started."
          )
          break
        case "south":
        case "down":
          opts.repl = false
          ohCoolASkatePark("Mission over! Let's skate instead.")
          break
        case "west":
        case "left":
          opts.repl = false
          enterPit("You fell in a deep pit just west of the castle!")
          break
        default:
          console.log(
            `You can't go "${input}". I don't even know what that is.`
          )
      }
    },
  }
)