Skip to content

Instantly share code, notes, and snippets.

@jalateras
Created August 3, 2016 04:39
Show Gist options
  • Save jalateras/353d1e275d559239adf780f17e30ff99 to your computer and use it in GitHub Desktop.
Save jalateras/353d1e275d559239adf780f17e30ff99 to your computer and use it in GitHub Desktop.
split and merge
/**
* @author Jim Alateras
*/
var fs = require('fs');
var stream = require('stream')
var path = require('path');
var File = require('vinyl');
var vfs = require('vinyl-fs');
var buffer = require('vinyl-buffer');
var newLine = require('./vinyl-split');
var concat = require('gulp-concat');
var through = require('through2');
function split(files) {
var ptstream = through.obj(function(file, enc, done) {
var base = path.join(file.path, '..');
var createStreamer = function(index) {
var count = 0;
var streamer = through(function(chunk, enc, done) {
if (count++ % files.length === index) {
this.push(chunk);
}
done();
});
// streamer.on('error', this.emit.bind(this, 'error'));
return streamer;
};
if (file.isNull()) {
return done();
}
if (file.isBuffer()) {
var bufferStream = new stream.PassThrough();
bufferStream.end(file.contents);
file.contents = bufferStream;
}
for (var i = 0, len = files.length; i < len; i++) {
var entry = files[i];
this.push(new File({
base: base,
path: path.join(base, entry),
contents: file.contents.pipe(createStreamer(i))
}));
}
done();
});
return ptstream;
}
vfs.src(['/Users/jima/Downloads/abc-channel-mappings.csv'], {buffer: false})
.pipe(newLine())
.pipe(split(['one.csv', 'two.csv', 'three.csv']))
.pipe(buffer())
.pipe(concat('all.csv'))
.pipe(vfs.dest('/tmp/jima'));
@jalateras
Copy link
Author

grabs a file, splits it into new lines, splits it across files, and the merges it again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment