0

I have a directory lib/grunt-tasks parallel to my Gruntfile.js. It has a zip.js file in it (loaded via loadTasks). It requires the node-archiver and fs modules.

For some reason, although the .zip file is created, it contains nothing after archive.finalize(), and never fires the entry or error events.

I also don't get any errors requiring the 2 modules, so I don't believe there's any issue for the task to see the parent node_modules directory at the root of my project.

I can run a similar JS script from the command-line successfully: node lib/grunt-tasks/test.js ...with hard-coded paths. I wonder if there's something fundamental I'm missing about Grunt and/or Node. I'm just surprised I'm not seeing any errors though.

Any help is appreciated.

/**
 * Zips key messages according to their config data
 *
 * @see 'app-config.js'
 */

'use strict';

var archiver    = require('archiver');
var fs          = require('fs');

module.exports = function (grunt) {
    grunt.registerMultiTask('zip', function(){
        // Load in task config and options
        var config          = this.data;
        var data            = config.data;

        // Loop through all key messages and generate their .zip files
        data.key_messages.forEach(function(message){
            var srcDir      = config.cwd + "/" + message.path;
            var outputFile  = config.cwd + '/' + data.project.id + '_' + message.id + '.zip';

            // Create an actual file stream to output data to
            var output = fs.createWriteStream( outputFile );
            // Instantiate new archiver object
            var archive = archiver('zip');

            archive.on('error', function(err){
                console.log('error:', err);
            });

            archive.on('entry', function(a, b, c){
                console.log('entry:', a, b, c);
            });

            archive.pipe( output );

            grunt.log.writeln('Compressing to ' + outputFile.cyan);

            archive.bulk([
                {cwd: srcDir, src: ['**/*'], expand: true}
            ]);

            archive.finalize();
        });
    });
}
daleyjem
  • 2,335
  • 1
  • 23
  • 34

1 Answers1

0

This would go for any asynchronous process within Grunt (which I wasn't aware 'node-archiver' was)... but still worth noting:

Per Grunt's documentation, an async process should be set up using Grunt.task.async() to prevent the task from completing/closing prematurely.

daleyjem
  • 2,335
  • 1
  • 23
  • 34