Last active
August 29, 2015 13:56
-
-
Save nfroidure/9292381 to your computer and use it in GitHub Desktop.
gulp-retain
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
function gRetain(options) { | |
var stream = new Stream.Writable({objectMode:true}) | |
, fileMap = {} | |
, timeout | |
, size = 0 | |
; | |
options.limit = options.limit || 0; | |
options.delay = options.delay || 300; | |
stream._write = function(file, unused, cb) { | |
size += file.contents.length | |
- (fileMap[file.path] ? fileMap[file.path].contents.length : 0); | |
if(options.limit && size > options.limit) { | |
stream.emit('error', new Error('Maximum memory size reached!')); | |
cb(); | |
} | |
fileMap[file.path] = file; | |
if(timeout) { | |
clearTimeout(timeout); | |
} | |
timeout = setTimeout((function(files) { | |
var newStream = new Stream.PassThrough({objectMode: true}); | |
function sendFile() { | |
if(files.length) { | |
newStream.write(files.shift.clone(), sendFile); | |
} else { | |
newStream.end(); | |
} | |
} | |
stream.emit('incoming', newStream); | |
sendFile(); | |
})(Object.keys(fileMap).map(function(key) { | |
return fileMap[key]; | |
})), options.delay); | |
cb(); | |
} | |
return stream; | |
} | |
// usage | |
gulp.src('coffee/**/*') | |
.pipe(watch()) | |
.pipe(coffee()) | |
.pipe(gRetain()) | |
.on('incoming', function(stream) { | |
stream.pipe(concat()) | |
.pipe(gulp.dest('build/')) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment