Skip to content

Instantly share code, notes, and snippets.

@avaly
Created February 18, 2014 11:09
Show Gist options
  • Save avaly/9068932 to your computer and use it in GitHub Desktop.
Save avaly/9068932 to your computer and use it in GitHub Desktop.
gulp-zip-custom
var path = require('path'),
gutil = require('gulp-util'),
nodeZip = require('node-native-zip'),
through = require('through2');
var gulpZipCustom = function(filename) {
if (!filename) {
throw new gutil.PluginError('gulp-zip-custom', chalk.blue('filename') + ' required');
}
var firstFile;
var zip = new nodeZip();
return through.obj(function(file, enc, cb){
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-zip-custom', 'Streaming not supported'));
return cb();
}
if (!firstFile) {
firstFile = file;
}
var relativePath = file.path.replace(file.cwd + path.sep, '');
zip.add(relativePath, file.contents);
cb();
}, function (cb) {
if (!firstFile) {
return cb();
}
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.cwd,
path: path.join(firstFile.cwd, filename),
contents: zip.toBuffer()
}));
cb();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment