question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for other enquirer prompts.

See original GitHub issue

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, like multiselect does
  • autocomplete prompts for when you want to suggest options, but not constrain input to them, like select does
  • invisible or password 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:open
  • Created 2 years ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
brandonbechtelcommented, Dec 19, 2022

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

1reaction
jskrzypekcommented, Dec 16, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the enquirer.prompt function in enquirer - Snyk
To help you get started, we've selected a few enquirer.prompt examples, ... printSubheader('Add New Environment'); prompt([ { type: 'input', name: 'name', ...
Read more >
enquirer-prompt-confirm - npm
enquirer -prompt-confirm NPM version NPM downloads. Adds confirm (yes/no) prompt support to Enquirer. confirm prompt example. Install.
Read more >
How To Create Interactive Command-line Prompts with ...
Learn how to add interactive command-line prompts to your Node.js scripts using the Inquirer.js npm package.
Read more >
Fill out both Question and Answer field In Enquirer CLI Prompt
Enquirer does support custom prompts, but I'm struggling to get my head around exactly how I might go about implementing something like this....
Read more >
Enquirer - Best of JS
Extensible - Easily create and use custom prompts by extending Enquirer's built-in prompts. Pluggable - Add advanced features to Enquirer using plugins.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found