twolfson / grunt-zip

Zip and unzip files via a grunt plugin
MIT License
87 stars 19 forks source link

How can I named zip file with folder name? #35

Closed galenyuan closed 9 years ago

galenyuan commented 9 years ago

I want's to named .zip file with the name of folder, but can't find any method in doc, can you please help me to solve it? thanks!

galenyuan commented 9 years ago

For example I have a project like this: -root ---folder1 -----jquery.js ---folder2 -----jquery.js It should create two zip files like this: -folder1.zip ---jquery.js -folder2.zip ---jquery.js

Any solution? Thanks so much.

twolfson commented 9 years ago

Each task will only generate a single zip file. You can dynamically configure grunt to generate new tasks for the folders:

// Load in dependencies
var fs = require('fs');
var path = require('path');

module.exports = function (grunt) {
  // Create a task for each folder
  var zipConfig = {};
  var folders = fs.readdirSync('root');
  folders.forEach(function createZipTask (folder) {
    // Save all folder contents to zip file (e.g. `root/folder1/*.*`)
    var folderGlob = path.join('root', folder, '*.*');
    var filename = folder + '.zip';
    zipConfig[filename] = folderGlob;
  });

  // Save config to grunt
  grunt.config.set('zip', zipConfig);

  // Load in our grunt tasks
  grunt.loadNpmTasks('grunt-zip');
};

Alternatively, you can create a task for each folder via hardcoding.