Adds support for promises to yargs handlers and allows them to be composable.
NOTE: This module is deprecated because yargs now support async commands!
$ npm install yargs-promise-handler --save
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.
// 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
: Callconsole.error(err)
before exiting if the promise fails, defaults totrue
$ npm test
$ npm test -- --watch
during development