Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip some or all prompts according to variables defined in template file? #426

Open
Toshinaki opened this issue Apr 19, 2023 · 0 comments
Open

Comments

@Toshinaki
Copy link

Toshinaki commented Apr 19, 2023

I have some template files under a folder doc/new.

One template (file.ejs.t) looks like:

---
to: doc/<%= name %>.md
---

And one (random.ejs.t) looks like:

---
to: doc/<%= h.customRandomGen() %>.md
---

The second one used a custom util function to generate a random file name.
But a filename name is required to be input for the first one.

I created a prompt.js under doc/new:

module.exports = [
  {
    type: "input",
    name: "name",
    message: "File name:",
    validate: function (value) {
      if (value.trim().length === 0) {
        return "File name can't be empty!";
      }
      return true;
    },
  }
];

And I can generate files by doc new:file or doc new:random.

For doc new:file, it's ok to ask for a name input, but for doc new:random, no input is needed, but the prompt shows and since the validation I defined, I can't input empty.

Is there any way to filter the prompts array, based on the variables appeared in template file?

So for template A, which uses a required name and optional desc, template B which uses only a required name, template C which uses optional desc, and template D which uses no input,
I can define a single array of prompts:

module.exports = [
  { ... name: "name", ...}, 
  { ... name: "desc", ...}, 
];

And the prompts are filtered:
template A: name,desc
template B: name
template C: desc
template D: nothing

Is there any way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant