forked from sindresorhus/gulp-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.15 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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var chalk = require('chalk');
var JSZip = require('jszip');
module.exports = function (filename, opts) {
if (!filename) {
throw new gutil.PluginError('gulp-zip', chalk.blue('filename') + ' required');
}
opts = opts || {};
var firstFile;
var zip = new JSZip();
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-zip', 'Streaming not supported'));
return cb();
}
if (!firstFile) {
firstFile = file;
}
// Because Windows...
var pathname = file.relative.replace(/\\/g, '/');
zip.file(pathname, file.contents, {
date: file.stat ? file.stat.mtime : new Date(),
createFolders: true
});
cb();
}, function (cb) {
if (!firstFile) {
return cb();
}
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, filename),
contents: zip.generate({
type: 'nodebuffer',
compression: 'DEFLATE',
comment: opts.comment
})
}));
cb();
});
};