vadimdemedes / ronin

Toolkit for killer CLI applications
http://vdemedes.github.io/ronin
MIT License
299 stars 15 forks source link

run middleware before and after #14

Closed agonbina closed 7 years ago

agonbina commented 9 years ago

would be great if we can choose to run the middleware functions either before or after .run Instead of use:

{
  before: [  'midd1'  ],
  after: [ 'midd2' ]
}
vadimdemedes commented 9 years ago

Thanks for suggestion, will implement in future releases.

agonbina commented 9 years ago

Another recommendation would be to add an api to easily save data from the hooks in the global scope: read-package.js hook

function(next) {
 // Open package.json if it exists and store its contents
  var pkg = ...
  this.state({
    version: pkg.version,
    dependencies: pkg.dependencies
  })
  next()
}

In my commands I can use the read-package middleware and retrieve the state:

use: [ 'read-package'],
run: function() {
  var pkg = this.hook('read-package').state()
  // pkg.version === '1.0.0'
  // pkg.dependencies === { ... }
}

This way you can very easily add common middleware that is used in different commands and have a clear convention of how the global state is accessed, instead of adding random properties to this.global

vadimdemedes commented 9 years ago

@agonbina I think this does not fit with the middleware/hooks concept. I imagine it as a chain of functions, that can affect the path to the main command or alter its behavior. For this use case I would rather make a separate script in lib/read-package.js and use it as a helper function.

agonbina commented 9 years ago

I think in frameworks like express you can attach data to the middleware chain, like the req object thats passed in as an arg to each middleware function. But yeah for that use case I could do as you said, but still I don't see a clean way to share state between "hooks" as they are running, besides this.global[prop] = ...?

vadimdemedes commented 9 years ago

@agonbina Currently no, this.global is the only way to share data that Ronin provides.

agonbina commented 9 years ago

:+1: I'll just use this.global[middleware-id] when I need it