Skip to content

Instantly share code, notes, and snippets.

@rimassa
Last active August 29, 2015 14:22
Show Gist options
  • Save rimassa/62ce1f9d2573096d549a to your computer and use it in GitHub Desktop.
Save rimassa/62ce1f9d2573096d549a to your computer and use it in GitHub Desktop.
gulp plumber async not working
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var logger = require('winston');
var File = require('vinyl');
var Readable = require('stream').Readable;
var Writable = require('stream').Writable;
var Transform = require('stream').Transform;
var util = require('util')
/**
* Gulp sample plugin
* @return {[type]} [description]
*/
var plugin = function () {
/**
* @class FileTransform
* @param {[type]} files [description]
*/
function FileTransform () {
Transform.call(this, {objectMode: true})
}
util.inherits(FileTransform, Transform);
FileTransform.prototype._transform = function (file, enc, cb) {
logger.info('Running plugin on file', file.path)
var self = this;
setTimeout(function () {
if (file.path == '/file5.tmp') {
cb(new Error('simulated error'))
} else {
cb(null, file);
}
}, 100)
}
return new FileTransform();
}
/**
* @class FileSource
* @param {[type]} files [description]
*/
function FileSource (files) {
Readable.call(this, {objectMode: true})
this._files = files;
}
util.inherits(FileSource, Readable);
FileSource.prototype._read = function () {
this.push(this._files.shift());
if (this._files.length == 0)
this.push(null)
}
/**
* @class FileDest
*/
function FileDest () {
Writable.call(this, {objectMode: true})
this._files = [];
}
util.inherits(FileDest, Writable);
FileDest.prototype._write = function (file, enc, cb) {
logger.info('Received file', file.path)
this._files.push(file);
cb(null)
}
gulp.task('test', function(cb) {
var files = [
new File({path: '/file1.tmp'}),
new File({path: '/file2.tmp'}),
new File({path: '/file3.tmp'}),
new File({path: '/file4.tmp'}),
new File({path: '/file5.tmp'}),
new File({path: '/file6.tmp'}),
new File({path: '/file7.tmp'}),
new File({path: '/file8.tmp'}),
new File({path: '/file9.tmp'}),
new File({path: '/file10.tmp'}),
]
var input = new FileSource(files);
var output = new FileDest();
var pipeline = input
.pipe(plumber({}))
.pipe(plugin())
.pipe(output)
.on('finish', function () {
logger.info('end');
})
return pipeline;
});
gulp.start('test');
info: Running plugin on file /file1.tmp
info: Received file /file1.tmp
info: Running plugin on file /file2.tmp
info: Received file /file2.tmp
info: Running plugin on file /file3.tmp
info: Received file /file3.tmp
info: Running plugin on file /file4.tmp
info: Received file /file4.tmp
info: Running plugin on file /file5.tmp
[16:08:02] Plumber found unhandled error:
Error: simulated error
{
"dependencies": {
"gulp": "^3.8.11",
"gulp-plumber": "^1.0.1",
"vinyl": "^0.4.6",
"winston": "^1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment