Last active
August 29, 2015 14:22
-
-
Save rimassa/dfa57285d3898c9fe0c6 to your computer and use it in GitHub Desktop.
gulp plumber async working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') { | |
self.emit('error', new Error('simulated error')) | |
cb(); | |
} 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:10:24] Plumber found unhandled error: | |
Error: simulated error | |
info: Running plugin on file /file6.tmp | |
info: Received file /file6.tmp | |
info: Running plugin on file /file7.tmp | |
info: Received file /file7.tmp | |
info: Running plugin on file /file8.tmp | |
info: Received file /file8.tmp | |
info: Running plugin on file /file9.tmp | |
info: Received file /file9.tmp | |
info: Running plugin on file /file10.tmp | |
info: Received file /file10.tmp | |
info: end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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