Closed agonbina closed 7 years ago
Thanks for suggestion, will implement in future releases.
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
@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.
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] = ...
?
@agonbina Currently no, this.global
is the only way to share data that Ronin provides.
:+1: I'll just use this.global[middleware-id]
when I need it
would be great if we can choose to run the middleware functions either before or after
.run
Instead ofuse
: