hygen
hygen copied to clipboard
Support lifecycle hook
I would like to suggest that provide that lifecycle hook of the generator, like afterAll, beforeAll, beforeEach and afterEach. Now, actually, we have afterEach when we pass sh in template files.
However, in the case, we might be run the script after all code generated. I hope that we can set it at .hygen.js, like
module.exports = {
afterAll: (context) => {
...
}
}
Now, we can do it a workaround as prescript && hygen <name> <action> && postscript. But still hope that we could have the hook. That might be also helpful on #175.
Thanks!
One other way to create a makefile to help wrap the before and after generation. But adding a hook on this would be very useful.
Intersting. So you want a "post" action after all generation has completed. The way I solve it usually is have:
component/
new/
0_create-component.t
1_inject-dependency.t
zz_post-action.t <-- this action runs a shell script (in this case 'yarn install') and it appears in the end
Does this work for you?
@jondot It's better that we have a script hook rather than putting a mock post-action.t file. The reason is that we don't want to generate a file without the template we need. That means that we still need a process to delete all script placeholder files.
Adding on to this, having lifecycle hooks for the following would be helpful
- before arg parsing
- before generation but just after arg parsing
- before/after hooks for each generated template/file
- after all generation is done
I'm not sure where how the end user would configure this but for context we've run into a situation where we want to run a script after a certain generator has completed but only if a specific option was passed or if a specific file was generated/modified (instead of skipped for example).