idleberg
idleberg

Reputation: 12882

Gulp prompt to decide which tasks to run?

I have just discovered the gulp-prompt task. While it makes it easy to prompt the user in different ways, the examples don't hint at how to make use of the user's input. For instance I would like to offer two setups (e.g. CDN-hosted or local assets) to the user and run my tasks conditionally. How can you do that in Gulp? This could save me from looking into a Yeoman generator.

Upvotes: 4

Views: 2851

Answers (2)

Denis Molodtsov
Denis Molodtsov

Reputation: 824

This is another implementation. It will ask you to choose the task with a keyboard

gulp.task('default', function() {
var taskNames = [];
for (var taskName in gulp.tasks) {
    if (gulp.tasks.hasOwnProperty(taskName)) {
        taskNames.push(taskName);
    }
}

return gulp.src('*').pipe(
    prompt.prompt({
        type: 'checkbox',
        name: 'tasks',
        message: 'Choose task name',
        choices: taskNames
    }, function(res){
        //value is in res.task (the name option gives the key)
        console.log(res);
        res.tasks.forEach(function(taskName){
            console.log(taskName);
            gulp.tasks[taskName].fn();
        }); 
    }));

});

Upvotes: 0

GNUrub
GNUrub

Reputation: 106

Exemple

...
...
var inject = require('gulp-inject');
var cdnizer = require('gulp-cdnizer');
gulp.task('mytask', function(){
  var target = gulp.src('./src/index.html');
  return gulp.src('*')
  .pipe(prompt.prompt({
        type: 'input',
        name: 'type',
        message: 'What you like to do? [cdn/assets]'
    }, function(res){
        var sources;
        if(res.type === 'cdn'){
          sources = ...
          target.pipe(inject(sources))
          .pipe(gulp.dest('./src'));
        }else{
          target.pipe(cdnizer({...});
        }

    }));
});

Upvotes: 5

Related Questions