Yargs: Feature Request: (PR-AN-01) Global middleware

Created on 22 Apr 2018  路  3Comments  路  Source: yargs/yargs

Thank you for this awesome software! :)

Context

We built a cli tool to help us install, build, and run our applications. We are now trying to capture analytics about how this tool is being used by developers:

  • frequently used commands
  • common errors
  • etc ...

For this, we would like to log data on every command. To do this, we would need to execute a function in every handler. There are two main issues we are facing:

  • Violation of DRY Principle - we have roughly 40+ handlers.
  • Maintainability - anytime a new handler is added by a developer, he/she would need to remember to add the callback.

Some work has already been done in this direction but for an individual command ( see @Khaledgarbaya PR https://github.com/yargs/yargs/pull/881 ).

Proposal

A suggested solution is to add a yargs.middleware method which accepts a single callback or a list of callbacks. The method will extend the list of middleware passed on to each command ( see PR https://github.com/yargs/yargs/pull/1109 ).

Any feedback would be much appreciated.

Most helpful comment

Published this feature to npm with public access under @aorinevo/yargs.

Install

npm install @aorinevo/yargs

Example

const someMiddlewareFunc = argv => console.log('I\'m a middelware function: ', argv);
const someOtherMiddlewareFunc = argv => console.log('I\'m some other middelware function: ', argv);

module.exports = require('@aorinevo/yargs')
  .middleware([someMiddlewareFunc, someOtherMiddlewareFunc]).argv;

On each cli command, someMiddlewareFunc and someOtherMiddlewareFunc get called first - in the order in which they appear, then any command level middleware, and finally the handler for the command.

All 3 comments

Published this feature to npm with public access under @aorinevo/yargs.

Install

npm install @aorinevo/yargs

Example

const someMiddlewareFunc = argv => console.log('I\'m a middelware function: ', argv);
const someOtherMiddlewareFunc = argv => console.log('I\'m some other middelware function: ', argv);

module.exports = require('@aorinevo/yargs')
  .middleware([someMiddlewareFunc, someOtherMiddlewareFunc]).argv;

On each cli command, someMiddlewareFunc and someOtherMiddlewareFunc get called first - in the order in which they appear, then any command level middleware, and finally the handler for the command.

just landed this feature request, great work @aorinevo.

So Late to the party OMG this amazing, Thanks @aorinevo

Was this page helpful?
0 / 5 - 0 ratings

Related issues

proppy picture proppy  路  3Comments

DaelDe picture DaelDe  路  5Comments

alexreg picture alexreg  路  4Comments

alexreg picture alexreg  路  5Comments

steveharman picture steveharman  路  4Comments