Search code examples
node.jspromptgruntjs

grunt-init template conditional prompts


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.


Solution

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