twolfson/grunt-zip

Dynamic output paths

Closed this issue · 6 comments

I apologize for asking something that may be a Grunt 101 type thing. I have a source folder, /chapters, and under this are multiple demos folder. So for example:

/chapters/1/demos
/chapters/2/demos

etc

I want to zip them - and I assume **/demos/ would match. But can I set up dest such that I use /output as a root, but it would correctly use the same subdirectory it found when zipping? So using the example above, I'd have

/output/chapters/1/demos.zip
/output/chapters/2/demos.zip

We have 2 options to mitiage this issue:

  • cwd, specifies root directory for all paths to be resolved against
  • router, allows for custom path for each file

In your case, you probably want cwd

{
  'my-task': {
    cwd: 'output'
    src: 'output/**/demos/'
    dest: 'demos.zip'
  }
}

https://github.com/twolfson/grunt-zip#zip

From what I see though cwd and router both change how the file is zipped,
such that when you extract them, the paths are different. I'm trying to
make dest be dynamic, so if the folder zipped was chapters/1/demos, it
would save to output/1/demos.zip.

Is that doable? I apologize if I misunderstood.

On Tue, Jun 17, 2014 at 5:12 PM, Todd Wolfson notifications@github.com
wrote:

Closed #26 #26.


Reply to this email directly or view it on GitHub
#26 (comment).

Raymond Camden, Web Developer for Adobe

Email : raymondcamden@gmail.com
Blog : www.raymondcamden.com
Twitter: raymondcamden

Ah, I see. Nope, there is currently no way inside the task to generate multiple zip files. However, you can generate corresponding tasks via some of grunt's utilities.

// Collect directory paths (e.g. `output/1/demos/`, `output/2/demos/`)
var outputDirs = grunt.file.expand('output/**/demos/');

// Define our config (e.g. `output-1`, `output-2`)
var zipConfig = {};
outputDirs.forEach(function (outputDir, i) {
  zipConfig['output-' + i] = {
    src: outputDir, // e.g. `output/1/demos`
    dest: 'output-' + i + '.zip' // e.g. `output-1.zip`
  };
});
grunt.config.set('zip', zipConfig);

http://gruntjs.com/api/grunt.file#grunt.file.expand

Ok, so I'm definitely crossing the line now and asking you for Grunt support, so feel free to tell me to stuff it. ;) I'm close I think. This is what I have now:

grunt.registerTask('zipDemos', "Zips demos appropriately.", function() {
    console.log("Doing zipDemos");
    var sourceDirs = grunt.file.expand(['./chapters/**/demos','./chapters/**/demo']);
    sourceDirs.forEach(function(dir) {
        var outputZip = dir.replace("chapters", "output/chapters");
        //use a uniform demos.zip, not demo
        if(outputZip.charAt(outputZip.length-1) === "o") outputZip += "s";
        outputZip += ".zip";
        console.log("source: "+dir + " -> "+outputZip);
        grunt.config.set('zip.demos', {src:dir, dest:outputZip});
        grunt.task.run('zip');
    });
});

But this doesn't work because - if I read it right - the task.run stuff is queued. So when it runs, only the last config value is used. Any ideas?

Actually - I think I got it now. :) Thanks again!

Woot, glad you got it =)