【问题标题】:Grunt + custom node-archiver task not workingGrunt + 自定义节点归档任务不工作
【发布时间】:2016-11-22 18:17:32
【问题描述】:

我有一个目录lib/grunt-tasks 与我的Gruntfile.js 平行。它有一个zip.js 文件(通过loadTasks 加载)。它需要node-archiverfs 模块。

由于某种原因,虽然创建了 .zip 文件,但它在 archive.finalize() 之后不包含任何内容,并且不会触发 entryerror 事件。

我也没有收到任何需要 2 个模块的错误,因此我认为在我的项目根目录中查看父级 node_modules 目录的任务不会有任何问题。

我可以从命令行成功运行类似的 JS 脚本:node lib/grunt-tasks/test.js ... 带有硬编码路径。我想知道 Grunt 和/或 Node 是否缺少一些基本的东西。我只是很惊讶我没有看到任何错误。

感谢任何帮助。

/**
 * 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();
        });
    });
}

【问题讨论】:

    标签: node.js gruntjs node-archiver


    【解决方案1】:

    这适用于 Grunt 中的任何异步进程(我不知道 'node-archiver' 是)...但仍然值得注意:

    根据 Grunt 的文档,应使用 Grunt.task.async() 设置异步进程,以防止任务过早完成/关闭。

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 2013-06-09
      • 2017-11-10
      • 2018-06-26
      • 2016-03-27
      • 1970-01-01
      • 2015-07-28
      • 2014-09-09
      • 2013-01-02
      相关资源
      最近更新 更多