This package has been deprecated

Author message:

yargs now supports async commands

yargs-promise-handler

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i yargs-promise-handler

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

6.04 kB

Total Files

5

Last publish

Collaborators

  • satazor