-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.38 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
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var packer = require('zip-stream');
var Queue = require('node-queuer').default;
var chalk = gutil.colors;
var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-zip-stream';
function gulpZip(fileName, opts) {
if (!fileName) {
throw new PluginError(PLUGIN_NAME, chalk.blue('filename') + ' required');
}
var archive = new packer(opts);
var queue = new Queue();
var stream = through.obj(function (file, enc, cb) {
var self = this;
var pathname = file.relative.replace(/\\/g, '/');
if (!pathname) {
cb();
return;
}
if (file.stat && file.stat.isFile && file.stat.isFile()) {
queue.task(function (done) {
archive.entry(file.contents, {name: file.relative}, function (err) {
if (err) self.emit('error', new PluginError(PLUGIN_NAME, chalk.blue(file.path), err));
done();
});
});
}
cb();
}, function (callback) {
queue.task(function (done) {
archive.finish();
done();
});
var file = new gutil.File({
path: fileName,
contents: archive
});
this.push(file);
callback();
});
return stream;
}
module.exports = gulpZip;