复制任务的Grunt循环,但仅复制最后一个文件 [英] Grunt loop for copy task, but copying only last file

查看:64
本文介绍了复制任务的Grunt循环,但仅复制最后一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can you help me with this problem?

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

I'm trying to create a function in Gruntfile.js to copy template files to my project using a loop and .json file to "automate" a copy job, apparently the function looks great because Grunt runs the copy job according to the number of records in the .json file, using a grunt.log.write in a loop Grunt shows the names of the .json files but in reality it copies only the last registered file.

首先是我的 .json 文件:

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

第二个复制任务与循环变量:

Second my copy task with the loop variables:

copy: {
    core: {
        files: [{
            expand: true,
            cwd: "core/<%=grunt.option('coreName')%>/<%=grunt.option('coreNumber')%>/",
            src: "**",
            dest: "../sass/2_deploy/core/"
        }]
    }
}

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

The intention was to get the file inside the directory of the version "header /1/", "footer/2/" and transfer into the deploy directory according to the code above.

第三,这是读取 .json 文件并声明变量并在循环内执行任务的函数:

Third, here is the function that reads the .json file and declares the variables and executes the task inside the loop:

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返回以下信息:

At this point, when executing the task, Grunt returns this information:

$ grunt moveCore
Running "moveCore" task
footerheader
Running "copy:core" (copy) task
Copied 1 file

Running "copy:core" (copy) task
Copied 1 file

从任务的描述来看,grunt似乎已经为每个记录执行了两次任务,但是实际上它仅将最后一个标头"文件移动到目录中,我的问题是这种操作是否真的可能还是我应该放弃Gruntfile中的循环.

From the description of the task it seems that grunt has executed task one for each record twice, but in practice it only moved the last "header" file to the directory, my question would be if this type of action is really possible or if I should abandon the loop within the Gruntfile.

非常感谢您的帮助!问候!

Thanks a lot for the help! Regards!

推荐答案

我认为您应该构建一个要复制的文件数组,然后将该数组传递给任务,因此 grunt 将运行所有文件夹的任务只能复制一次,而不是多次运行该任务.

I think you should build an array of files to copy, and then pass the array to the task, so grunt will run the task for all the folders to copy only once, instead of running the task multiple times.

首先,您定义将创建要复制的文件数组的函数:

First you define the function that will create the array of files to copy:

function getFiles() {
    var models = require('./variables.json');
    var cores = models.config.modules;
    var files = [];
    for (var core in cores) {
        files.push({
            expand: true,
            cwd: "core/" + cores[core].name + "/" + cores[core].number + "/",
            src: "**",
            dest: "../sass/2_deploy/core/"
        });
        grunt.log.write("core/" + cores[core].name + "/" + cores[core].number + "/\r\n");
    }
    return files;
}

然后,定义 copy 任务以使用文件数组:

Then, define the copy task to use the array of files:

grunt.initConfig({
    copy: {
        core: {
            files: getFiles()
        }
    }
});

然后只需定义任务:

grunt.registerTask('default', ['copy:core']);

生成的 Gruntfile.js 如下所示:

module.exports = function(grunt) {

    function getFiles() {
        var models = require('./variables.json');
        var cores = models.config.modules;
        var files = [];
        for (var core in cores) {
            files.push({
                expand: true,
                cwd: "core/" + cores[core].name + "/" + cores[core].number + "/",
                src: "**",
                dest: "../sass/2_deploy/core/"
            });
            grunt.log.write("core/" + cores[core].name + "/" + cores[core].number + "/\r\n");
        }
        return files;
    }

    grunt.initConfig({
        copy: {
            core: {
                files: getFiles()
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-copy');

    grunt.registerTask('default', ['copy:core']);

};

希望有帮助!

这篇关于复制任务的Grunt循环,但仅复制最后一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆