0

I have a gulp task. If a file is missing (foobar.json) I want it to throw a custom error message.

Example:

var gulp = require('gulp');
var plumber = require('gulp-plumber');
var notify = require("gulp-notify");
var foobar = './foobar.json';

gulp.task('my-task'), function() {
  if (typeof foobar === 'undefined') {
    console.log('foobar.json file is required to run my-task. Blau Blau Blau.');
  }
  else {
    something.init();
  }
});

Of course, console.log does not work. How do I do this?

Please don't mark as duplicate. There is no question that specifically deals with this simple issue of checking for a file, and throwing a simple message. All of the questions that I could find required a lot more complexity than this simple issue.

heart.cooks.mind
  • 1,065
  • 1
  • 9
  • 22
  • The first thing I got when I googled "Gulp custom error message" was this: https://stackoverflow.com/questions/29734769/nicely-throwing-an-error-in-gulp-task – DevNebulae Aug 01 '17 at 15:09
  • Yes, I saw that one. But it requires gulpUtil. The article says "This post is old, and probably obsolete". – heart.cooks.mind Aug 01 '17 at 16:43
  • I would recommend you to do so, since the gulp-util package actually takes care of a lot of error message utils, like stack traces etc. Furthermore, I would say @Hitmands solution is correct and you need the fs module to read from disk. – DevNebulae Aug 01 '17 at 17:09

2 Answers2

1

Use Gutil

var fs = require('fs');
var gulp = require('gulp');
var foobar = './foobar.json';

const gutil = require("gulp-util");

gulp.task('my-task'), function() {
  if (!fs.existsSync(foobar)) {
    throw new gutil.PluginError({
      plugin: 'my-task',
      message: `cannot read "${foobar}"`
    });
  }
});
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • I didn't downvote this, but I don't think I will use this because I want to avoid adding two more modules to just to add an error message. Is it possible to use Plummer to do this? – heart.cooks.mind Aug 01 '17 at 17:07
  • `fs` is a built-in modules, it comes once you install node. `Gutil` (aka gulp utils) is designed to pair with gulp, enhances your flow (colored logs, vynil files, proper gulp error management, etc...). Don't be scared, they are useful things. – Hitmands Aug 02 '17 at 08:44
-1

This worked without the need for extra modules:

var gulp = require('gulp');
var foobar = './foobar.json';

gulp.task('my-task'), function() {
  if (foobar.length) {
    console.log('A foobar.json file is required to run my-task. Blau Blau Blau.');
  }
  else {
    something.init();
  }
});
heart.cooks.mind
  • 1,065
  • 1
  • 9
  • 22
  • I don't know if it is just an example, but, the `if` statement in your snippet is a constant expression and will always be `true`. – Hitmands Aug 02 '17 at 08:46
  • The IF is checking if not checking if the variable exists. It is checking if it has length. If the file does not exist it does return false. If the file is there it has length and does indeed return true. – heart.cooks.mind Aug 07 '17 at 14:55
  • please have a better look on your script. Also read what is a [`Constant Expression`](https://stackoverflow.com/questions/3755524/example-of-something-which-is-and-is-not-a-constant-expression-in-c) – Hitmands Aug 07 '17 at 14:58
  • I don't know why you down-voted my working answer. I can't make you like my answer. btw - I know what a constant expression is. Also, I have tested my answer or I would not have posted it. I know this works in my use-case, I am using it. I don't know what else to tell you. People will try it. If it works for them good. If not they can leave it. – heart.cooks.mind Aug 07 '17 at 15:19
  • I've downvoted this because checking the `length` property of `var foobar = './foobar.json';` will always be `13`, so, this is a `Constant Expression` (always `true`). – Hitmands Aug 07 '17 at 15:31