Skip to content
This repository has been archived by the owner on Aug 7, 2018. It is now read-only.

moxystudio/yargs-promise-handler

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published