0

I am using gulp.js. But I want to add a condition that if request.debug = true; in any of my js file then gulp task should fail. I have found gulp-if and gulp-fail that I can use for this purpose but I don't know that how can I check the specific condition? Thanks

Ask
  • 3,076
  • 6
  • 30
  • 63

1 Answers1

1

You could use the gulp-contains module. If the condition provided to the contains function is true, an error is thrown.

gulp.task('default', function () {
    gulp.src('src/**/*.js')
        .pipe(contains('request.debug = true;'));
});
Stephen S
  • 3,936
  • 2
  • 23
  • 33
  • Thanks for your answer, I'll definitely give it a try. One more thing, how can we then fail gulp task if this condition is true? – Ask Jul 11 '18 at 07:43
  • The module will throw an error as a default, so it will fail automatically. You could add a callback in case you need some additional error handling logic. Refer [here](https://www.npmjs.com/package/gulp-contains#usage) – Stephen S Jul 11 '18 at 09:51
  • One more thing, is there anyway we can check that request.debug = true; is not commented? – Ask Jul 12 '18 at 05:50
  • It is possible to do so with a regular expression as there many ways to comment the code block. You can add your regex in the contains function argument. – Stephen S Jul 12 '18 at 08:08
  • Ok thanks and do you have any idea how can we trace the location where string found using gulp-contains? – Ask Jul 12 '18 at 09:33
  • The plugin doesn't return the specific position afaik. You can use the onFound callback which returns the file object where the match occurred. You could maybe read the file & find the specific index with that. It would be better if you could ask a new question on SO in case of more specifics. – Stephen S Jul 12 '18 at 09:46
  • I have asked a new question regarding this https://stackoverflow.com/questions/51303941/how-can-i-extract-filename-from-gulp-contains-callback Can you answer that question? – Ask Jul 12 '18 at 11:04