Grunt: flow control

135 Views Asked by At

From my own task, I what to use grunt-prompt to ask a boolean Q and then run or don't run a task based on the answer.

The problem is that grunt.task.run() does not actually run a task but rather adds it into a queue.

so if I do from my own task:

grunt.task.run('prompt:ask');
if (grunt.config('answer')) { 
   grunt.task.run('do:something');
}

When the if executes the prompt task did not actually run yet...

Is there a recommended way to do this?

maybe some syntax like:

grunt.task.run('prompt:ask').andThen(function(){
   if (grunt.config('answer')) { 
      grunt.task.run('do:something');
   }
})

Note To Self: Check what grunt returns from grunt.task.run

1

There are 1 best solutions below

4
On BEST ANSWER

You'll need to use the then option for the plugin to execute code once an answer is entered:

prompt: {
  ask: {
    options: {
      questions: [
        // your question here...
      ],
      then: function(results) {
        if (results[0].whatever) {  // obviously you'll need to update this...
          grunt.task.run('prompt:ask');
        }
      }
    }
  }
}