jondot / hygen

The simple, fast, and scalable code generator that lives in your project.
http://www.hygen.io
MIT License
5.67k stars 255 forks source link

How to use hygen generator when prompts can hang #313

Open blimmer opened 3 years ago

blimmer commented 3 years ago

I have three generators:

  1. handler - this generator basically just delegates between the other two generators based on a prompt
  2. batch-handler - handles batches
  3. sequential-handler - handles one-at-a-time message processing

When you run hygen handler new, it prompts asking which kind of handler you want (batch or sequential):

Screen Shot 2021-04-29 at 3 46 48 PM

Based on your answer, it triggers the proper generator using this sh command:

---
sh: hygen "<% if (type === 'one-at-a-time') { %><%= 'sequential-handler' %><% } else if (type === 'batches') { %><%= 'batch-handler' %> <% } %>" new
---

Then, those handlers should prompt again, but it appears to just "hang":

Screen Shot 2021-04-29 at 3 48 07 PM

I'm assuming this is because stdin isn't being passed through to the sh command.

If I run the sub-generator separately, it does prompt properly:

Screen Shot 2021-04-29 at 3 50 18 PM

I have an example of this issue in a sample repo: https://github.com/blimmer/hygen-issue

jondot commented 3 years ago

i think maybe the right way for you to do that is to switch to "script mode" where actually you build your own workflow:

https://github.com/jondot/hygen/blob/master/src/__tests__/metaverse/hygen-templates/_templates/recursive-prompt/new/prompt.js

blimmer commented 3 years ago

I see what you mean, but I'm not quite sure that meets my exact use-case. I basically have two generators and I want to guide the user through a series of prompts to choose the proper hygen generator to run. I could always just write a separate enquirer prompt to do this, probably. I was originally thinking it would be nice to keep everything in the same framework, though.

airtonix commented 3 years ago

To add to this, lets say you have the following:

image

Here, a package is the base output type, so :

So I want all the generated workspace package types to have all the files and attributes of the package generator output, but i then want to do extra stuff.

---
sh: >-
  yarn generate new package
  title:<%= title %>
  code:<%= code %>
  package_name:<%= package_name %>
  package_path:<%= package_path %>
  main_export:<%= main_export %>

---
---
sh: mv packages/<%= code %> tools/
---

but it hangs on tools/generators/new/cli/1-create-package.ejs.t

airtonix commented 3 years ago

So i figured this out by :

yarn generate new cli --title my awesome thinger
# ...asks a bunch of questions providing a mutated version of `title` as an initial value to each

Now because interactive is true by default you get asked all the questions.

if you instead provide --no-interactive then it will only succeed if you provide all the expected parameters (fails otherwise)

You won't find a use of this topic in that repo, but I'll paste it here from another repo:

tools/generators/new/cli/01_generate_package.ejs.t

---
sh: >-
  yarn generate new package
  --no-interactive
  --title <%= title %>
  --code <%= code %>
  --package_name <%= package_name %>
  --package_path <%= package_path %>
---

This ends up creating what I expect and then any other parts of my new/cli generator take effect.

vospascal commented 3 years ago

@airtonix you could check it it’s in the args and then use the skip methode