How do I access a user input from grunt-prompt in other tasks

1.5k Views Asked by At

I need to write a grunt job for reading a user input using grunt-prompt and then create a directory with that name. I am trying to use the config to access variable in another grunt task which will be executed after the grunt-prompt. But all following methods gives an undefined.

I tried:

grunt.config('config.database')
grunt.config('database')
grunt.config('target.config.database')

Please advise. Here is my full script:

module.exports = function(grunt) {

grunt.initConfig({          

    prompt: {
        target: {
            options: {
                questions: [
                    {
                        config: 'directory',
                        type: 'input',
                        message: 'Enter direcotry Name',
                        validate: function(value){
                            if(value == '') {
                                return 'Should not be blank';
                            }
                            return true;
                        }
                    }                       
                ]
            }
        },
    },

    mkdir: {
        all: {
            options: {
                mode: 0777,
                create: [
                    grunt.config('config.directory')
                ]
            }
        }
    }       

});

grunt.loadNpmTasks('grunt-mkdir');
grunt.loadNpmTasks('grunt-prompt');

grunt.registerTask('default', 'prompt:target', 'mkdir']);
};
2

There are 2 best solutions below

0
On BEST ANSWER

The config key is directory.

But your issue is that your call to grunt.config() is executed when the Gruntfile is read, way before the tasks are run. At that point, prompt has not run, so the option is undefined.

You want something that will be only evaluated at runtime, so use a template string '<%= directory %>' instead of grunt.config('directory')

mkdir: {
    all: {
        options: {
            mode: 0777,
            create: ['<%= directory %>']
        }
    }
}
0
On

Just to add an alternative solution. One that may be useful under other circumstances. You can use grunt-prompt's then() to set the config once the prompt is complete.

grunt.initConfig({          

prompt: {
    target: {
        options: {
            questions: [
                {
                    config: 'directory',
                    type: 'input',
                    message: 'Enter direcotry Name',
                    validate: function(value){
                        if(value == '') {
                            return 'Should not be blank';
                        }
                        return true;
                    }
                }                       
            ],
            then: function(results){                
                grunt.config.set('mkdir.all.options.create', [results.directory]);                      
            }
        }
    },
},

mkdir: {
    all: {
        options: {
            mode: 0777,
            create: []
        }
    }
}       

});

Obviously for the example the accepted answer is the cleanest solution. But using the then function is nice if you have to do other tasks after the prompt as well as setting the config.