New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

interactive-cli

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interactive-cli

Making it easy to create interactive command line scripts in Node.js

  • 1.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
increased by755.78%
Maintainers
1
Weekly downloads
 
Created
Source

Interactive CLI for Node.js

The Interactive CLI is a library that makes it easy to create interactive command line scripts in Node.js by utilizing promises.

npm version

Installation

Using npm:

npm i --save interactive-cli

Example interaction:

$ node examples/basic-usage.js --example

Would you like to
a) Create a new user
b) Delete a user
q) Quit
?:  a

Enter user's email
email:  leonaro@dicaprio.com

What's the name for the user
firstname:  Leonardo
lastname:  DiCaprio
User Leonardo DiCaprio was created successfully!

All DONE!

Would you like to
a) Create a new user
b) Delete a user
q) Quit
?:  b

Which user would you like to delete?
a) Leonardo DiCaprio
b) Jennifer Lopez
q) Quit
?:  b

Are you absolutely sure?
continue? (y/n):  y
User Jennifer Lopez was successfully deleted!

All DONE!

Would you like to
a) Create a new user
b) Delete a user
q) Quit
?:  q

The Gist

const { promptFields, promptToContinue, promptOptions, startWith, onFinalError, exit, DontContinue } = require('../')

new Promise((resolve, reject) => {
  // Do some preparation, such as getting
  // a reference to a database or authenticating
  resolve({
    createUser: () => Promise.resolve(),
    listUsers: () =>
      Promise.resolve({
        '31725276-73a9-4830-aa77-a86fce4dd7f8': 'Leonardo DiCaprio',
        '53bd3330-4bd7-47c5-a685-f1039e043eae': 'Jennifer Lopez',
      }),
    deleteUser: () => Promise.resolve(),
  })
})
  .then((api) => {
    const initialOptions = {
      createUser: 'Create a new user',
      deleteUser: 'Delete a user',
    }
    const handler = (selection) => {
      switch (selection) {
        case 'createUser':
          return createUser(api)

        case 'deleteUser':
          return deleteUser(api)

        default: {
          throw new ExitScript(`Unknown selection "${selection}"`)
        }
      }
    }

    return startWith('Would you like to', initialOptions, handler)
  })
  .catch(onFinalError)
  .then(exit)

function createUser(api) {
  const user = {}
  return promptFields("Enter user's email", 'email')
    .then((email) => {
      user.email = email
    })

    .then(() => promptFields("What's the name for the user", ['firstname', 'lastname']))
    .then((res) => {
      user.firstname = res.firstname
      user.lastname = res.lastname
    })

    .then(() => {
      return api.createUser(user).catch((err) => {
        throw DontContinue('User could not be created because of error: ' + err.message)
      })
    })

    .then(() => {
      console.log(`User ${user.firstname} ${user.lastname} was created successfully!`)
    })
}

function deleteUser(api) {
  const data = {}
  return api
    .listUsers()
    .then((users) => {
      data.users = users
      return users
    })
    .then((users) => promptOptions('Which user would you like to delete?', users))
    .then((selection) => {
      data.selection = selection
      return selection
    })
    .then((selection) => {
      console.log('\n' + 'Are you absolutely sure?')
      return promptToContinue(selection)
    })
    .then((selection) => {
      api.deleteUser(selection)
    })
    .then(() => {
      console.log(`User ${data.users[data.selection]} was successfully deleted!`)
    })
}

License

MIT

FAQs

Package last updated on 13 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc