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

@yankeeinlondon/ask

v0.2.9

Published

Provides two builder patterns to wrap around the popular inquirer package

Downloads

2

Readme

Ask

two builders wrapped around the popular inquirer package to make asking questions even easier.

Overview

I use the inquirer package every few years but each time I go through a learning curve which I wish I didn't have to go through. This repo is an attempt to simplify the process by:

  • using builder patterns where possible
  • bring the documentation into the type system (and therefore closer to the user)
  • improve question composition so that interactive flows are easy to build

Installation

pnpm install @yankeeinlondon/ask

Usage

There are two main builder patterns which will be used in Ask:

  1. ask - for building questions
  2. survey - for composing questions into an interactive flow

Ask Builder

By importing ask from this repo, you'll get an API surface which provides a standardized way of composing any of the questions provided in the core inquirer package:

import { ask } from "@yankeeinlondon/ask";

const name = ask.input("name", "What is your name?");
const age = ask.number("age", "How old are you?");

These questions can be asked simply by calling the result as an async function:

const answers = {
    name: await name(),
    age: await age()
}

Choices

Many of the question types -- such as select, checkbox, rawlist, and expand -- ask that you provide a list of choices for the user to choose from.

For questions which have choices, the third parameter -- after the property name and message/prompt -- will be those choices. You have several options in which you can express these choices so let's review them:

/**
 * a simple array.
 *
 * The elements in the array become both the keys and values of the
 * choices.
 */
const color = ask.select(
    "color",
    "What is your favorite color?",
    ["red","blue","green"]
);

// using a simple key/value notation
// -----------------------------------------------------------
// the KEYS are the "names" of the choices, the VALUES are
// the actual value the answer will return.
const color_obj = ask.select(
    "color",
    "What is your favorite color?",
    {
        Red: "red",
        Blue: "blue",
        Green: "green"
    }
);

/**
 *  Using a key/value where value is a tuple:
 *
 * this allows you set both the value AND a description
 */
const color_obj = ask.select(
    "color",
    "What is your favorite color?",
    {
        Red: ["red", "Red like a rose"],
        Blue: ["blue", "Blue like the sky"],
        Green: ["green", "Green like grass"]
    }
);

/**
 * Using the DictProxy shorthand
 *
 * this allows any of the props available in the fully qualified
 * `Choice` type from being expressed:
 *
 * - the "key" is the "name"
 * - you must state the "value"; otherwise all other props
 * are optional
 */

const color_proxy = ask.select(
    "color",
    "What is your favorite color?",
    {
        Red: { value: "red", description: "Red like a rose" },
        Blue: { value: "blue", key: "b" },
        Green: { value: "green", short: "gr" }
    }
)

Any question type which has choices provides the same call signature and variants for representing the choices.

Options

Many questions, share some key options, but all options present only the options relevant to themselves as a question type.

Where possible, we have attempted to increase the commonality across question types. Examples include:

  • default is found on some of the core inquirer commands but oddly missing in others -- like checkbox -- so we've extended it to work here too.

survey Builder

  • The survey builder's intent is to aid in the composition of questions and interactive flows and to make the process as seamless as possible.

  • The API will look something like this:

    import { ask, survey } from "@yankeeinlondon/ask";
    
    const q1 = ask.input(...);
    const q2 = ask.input(...);
    const q3 = ask.input(...);
    
    const mySurvey = survey(q1,q2,q3);
  • this API will accept any number of questions and ensure the following:

    • each question at runtime will be run in the specified order
    • at design time, if a question has "requirements" (aka, configured using the withRequirements() part of the ask() builder API -- a type error will be raised unless a question prior to this dependant question provides this information (aka, meets the requirement).
  • when a survey is configured (as is seen the above example) we are then exposed to a simple API surface which exposes a start() function:

    export type ConfiguredSurvey<...> = {
      start<T extends Record<string, unknown> | undefined>(initialState?: T) => Answers
    }
  • this start() call offers an optional way to start with a known state or leave it undefined for no initial state

  • the end result is a dictionary of key/values which is the aggregation of each question's response