hygen icon indicating copy to clipboard operation
hygen copied to clipboard

Support lifecycle hook

Open LucienLee opened this issue 5 years ago • 4 comments
trafficstars

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!

LucienLee avatar Feb 24 '20 11:02 LucienLee

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.

gnomeria avatar Mar 28 '20 09:03 gnomeria

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 avatar Jul 03 '20 12:07 jondot

@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.

LucienLee avatar Jul 07 '20 16:07 LucienLee

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).

Whoaa512 avatar Jul 14 '20 21:07 Whoaa512