将价值从conf.js传递给一个咕task任务或咕噜任务
如何在grunt
任务之间传递信息? 我想从一个grunt
task任务传递一个价值到另一个grunt
task任务。
我的情况是,在完成量angular器testing之后,我想把一个价值传递给一个新的grunt
task任务。 为了达到这个目的,我继续在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
在gruntjs文件中似乎是undefined
的。
这个答案早就过时了。 我确实按照@mparnisaribuild议将variables写入文件。 所以我在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
以防止出现任何咕噜声错误。