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

@fcostarodrigo/ask

v3.1.0

Published

Wrapper of yargs and enquirer to ask user for input.

Downloads

10

Readme

Ask

Wrapper of yargs and enquirer libs to ask user for input.

Install

npm i @fcostarodrigo/ask

Usage

Command line arguments

Create command line arguments. Each command line argument can be passed through the command line, environment variables and .env file. If the argument is missing and required, the user is prompted for it. Arguments are required by default. The options --help and --version are created automatically.

Pass an object describing each command line argument, the returned promise is an object of the same shape with the values extracted.

import { askArgv } from "@fcostarodrigo/ask";

const { password } = await askArgv({
  password: {
    type: "password",
  },
});

Passing value using the command line.

node src/test.js --password xxx
# no prompt

Passing value using environment variable.

PASSWORD=xxx node src/test.js
# no prompt

Passing value using the .env file.

echo PASSWORD=xxx > .env
node src/test.js
# no prompt

Prompt the user for the missing value.

node src/test.js
# prompt for password

For each command line argument you can also specify:

  • yargsOverrides Override yargs lib configuration.
  • enquirerOverrides Override enquirer lib configuration.
  • defaultValue Default value.
  • options Possible options for strings and array types.
  • required If the value is required or not.

Positional arguments

import { askArgv } from "@fcostarodrigo/ask";

const { username, password } = await askArgv({
  username: { type: "string", position: 0 },
  password: { type: "password", position: 1 },
});
node src/test.js john xxx

.env location.

You can also pass the location of the .env file as the second parameter.

import { askArgv } from "@fcostarodrigo/ask";

const { username } = await askArgv({ username: { type: "string" } }, { dotEnvConfig: ".dev.env" });
echo USERNAME=john > .dev.env
node src/test.js

User input

You can prompt the user in the middle of your program without defining new command line arguments.

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

const password = await ask({ name: "password", type: "password" });

Environment variables only

You can use askEnv to get values from environment variables alone. An error will be thrown if the environment variable is required, missing and there is no default value for it. You can pass dotEnvConfig as a field of the object of the second argument. The value will be parsed according to the type. Arrays are split by a single , and booleans are true if they match the string true when converted to lower case.

const { username, password } = askEnv({
  username: { type: "string" },
  password: { type: "password" },
});
  • required If the value is required or not.
  • defaultValue Default value.
  • options Array of allowed values.

Supported types

| Type | options | yargs | enquirer | | -------- | ------- | --------------------------------------------- | --------------------------------------------------------------------------- | | array | no | array | list | | array | yes | array | auto complete | | boolean | | boolean | confirm | | number | | number | numeral | | string | no | string | input | | string | yes | string | auto complete | | password | | string | invisible |

Changelog

Changelog

License

MIT License