1

I'm creating a new grunt-init template for my project and was wondering if there is a way to do conditional prompts based on the answers given to previous prompts.

My main goal is to be able to use the Github API to create an issue when I create a new module in my project. After asking for the module information, I would ask if a Github issue should be created. If yes, then ask for information like assignee, milestone, labels. If no, I don't care about any of those features.

Right now, I can just default them to blanks, but I'd like to just skip those prompts entirely.

dimmech
  • 827
  • 9
  • 19
doowb
  • 3,372
  • 1
  • 17
  • 25

1 Answers1

4

The init property exposes a init.prompts() object which you could modify based on the answers.

Something like this:

exports.template = function(grunt, init, done) {
    init.process([
        init.prompt('create_github_issue', function(value, props, done) {
            init.prompts['milestone'] = init.prompt('milestone');
            done();
        })
    ], function(err, props) {
        // handle all the props
        done();
    });
};

See the gruntplugin template for how to implement an init task.

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
  • I thought this looked promising but for some reason it never asks me the prompt inside the function. It seems to just ask the first question and fly through the function without doing anything with it. Also, that example does not have any inner prompts. – Metropolis Sep 26 '16 at 20:59