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

async-autocomplete-cli

v1.0.0

Published

tool for prompting the user to select from a list of choices, where the choices are fetched asynchronously, and new choices are fetched as the user types

Downloads

11

Readme

async-autocomplete-cli

CircleCI semantic-release Commitizen friendly npm version

A CLI prompt that allows the user to select from a list of choices, where the choices are fetched asynchronously (e.g. from a request to a remote server). The user can type filter text and new choices matching that filter text will be fetched.

Based upon prompts; this basically a stripped-down fork of that repo.

Usage Example

npm i --save async-autocomplete-cli
const { asyncAutocomplete } = require('async-autocomplete-cli')

async function go() {
  const instance = await asyncAutocomplete({
    message: 'Select an AWS EC2 Instance',
    suggest: async (input, cancelationToken, yield) => {
      const results = []
      if (!input) {
        // getRecentSelectedInstances not implemented in this example.
        results.push(...(await getRecentSelectedInstances()))
        yield(results)
      }

      const Filters = []
      if (input)
        Filters.push({
          Name: 'tag:Name',
          Values: [`*${input}*`],
        })
      const args = { MaxResults: 100 }
      if (Filters.length) args.Filters = Filters
      const request = ec2.describeInstances(args)
      cancelationToken.on('canceled', () => request.abort())

      if (cancelationToken.canceled) return []

      const { Reservations } = await request.promise()
      for (const { Instances } of Reservations || []) {
        for (const Instance of Instances || []) {
          const { InstanceId, Tags = [] } = Instance
          const name = (Tags.find(t => t.Key === 'Name') || {}).Value
          results.push({
            title: `${InstanceId} ${name || ''}`,
            value: Instance,
            initial: !results.length,
          })
        }
      }

      if (!results.length) {
        results.push({
          title: `No matching EC2 Instances found${
            input ? ` with name starting with ${input}` : ''
          }`,
        })
      }

      return results
    },
  })
}

go()

API

asyncAutocomplete(options)

The suggest function will be called with:

  • The user input (a string, may be empty)
  • A cancelationToken. This is an EventEmitter that will event 'canceled' when the user input has changed. It also has a canceled property that is initially false and becomes true when the user input has changed. You must handle the 'canceled' event if you want choices for the latest user input to load ASAP.
  • A yield function you can call with an array of choices objects [{ title, value }, ...]. You can also return the final choices, but calling this function allows you to change the choices even if the user input remains the same. For example, you might want to save what the user has recently selected to a local file, and show the recent selections immediately while waiting to load other choices from the server.

Options

| Param | Type | Description | | ---------- | :--------: | ------------------------------------------------------------------------------------------------------------------ | | message | string | Prompt message to display | | suggest | function | Function to fetch choices | | limit | number | Max number of results to show. Defaults to 10 | | style | string | Render style (default, password, invisible, emoji). Defaults to 'default' | | clearFirst | boolean | The first ESCAPE keypress will clear the input |