Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs Gulp - confirm before starting task

I have a very heavy gulp task that I want to ask the user if he really wants to start the task.

I already saw the gulp-confirm and gulp-prompt but it's not helping me:
I don't want to pipe the 'confirm' I just don't want to start the heavy task before user confirmation.

I'm trying to achieve something like this:

gulp.task('confirm', [], function ()
  {
            //Ask the confirm question here and cancel all task if the user abort
            prompt.confirm({
                message: 'This task will create a new tag version\n. Are you sure you want to continue?',
                default: true
            });
  });

    gulp.task('Build Release QA', ['confirm'], function ()//use this task to make release to QA
    {
        //very heavy task, don't start it if user abort it from 'confirm' task

    });

Any ideas?

like image 637
cheziHoyzer Avatar asked Feb 13 '26 01:02

cheziHoyzer


1 Answers

There's no need for a Gulp plugin, actually the package those two wrap around -- inquirer -- does anything you need:

var inquirer = require('inquirer');

gulp.task('default', function(done) {
    inquirer.prompt([{
        type: 'confirm',
        message: 'Do you really want to start?',
        default: true,
        name: 'start'
    }], function(answers) {
        if(answers.start) {
            gulp.start('your-gulp-task');
        }
        done();
    });
});

It's important you pass the done callback and call it once you are in the callback function of inquirer, so your task doesn't end prematurely.

That's one of the better things in Gulp: You might not need those plugins ;-)

like image 166
ddprrt Avatar answered Feb 14 '26 23:02

ddprrt