-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var gulputil = require('gulp-util');
var through2 = require('through2');
var Readable = require('readable-stream/readable');
var es = require('event-stream');
var PLUGIN_NAME = 'gulp-piece';
var createReadableStreams = function (cachedFiles) {
var allStreams = [];
cachedFiles.forEach(function (file) {
var stream = new Readable();
stream.push('\s');
stream.push(null);
stream = stream.pipe(through2.obj(function (chunk, enc, next) {
next(null, file);
}));
allStreams.push(stream);
});
return es.merge.apply(es, allStreams);
};
var piece = function (pipes) {
var cachedFiles = [];
if (!Array.isArray(pipes)) {
pipes = Array.prototype.slice.call(arguments, 0);
}
if (pipes.length === 0) {
return through2.obj();
}
return through2.obj(function (file, encoding, done) {
if (file.isNull()) {
return done();
}
if (file.isBuffer()) {
cachedFiles.push(file);
return done();
}
if (file.isStream()) {
throw new gulputil.PluginError(PLUGIN_NAME, 'Streaming not supported');
}
done();
}, function (done) {
var self = this;
pipes.unshift(createReadableStreams(cachedFiles));
pipes.push(through2.obj(function (file, encoding, next) {
self.push(file);
next();
}, function (callback){
callback();
done();
cachedFiles = null;
}));
pipes.reduce(function (oStream, transformStream, index) {
return oStream.pipe(transformStream);
});
});
};
module.exports = piece;