Stop Gulp Task when conditions are met

I am trying to make sure that if the -theme flag is not specified, it stops the gulp task and wonders how best to do this in a dry way.

I would like each individual task to stop if the -theme option is not specified, and also stop the default task if it is not completed.

I tried a few things with no luck.

Thanks,

gulp.task('test', function() { if(typeof(args.theme) == 'undefined' || args.theme === true) { console.log(msg.noTheme); return; // end task } // run rest of task... }); gulp.task('test-2', function() { if(typeof(args.theme) == 'undefined' || args.theme === true) { console.log(msg.noTheme); return; // end task } // run rest of task... }); gulp.task('default', ['test-1', 'test-2']); 
+7
javascript gulp
source share
3 answers

You can just stop the script with

 process.exit() 
+10
source share

I think the easiest way would be to create a verifyArgs function that throws an error if the requirements are not met:

 function verifyArgs() { if(typeof(args.theme) == 'undefined' || args.theme === true) { throw Error(msg.noTheme); } } gulp.task('test', function() { verifyArgs(); // run rest of task... }); gulp.task('test-2', function() { verifyArgs(); // run rest of task... }); gulp.task('default', ['test-1', 'test-2']); 
+5
source share

Some async function might help here. Maybe like this:

 function processArgs(callback) { if(typeof(args.theme) == 'undefined' || args.theme === true) { return callback(new Error('Theme Not Defined')); } return callback(); } gulp.task('test', function(done) { processArgs(function(err) { if(err) { console.log(err); return done(err); } //else run my task }) }); 
+1
source share

All Articles