-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
52 lines (43 loc) · 1.17 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
const Stream = require('stream')
const through = require('through2').obj
const unzip = require('unzipper')
const Vinyl = require('vinyl')
module.exports = (options = {}) => {
function transform (file, enc, callback) {
if (file.isNull()) {
this.push(file)
return callback()
}
let stream = new Stream.PassThrough()
if (file.isBuffer() && !file.pipe) {
stream.end(file.contents)
} else {
stream = file
}
const opts = {}
opts.filter = options.filter || (() => true)
opts.keepEmpty = options.keepEmpty || false
stream.pipe(unzip.Parse())
.on('entry', entry => {
const chunks = []
if (!opts.filter(entry)) {
entry.autodrain()
return
}
entry.pipe(through((chunk, enc, cb) => {
chunks.push(chunk)
cb()
}, cb => {
if (entry.type === 'File' && (chunks.length > 0 || opts.keepEmpty)) {
this.push(new Vinyl({
cwd: './',
path: entry.path,
contents: Buffer.concat(chunks)
}))
}
cb()
}))
}).on('close', callback)
}
return through(transform)
}