动态生成grunt目标

时间:2015-11-15 23:46:29

标签: javascript gruntjs

我有以下grunt文件。我正在尝试动态生成dev_ *目标以使其更易于维护。我发现以下https://quickleft.com/blog/grunt-js-tips-tricks/显示出动态生成任务,但我似乎无法得到它

'use strict';



module.exports = function (grunt) {

    // load all grunt tasks.
    require('load-grunt-tasks')(grunt);

    grunt.file.expand('node_modules/testcore/node_modules/grunt-*/tasks').forEach(grunt.loadTasks);


    grunt.registerMultiTask('test', 'Runs various test suites. Try :unit or :e2e', function () {
        // runs all tasks defined in the specific target's list, e.g., "test:unit" would run all
        // tasks in config.test.unit
        grunt.task.run(this.data);
    });

    var testcore_protractor_bin = 'node_modules/testcore/node_modules/protractor/bin/';
    var browser  =  {
        safari : 'safari',
        firefox : 'firefox'
    };


    grunt.config.merge({
        protractor:{
            options: {
                configFile: "node_modules/testcore/src/conf/protractor.conf.js",
                keepAlive: false,
                args: {
                    specs: [process.cwd() + '/test/e2e/stories/**/*.js'],
                    browser: 'chrome',
                    seleniumAddress: 'http://localhost:4444/wd/hub'
                }
            },
            dev_firefox: {
                options: {
                    args:{
                        specs: [process.cwd() + '/test/e2e/stories/**/*.js'],
                        browser: browser.firefox,
                        seleniumAddress: 'http://localhost:4444/wd/hub'
                    }
                }
            },
            //Will need to install the safari driver manually https://code.google.com/p/selenium/wiki/SafariDriver
            dev_safari: {
                options: {
                    args:{
                        specs: [process.cwd() + '/test/e2e/stories/safari/**/*.js'],
                        browser: browser.safari,
                        seleniumAddress: 'http://localhost:4444/wd/hub'
                    }
                }
            },
        },

        test: {
            e2e_firefox: ['env:test', 'exec:webdriverUpdate', 'protractor_webdriver:e2e', 'protractor:dev_firefox'],
            e2e_safari: ['env:test', 'exec:webdriverUpdate', 'protractor_webdriver:e2e', 'protractor:dev_safari'],,
        },
        env: {
            test: {
                NODE_ENV: 'test',
            }
        },
        protractor_webdriver: {
            e2e: {
                options: {
                    path: testcore_protractor_bin,
                    command: 'webdriver-manager start'
                }
            }
        },
        exec: {
            webdriverUpdate: {
                command: testcore_protractor_bin + 'webdriver-manager update'
            },

        }
    });

};

我使用此部分更新了代码

   var browser  =  {
        safari : 'safari',
        firefox : 'firefox'
    };

    for (var i = 0; i < browser.length; i++){
        var name = 'dev_' + browser[i];

        grunt.config.merge({
            protractor:{
                '<%= name %>' : {
                    options: {
                        args:{
                            specs: [process.cwd() + '/test/e2e/stories/**/*.js'],
                            browser: browser[i],
                            seleniumAddress: 'http://localhost:4444/wd/hub'
                        }
                    }
                }
            }
        });
    };

然而我得到的错误是

Running "protractor:dev_firefox" (protractor) task
Verifying property protractor.dev_firefox exists in config...ERROR
>> Unable to process task.
Warning: Required config property "protractor.dev_firefox" missing. Use --force to continue.

完整的grunt文件就是这样

'use strict';



module.exports = function (grunt) {

    // load all grunt tasks.
    require('load-grunt-tasks')(grunt);

    grunt.file.expand('node_modules/testcore/node_modules/grunt-*/tasks').forEach(grunt.loadTasks);


    grunt.registerMultiTask('test', 'Runs various test suites. Try :unit or :e2e', function () {
        // runs all tasks defined in the specific target's list, e.g., "test:unit" would run all
        // tasks in config.test.unit
        grunt.task.run(this.data);
    });

    var testcore_protractor_bin = 'node_modules/testcore/node_modules/protractor/bin/';
    var browser  =  {
        safari : 'safari',
        firefox : 'firefox'
    };

    for (var i = 0; i < browser.length; i++){
        var name = 'dev_' + browser[i];

        grunt.config.merge({
            protractor:{
                '<%= name %>' : {
                    options: {
                        args:{
                            specs: [process.cwd() + '/test/e2e/stories/**/*.js'],
                            browser: browser[i],
                            seleniumAddress: 'http://localhost:4444/wd/hub'
                        }
                    }
                }
            }
        });
    };
    grunt.config.merge({
        protractor:{
            options: {
                configFile: "node_modules/testcore/src/conf/protractor.conf.js",
                keepAlive: false,
                args: {
                    specs: [process.cwd() + '/test/e2e/stories/**/*.js'],
                    browser: 'chrome',
                    seleniumAddress: 'http://localhost:4444/wd/hub'
                }
            },

        },

        test: {
            e2e_firefox: ['env:test', 'exec:webdriverUpdate', 'protractor_webdriver:e2e', 'protractor:dev_firefox'],
            e2e_safari: ['env:test', 'exec:webdriverUpdate', 'protractor_webdriver:e2e', 'protractor:dev_safari'],
        },
        env: {
            test: {
                NODE_ENV: 'test',
            }
        },
        protractor_webdriver: {
            e2e: {
                options: {
                    path: testcore_protractor_bin,
                    command: 'webdriver-manager start'
                }
            }
        },
        exec: {
            webdriverUpdate: {
                command: testcore_protractor_bin + 'webdriver-manager update'
            }
        }
    });

};

如何动态生成grunt目标?

1 个答案:

答案 0 :(得分:0)

'<%= foo %>'不会按照您的想法行事。我不知道你会在哪里得到这个想法。

你可能想要这样的东西:

grunt.config.protractor[name] = {
    options: {
        args: {
            specs: [process.cwd() + '/test/e2e/stories/**/*.js'],
            browser: name,
            seleniumAddress: 'http://localhost:4444/wd/hub'
        }
    }
};