Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Grunt for Grunt.task.run上的循环,使用变量_Javascript_Node.js_For Loop_Gruntjs_Command Line Interface - Fatal编程技术网

Javascript Grunt for Grunt.task.run上的循环,使用变量

Javascript Grunt for Grunt.task.run上的循环,使用变量,javascript,node.js,for-loop,gruntjs,command-line-interface,Javascript,Node.js,For Loop,Gruntjs,Command Line Interface,我目前正在尝试在grunt中创建一个任务,并发送参数多次运行另一个任务 以下是我已经开始运行的单构建任务: var country = grunt.option('country') || 'X0'; var product = grunt.option('product') || 'Y0'; var subdomain = grunt.option('subdomain') || 'Z0'; grunt.registerTask('build_home', [ 'sprite:pub

我目前正在尝试在grunt中创建一个任务,并发送参数多次运行另一个任务

以下是我已经开始运行的单构建任务:

var country = grunt.option('country') || 'X0';
var product = grunt.option('product') || 'Y0';
var subdomain = grunt.option('subdomain') || 'Z0';

grunt.registerTask('build_home', [
    'sprite:publicProduct',
    'sprite:home',
    'uglify:home',
    'sass:home',
    'csscomb:home',
    'cssmin:home'
]);
因此,在我的日常工作中,我会运行以下命令:

grunt build_home --country=X0 --product=Y7 --subdomain=Z3
我现在想要的是一个可以运行我所有可能的预定义选项的任务:

grunt build_home_all
应该是这样的:

grunt.registerTask('build_home_all', function() {

    var products = ['Y0','Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8','Y9'];
    var subdomains = ['Z0','Z1','Z2'];

    for (i = 0; i < products.length; i++) { 
        for (j = 0; j < subdomains.length; j++) {
            grunt.task.run('build_home --product='+products[i]+' --subdomain='+subdomains[j]);
        };
    };

    grunt.log.ok(['Finished.']);

});
grunt.registerTask('build\u home\u all',function(){
var乘积=['Y0'、'Y1'、'Y2'、'Y3'、'Y4'、'Y5'、'Y6'、'Y7'、'Y8'、'Y9'];
var子域=['Z0','Z1','Z2'];
对于(i=0;i

我已经用grunt.util.spawn实现了这一点,但它有点异步运行,并强制我的cpu同时运行各种任务。

当使用调用任务的
grunt.task.run
方法时,您将无法以通常使用CLI的方式传递参数,但您仍然可以使用冒号分隔符号传递参数:
build\u home--country=X0--product=Y7--subdomain=Z3
=>
build\u home:X0:Y7:Z3

将您的
build\u home\u all
任务更新为以下内容:

function buildHomeAll() = {
    var products = ['Y0','Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8','Y9'],
        subdomains = ['Z0','Z1','Z2'],
        tasks = [],
        country = grunt.option('country') || 'X0';


    for (i = 0; i < products.length; i++) {
        for (j = 0; j < subdomains.length; j++) {
            tasks.push(['build_home',country, products[i],'subdomains[j]'].join(':'));
        };
    };
    grunt.task.run(tasks);

    grunt.log.ok(['Finished.']);
}
最后,公开
grunfile.js中的函数:

grunt.registerTask('build_home', buildHome);
grunt.registerTask('build_home_all', buildHomeAll);

使用调用任务的
grunt.task.run
方法时,您将无法以通常使用CLI的方式传递参数,但仍然可以使用冒号分隔的符号传递参数:
build\u home--country=X0--product=Y7--subdomain=Z3
=>
build\u home:X0:Y7:Z3

将您的
build\u home\u all
任务更新为以下内容:

function buildHomeAll() = {
    var products = ['Y0','Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8','Y9'],
        subdomains = ['Z0','Z1','Z2'],
        tasks = [],
        country = grunt.option('country') || 'X0';


    for (i = 0; i < products.length; i++) {
        for (j = 0; j < subdomains.length; j++) {
            tasks.push(['build_home',country, products[i],'subdomains[j]'].join(':'));
        };
    };
    grunt.task.run(tasks);

    grunt.log.ok(['Finished.']);
}
最后,公开
grunfile.js中的函数:

grunt.registerTask('build_home', buildHome);
grunt.registerTask('build_home_all', buildHomeAll);

与其在for循环中运行任务,不如将任务推送到在for循环中生成的数组中,然后运行该数组。@这种协调实际上非常有用,并且是解决方案的一部分,但我仍然不能运行类似于
grunt.task.run('build_home--country=X0--product=Y7--subdomain=Z3')的东西
显示时警告:未找到任务“build_home--country=X0--product=Y7--subdomain=Z3”。使用--force继续。
它只是不标识参数。您需要查看创建自定义任务作为每个生成过程的初始值设定项,并使用
this.args
传递选项。您排队的任务将看起来像
build\u home:X0:Y7:Z3
。参考:好的,我写了一个答案,可能需要一些调整,因为我做了一些假设。希望这有助于代替在for循环中运行任务,您应该将任务推送到在for循环中生成的数组中,然后运行该数组。@该协调实际上非常有用,并且是解决方案的一部分,但我仍然不能运行类似于
grunt.task.run('build\u home--country=X0--product=Y7--subdomain=Z3')的东西
显示时警告:未找到任务“build_home--country=X0--product=Y7--subdomain=Z3”。使用--force继续。
它只是不标识参数。您需要查看创建自定义任务作为每个生成过程的初始值设定项,并使用
this.args
传递选项。您排队的任务将看起来像
build\u home:X0:Y7:Z3
。参考:好的,我写了一个答案,可能需要一些调整,因为我做了一些假设。希望我已经成功地创建了任务,并且几乎完全按照您所做的那样完成了任务。区别在于我将选项设置为grunt.initConfig,并使用grunt.config.set对其进行了更改。会有明显的性能差异吗?非常感谢。顺便说一句,我已经成功地创建了任务,并且几乎和你做的一样。区别在于我将选项设置为grunt.initConfig,并使用grunt.config.set对其进行了更改。会有明显的性能差异吗?顺便说一句,非常感谢。