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

reloquent

v1.4.1

Published

Ask user configurable questions via read-line.

Downloads

91

Readme

reloquent

npm version Build status

Reloquent allows to ask users a question, a confirmation (y/n), or a series of questions via the read-line interface.

yarn add reloquent
npm i reloquent

Table Of Contents

API

There are 4 types of calls to the API:

  • ask a single question as a string;
  • ask a single question as an object;
  • ask multiple questions.
  • ask for a confirmation;

Their respective methods can be accessed via the import statement:

import ask, { askSingle, confirm } from 'reloquent'

Question Type

When asking a question which is not a string, the question object should have the following structure:

const q = {
  text: 'What is your name',
}
const q = {
  text: 'What is your name',
  validate(v) {
    if (!v.length) {
      throw new Error('Name required.')
    }
  },
}
const q = {
  text: 'What is your name',
  postProcess(v) {
    return `${v.toLowerCase()}`
  },
}

Default answer (shown to users in [default] brackets).

const q = {
  text: 'What is your name',
  defaultValue: 'Visitor',
}
const q = {
  text: 'What is your name',
  async getDefault() {
    await git('config', 'user.name')
  },
}
const q = {
  text: 'Please enter the password',
  password: true,
}

If both defaultValue and getDefault are provided, the result of the getDefault takes precedence:

const q = {
  defaultValue: 'I desire it much',
  getDefault() {
    return 'I desire it much so'
  },
}

getDefault will get precedence

When the password property is set to true, the answer will be hidden behind the * symbols.

import { askSingle  } from 'reloquent'

const Password = async () => {
  const res = await askSingle({
    text: 'Please enter the password',
    password: true,
  })
  return res
}
Please enter the password: ********

| Name | Type | Description | Default | | ------------ | --------------------------------------------------------------- | ----------------------------------------------------------------------- | ------- | | text* | string | The text to show to the user. | - | | defaultValue | string | The default answer to the question. | - | | password | boolean | Hide the inputs behind * when typing the answer. | false | | getDefault | () => (string | !Promise<string>) | The function which will get the default value, possibly asynchronously. | - | | validation | (answer: string) => void | The validation function which should throw on error. | - | | postProcess | (answer: string) => (string | !Promise<string>) | The transformation function for the answer. | - |

async askSingle(  question: (string|!Question),  timeout=: number,): string

Ask user a question via the CLI. Returns the answer to the question. If a timeout is passed, the promise will expire after the specified number of milliseconds if the answer was not given.

  • question* (string | !Question): The question to present to the user.
  • timeout number (optional): How long to wait before rejecting the promise. Waits forever by default.

Questions can be asked as a simple string.

import { askSingle } from 'reloquent'

(async () => {
  try {
    const answer = await askSingle('What brought you her', 10000)
    console.log(`You've answered: ${answer}`)
  } catch (err) {
    console.log()
    console.log(err)
    console.log('Nevermind...')
  }
})()
What brought you her: I guess Art is the cause.
You've answered: I guess Art is the cause.

Alternatively, Reloquent can ask a question which is passed as an object of the Question type, and return a string.

import { askSingle } from 'reloquent'

(async () => {
  const answer = await askSingle({
    text: 'Do you wish me to stay so long?',
    validation(a) {
      if (a.length < 5) {
        throw new Error('The answer is too short')
      }
    },
    defaultValue: 'I desire it much',
    postProcess(a) {
      return `${a}!`
    },
    async getDefault() {
      return 'I desire it much so'
    },
  })
  console.log(answer)
})()
Do you wish me to stay so long? [I desire it much]
I desire it much!

async askQuestions(  questions: !Questions,  timeout=: number,): !Object<string, string>

Ask user a series of questions via CLI and transform them into answers. Returns an object with keys as questions' texts and values as answers.

  • questions* !Questions: A set of questions.
  • timeout number (optional): How long to wait before rejecting the promise. Waits forever by default.

!Object<string, (string | !Question)> Questions: A set of questions.

import ask from 'reloquent'

const Ask = async () => {
  const questions = {
    title: {
      text: 'Title',
      validation(a) {
        if (!a) throw new Error('Please enter the title.')
      },
    },
    description: {
      text: 'Description',
      postProcess: s => s.trim(),
      defaultValue: 'A test default value',
    },
    date: {
      text: 'Date',
      async getDefault() {
        await new Promise(r => setTimeout(r, 200))
        return new Date().toLocaleString()
      },
    },
  }
  const res = await ask(questions)
  return res
}

If when provided with the following answers (leaving Date as it is), the result will be returned as an object:

Title: hello
Description: [A test default value] world
Date: [2/22/2020, 21:37:04] 

Result: {
  "title": "hello",
  "description": "world",
  "date": "2/22/2020, 21:37:04"
}

async confirm(  question: (string|!Question),  options=: !ConfirmOptions,): boolean

Ask a yes/no question. Returns true when answer was y and false otherwise.

  • question* (string | !Question): The question, such as "Add default options", or "Continue to delete?". The question mark can added automatically.
  • options !ConfirmOptions (optional): Options for the confirmation question.

ConfirmOptions: Options for the confirmation question.

| Name | Type | Description | Default | | ---------- | ---------------- | ------------------------------------------------------------------------ | ------- | | defaultYes | boolean | Whether the default value is yes. | true | | timeout | number | How long to wait before rejecting the promise. Waits forever by default. | - |

import { confirm } from 'reloquent'

const Confirm = async (question) => {
  const res = await confirm(question, {
    defaultYes: false,
  })
  return res
}
Do you wish to continue (y/n): [n] y

Result: true

Copyright