如何防止 gulp-notify 在 Windows 中破坏 gulp-watch?

2022-01-12 00:00:00 node.js javascript gulp build-system

我正在使用 gulp-notify 插件.这是我如何在 gulpfile.js 中实现它的一个例子(你可以看到我也在使用 gutil 和 livereload.我不知道它们是否发挥了任何作用,但如果它们发挥作用,请告诉我.)

I am using the gulp-notify plugin. This is an example of how I'm implementing it in a gulpfile.js ( You can see I'm using gutil and livereload as well. I don't know if they play any factors, but let me know if they do.)

gulp.task('js', function() {
  return gulp.src('./dev/scripts/*.coffee')
    .pipe(coffee({bare: true}).on('error', gutil.log))
    .pipe( gulp.dest('./build/js'))
    .pipe(notify('Coffeescript compile successful'))
    .pipe(livereload(server));
});

此插件适用于 OS X 和 Linux.在没有通知功能的 Windows 上,它会返回错误并破坏 gulp-watch 插件.这是我如何设置 gulp-watch 的示例:

This plugin works on OS X and Linux. On Windows, which doesn't have a notification feature, it returns an error and breaks the gulp-watch plugin. This is an example of how I have gulp-watch setup:

gulp.task('watch', function () {
  server.listen(35729, function (err) {
    if (err) {
      return console.log(err);
    }
    gulp.watch('./dev/scripts/*.coffee',['js']);
  });
});

因此,我在文档中阅读了 gulp-pipe 插件可以帮助我在 Windows 上不破坏 gulp-watch,但我找不到如何在这样的设置中使用它的示例.

So, I read in the documentation the gulp-pipe plugin can help me not break gulp-watch when on Windows, but I can't find an example of how I could use it in a setup like this.

推荐答案

你可以使用 gulp-if插件结合os节点模块来确定您是否在 Windows 上,然后排除 gulp-notify,如下所示:

You can use the gulp-if plugin in combination with the os node module to determine if you are on Windows, then exclude gulp-notify, like so:

var _if = require('gulp-if');

//...

// From http://stackoverflow.com/questions/8683895/variable-to-detect-operating-system-in-node-scripts
var isWindows = /^win/.test(require('os').platform());

//...

     // use like so:
    .pipe(_if(!isWindows, notify('Coffeescript compile successful')))

相关文章