Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Last active August 29, 2015 13:56
Show Gist options
  • Save nfroidure/9292381 to your computer and use it in GitHub Desktop.
Save nfroidure/9292381 to your computer and use it in GitHub Desktop.
gulp-retain
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