将值 fron conf.js 传递给咕噜咕噜的任务或在咕噜咕噜的任务之间传递

passing value fron conf.js to a grunt task or between grunt tasks

本文关键字:任务 之间 fron conf js 将值      更新时间:2023-09-26

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

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

这是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']
  }
});

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

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

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

gruntfile中,使用 grunt API 读取的 yaml 文件:

// --- 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']
    }
  });

并且似乎做了我需要的。唯一的怪癖是,名称为 _testReportFilePath.yml 的虚拟 yaml 文件必须始终存在于CWD中,以防止任何咕噜咕噜的错误。