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']);
};
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 ofgrunt.config('directory')