用于复制任务的Grunt循环,但仅复制最后一个文件

时间:2018-05-10 13:39:55

标签: loops gruntjs grunt-contrib-copy

你能帮我解决这个问题吗?

我正在尝试在2.0.15.1中创建一个函数,使用循环和Gruntfile.js文件将模板文件复制到我的项目中以“自动化”复制作业,显然该函数看起来很棒,因为Grunt运行复制作业根据.json文件中的记录数,在循环中使用.json Grunt显示grunt.log.write文件的名称,但实际上它只复制最后一个注册的文件

首先是我的.json文件:

.json

使用循环变量的第二个复制任务:

{
    "config": {
        "modules": [
            {"name": "footer", "number": 2},
            {"name": "header", "number": 1}
        ]
    }
}

目的是将文件放在版本“header / 1 /”,“footer / 2 /”的目录中,并根据上面的代码转移到deploy目录。

第三,这是读取copy: { core: { files: [{ expand: true, cwd: "core/<%=grunt.option('coreName')%>/<%=grunt.option('coreNumber')%>/", src: "**", dest: "../sass/2_deploy/core/" }] } } 文件并声明变量并在循环内执行任务的函数:

.json

此时,执行任务时,Grunt会返回以下信息:

function moveCoreFiles() {
    var models = require('./variables.json');
    var cores = models.config.modules;
    for (core in cores) {
        grunt.option('coreName', cores[core].name);
        grunt.option('coreNumber', cores[core].number);
        grunt.task.run('copy:core');
        grunt.log.write(grunt.option("coreName"));
    }
}
// ... enter code here
grunt.registerTask('moveCore', moveCoreFiles);

从任务的描述看来,grunt已经为每个记录执行了两次任务一次,但实际上它只将最后一个“头”文件移动到目录,我的问题是如果这种类型的动作真的可能或者我是否应该放弃Gruntfile中的循环。

非常感谢您的帮助! 此致!

0 个答案:

没有答案