jb510
jb510

Reputation: 368

How to specify zip destination folder name using gulp-zip

I'm trying to use gulp-zip to build a zip file that includes a version number, but when extracted extracts to a folder without a version number.

If I normally zip a folder, then rename it, then unzip it, the file is extracted to the original folder name, I assume because the destination parameter is set. If I do the same using gulp-zip (zip it first and then rename it with a version number) the extraction occurs to the folder with the version number.

var packageJSON = require('./package.json');
var fileName = packageJSON.name;
var fileVersion = packageJSON.version;

var zip = require('gulp-zip');
var rename = require('gulp-rename');

var buildTemp = './temp-build/';
var buildInclude = [ '**/*', '!package*.json', '!node_modules/' ];

gulp.task('buildZip', function () {
    return gulp.src(buildTemp + '/**/')
        .pipe(zip(fileName + '.zip'))
        .pipe(gulp.dest('./'))
        .pipe(notify({message: 'Zip task complete', onLast: true}));
});

gulp.task('renameZip', function () {
    return gulp.src(fileName + '.zip')
        .pipe(rename(fileName + '-' + fileVersion + '.zip'))
        .pipe(gulp.dest('.././'))
        .pipe(notify({message: 'Zip renamed and moved up', onLast: true}));
});


gulp.task('build', function (cb) {
    runSequence('buildZip', 'renameZip', cb);
});

That all works. I get a zip file in the gulproot named filename.zip, and I get a file named filename-version.zip in the directory above. The problem is when I unzip filename-version.zip it extracts to /filename-version/.

This was also asked here: https://github.com/sindresorhus/gulp-zip/issues/88 and referred to Stack Exchange and referenced here https://github.com/sindresorhus/gulp-zip/pull/35 with the recommended solution being to use rename but that doesn't seem to actually work.

I believe I need to pass an option to gulp-zip to specify the zip destination, but I'm unclear how to do that, or if it's even possible.

Upvotes: 1

Views: 1394

Answers (2)

farinspace
farinspace

Reputation: 8801

A more direct way of doing this:

gulp.src(distDir + '/**', {base: distDir})
    .pipe(rename(function(file) {
        file.dirname = projectName + '/' + file.dirname;
    }))
    .pipe(zip(projectName + '-' + version +'.zip'))
    .pipe(gulp.dest('.'));

Upvotes: 2

jb510
jb510

Reputation: 368

After a lot of tweaking I finally sorted out the magic combo.

First I needed to create a temp directory with the destination name I wanted. Second, I needed to incldue base in the src glob.

var packageJSON = require('./package.json');
var fileName = packageJSON.name;
var fileVersion = packageJSON.version;

var zip = require('gulp-zip');
var rename = require('gulp-rename');

var buildTemp = fileName;
var buildInclude = [ '**/*', '!package*.json', '!node_modules/' ];

gulp.task('buildZip', function () {
    return gulp.src(buildTemp + '/**/', {base: './'})
        .pipe(zip(fileName + '.zip'))
        .pipe(gulp.dest('./'))
        .pipe(notify({message: 'Zip task complete', onLast: true}));
});

gulp.task('renameZip', function () {
    return gulp.src(fileName + '.zip')
        .pipe(rename(fileName + '-' + fileVersion + '.zip'))
        .pipe(gulp.dest('.././'))
        .pipe(notify({message: 'Zip renamed and moved up', onLast: true}));
});


gulp.task('build', function (cb) {
    runSequence('buildZip', 'renameZip', cb);
}); 

Upvotes: 2

Related Questions