Add support for other enquirer prompts.
See original GitHub issue- I’d be willing to implement this feature (contributing guide)
Description
Enquirer is used by @nrwl/tao
to handle the prompts for generator options via the schema. Currently only the input
, select
, multiselect
, confirm
, & numeral
prompts are supported.
However, there are several other prompt types that would be very convenient to have for some generators, such as
list
prompts for when you want an array of items, but don’t want to constrain the options, likemultiselect
doesautocomplete
prompts for when you want to suggest options, but not constrain input to them, likeselect
doesinvisible
orpassword
might be good for secrets, if you have a generator that needs to access cloud resources/ private repos.- less obvious how it would be implmented, but
snippet
prompts would provide a very nice & clean way to make templating for generators that use EJS templates interactive
Motivation
I initially thought of this because I wanted to create a generator that would be able to add targets for running @nx-tools/nx-docker
to my apps. I was starting to lay it out and I realized that for a tags
input, there’s no effective way to prompt for a free-form list, either I make the schema option demand a single delimited string, and then manually split it in the generator’s code, or I don’t provide a prompt. The fact that enquirer provides a list
prompt that would be about a 5-line implementation was too tantalizing to ignore.
After looking at enquirer, I realized that several other prompt types might lend themselves very nicely to nx’s patterns (described above)
Suggested Implementation
The list
prompt type should be very straightforward to implement, as would the autocomplete
, invisible
, or password
prompts; just pass through the prompt type (if it’s one of those) and add some checks to validate that the type of the property matches what the prompt type returns.
Alternate Implementations
The snippet
prompt for doing interactive templating would require significantly more effort, and may not be feasible, but I imagining that it could probably be implemented look at the options allowed by the schema and then create a substitutions object to be used by EJS to render the template file into a template string that you can be passed to the snippet prompt. The problem here lies in the fact that the snippet prompt only returns the single, rendered template string and doesn’t have support for multiple templates or surfacing only what the filled in values were.
To have this jive well with the nx cli approach, you’d need to write a custom prompt based on the approach of the snippet prompt. It would need to be made aware of all of template files, maybe showing them all at once at once (ugly), iterate over them, or add some additional interactions to toggle between templates that still need to be filled in. You’d also want to have it return just the object of options as filled in by the user, so as not to disrupt the current flow of the generators (i.e. collect/resolve the prompted options & only then run generator logic – e.g. calling @nrwl/devkit
’s generateFiles()
).
Issue Analytics
- State:
- Created 2 years ago
- Comments:15
Good call. I almost did that, but wanted visibility on this issue as well - to your point about it impacting this work. I ended up creating a discussion instead of an issue. You can track it here: https://github.com/nrwl/nx/discussions/13902#discussion-4677294
@brandonbechtel that’s a great question to ask, and I think it really deserves its own issue, since this one is regarding expansion of the supported schema-driven prompt types. After you create it, I would definitely comment back on this issue with a reference to your new one, because a decision to switch libraries would certainly impact any development work on this one.