Git Product home page Git Product logo

yargs-promise-handler's Introduction

yargs-promise-handler

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status Greenkeeper badge

Adds support for promises to yargs handlers and allows them to be composable.

NOTE: This module is deprecated because yargs now support async commands!

Installation

$ npm install yargs-promise-handler --save

Why?

yargs does not support command handlers that return promises. Users must handle promises and call process.exit() manually.

This packages does exactly that under the hood and exposes the original promise under handler.promise() to allow handlers to be composable.

Usage

// commands/random
export const command = 'random';
export const describe = 'A command that either fails or succeeds based on randomness';

export const builder = (argv) => argv;
export const handler = promiseHandler((argv) => Math.random() > 0.5 ?
    Promise.resolve() :
    Promise.reject(Object.assign(new Error('Oh noes'), { exitCode: 5 })));

The code above will either exit with code 0 or 5 if the promise either fulfills or rejects respectively.
Note that if the error.exitCode is undefined, it will default to 1.

The second argument of promiseHandler accepts an options object:

  • logError: Call console.error(err) before exiting if the promise fails, defaults to true

Tests

$ npm test
$ npm test -- --watch during development

License

MIT License

yargs-promise-handler's People

Contributors

greenkeeper[bot] avatar satazor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.