idleberg idleberg - 29 days ago 27
Javascript Question

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.

Answer

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({...});
        }

    }));
});
Comments