简体   繁体   English

设置环境变量grunt或grunt-exec

[英]Set environment variables grunt or grunt-exec

I'm trying to use grunt-exec to run a javascript test runner with a deployed link variable passed in. 我正在尝试使用grunt-exec来运行传入的已部署链接变量的javascript测试运行器。

I am trying to do so by setting an environment variable grunt.option('link') using exec:setLink . 我试图通过使用exec:setLink设置环境变量grunt.option('link')exec:setLink In my test_runner.js I grab the variable with process.env.TEST_LINK . 在我的test_runner.js我使用process.env.TEST_LINK获取变量。 Unfortunately, it appears that grunt-exec won't run bash commands such as export(?) 不幸的是,似乎grunt-exec不会运行bash命令,例如export(?)

Really, I don't care how the variable gets to my test_runner.js so any other ideas would be welcome. 真的,我不关心变量如何到达我的test_runner.js所以任何其他想法都会受到欢迎。

exec: {

  // DOESN'T WORK: Sets env variable with link for selenium tests
  setLink: {
    cmd: function () {
      return "export TEST_LINK=" + "'" + grunt.option('link') + "'";
    }
  },
  // Integration tests, needs TEST_LINK
  selenium: {
    cmd: function () {
      return "node test/runner/jasmine_runner.js";
    }
  }

With grunt-exec , environment variables for the child process can be specified in the env option : 使用grunt-exec ,可以在env选项中指定子进程的环境变量:

exec: {
  selenium: {
    cmd: function () {
      return "node test/runner/jasmine_runner.js";
    },
    options: {
      env: {
        'TEST_LINK': grunt.option('link')
      }
    }
  }
}

One thing to bear in mind is that if only TEST_LINK is specified in the env option, that will be the only environment variable for the child process. 要记住的一件事是,如果在env选项中只指定了TEST_LINK ,那么这将是子进程的唯一环境变量。 If you want the current process's environment variables to be passed, too, you can do something like this: 如果您希望传递当前进程的环境变量,也可以执行以下操作:

exec: {
  selenium: {
    cmd: function () {
      return "node test/runner/jasmine_runner.js";
    },
    options: {
      env: Object.assign({}, process.env, { 'TEST_LINK': grunt.option('link') })
    }
  }
}

I ended up just using node process.env['TEST_LINK'] = grunt.option('link'); 我最后只使用了node process.env['TEST_LINK'] = grunt.option('link'); Then retrieved in my javascript with process.env['TEST_LINK']; 然后在我的javascript中使用process.env['TEST_LINK'];检索process.env['TEST_LINK'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM