将值从conf.js传递到grunt任务或grunt任务之间

时间:2016-03-12 04:41:48

标签: javascript node.js gruntjs protractor

如何在grunt任务之间传递信息?我想将grunt任务中的值传递给另一个grunt任务。

我的情况是,在完成量角器测试后,我想将值传递给新的grunt任务。为实现此目的,我继续在process.env中设置了值,并尝试在新的process.env任务中使用grunt。但这似乎不起作用

这是conf.js

afterLaunch: function(exitCode) {
    return new Promise(function(fulfill, reject) {
      if (typeof jasmine.getEnv().testReportFilePath !== 'undefined' && jasmine.getEnv().testReportFilePath !== null) {
        process.env.testReportFilePath = jasmine.getEnv().testReportFilePath;
        console.log('Trying: ' + process.env.testReportFilePath);
        fulfill('Success: Environment variable testReportFilePath is set in conf.js');
      } else {
        reject(new Error('Failed: Environment variable testReportFilePath not set in conf.js'));
      }
    });

这是我的Gruntfile

grunt.loadNpmTasks('grunt-protractor-runner');
grunt.loadNpmTasks('grunt-protractor-webdriver');
grunt.loadNpmTasks('grunt-execute');

grunt.config('protractor', {
    require: [ setTesting ],
    options: {
      configFile: 'conf.js', // common config file
      keepAlive: true, // If false, the grunt process stops when the test fails.
      noColor: false // If true, protractor will not use colors in its output.
    },
    run_specific_suite: {
      options: {
        args: {
          baseUrl: '<%= grunt.option("testUrl") %>',
          browser: '<%= grunt.option("testBrowser") %>',
          params: {
            environment: '<%= grunt.option("testEnv") %>'
          },
          suite: '<%= grunt.option("testSuite") %>'
        }
      }
    },
});

grunt.config('execute', {
  email_stakeholders: {
    options: {
      args: [
        process.env.testReportFilePath,
        'myemail@email.com'
      ]
    },
    src: ['toDelete.js']
  }
});

但gruntjs文件中的process.env.testReportFilePath似乎是undefined

1 个答案:

答案 0 :(得分:0)

这个答案早就应该了。我确实按照@mparnisari建议将变量写入文件。所以我在conf.js中执行了以下操作,将值写入yaml文件:

public void compress(char a, BinaryTree huffTree, StringBuilder result) { if (huffTree.root.data.equals(a)) { return; } //else if (!huffTree.getLeftSubtree().isLeaf()) else if (huffTree.getLeftSubtree() != null) { result.append("1"); compress(a, huffTree.getLeftSubtree(), result); } else if (huffTree.getRightSubtree() != null) { result.append("0"); compress(a, huffTree.getRightSubtree(), result); } }

var int_array = [1,2]; console.log(Math.min.apply(null,int_array)); 中,使用grunt api读取yaml文件:

fs.writeFileSync(path.join(process.cwd(),'_testReportFilePath.yml'),
                         'testReportFilePath: ' + jasmine.getEnv().testReportFilePath);

似乎做我需要的。只有quirk是gruntfile中必须始终存在名为// --- grunt execute task --- // grunt.config('execute', { email_stakeholders: { options: { args:[ grunt.file.readYAML(path.join(process.cwd(),'_testReportFilePath.yml')).testReportFilePath, // html report 'myemail@email.com'//enter the recipient emails here ] }, src: ['test/scripts/nightlyPostTasks.js'] } }); 的虚拟yaml文件,以防止任何grunt错误。