简体   繁体   English

如何从grunt任务中运行node_modules中的脚本?

[英]How can I run a script in node_modules from a grunt task?

I want to simplify how our test suites can be run from the command line. 我想简化从命令行运行测试套件的方法。 So I added the following grunt-shell task to our Gruntfile.js : 所以我将以下grunt-shell任务添加到我们的Gruntfile.js

module.exports = function( grunt ) {

  grunt.initConfig(
    {
      shell : {
        e2e : {
          command : "./node_modules/protractor/bin/protractor protractor_conf.js"
        }
      }
    }
  );

  grunt.loadNpmTasks( "grunt-shell" );

  grunt.registerTask( "e2e", ["shell:e2e"] );
};

When I run the task, I get the error: 当我运行任务时,我收到错误:

'.' is not recognized as an internal or external command, operable program or batch file.

All the examples I found for running shell commands ran binaries which were globally accessible, but I want to start the protractor binary that was installed locally. 我发现的运行shell命令的所有示例都运行了全局可访问的二进制文件,但我想启动本地安装的量角器二进制文件。

I'm using bash on Windows, in case that matters. 我在Windows上使用bash,以防万一。

I'm working on OSX... but it should still apply for you? 我正在研究OSX ......但是它仍然适用于你?

I just provided an absolute path in my Gruntfile: 我刚在Gruntfile中提供了一个绝对路径:

module.exports = function(grunt) {

    grunt.initConfig({
        shell: {
            test: {
                // command: 'sh /Users/default/Sites/dev/test.sh'
                command: 'node /Users/default/Sites/dev/test.js'
            }
        }
    });

    grunt.loadNpmTasks('grunt-shell');
    grunt.registerTask('default', ['shell']);
};

Which provided me with the correct output. 这为我提供了正确的输出。

Some other useful tasks for similar problems I came across: 我遇到的类似问题的其他一些有用的任务:

grunt-execute, grunt-run grunt-execute,grunt-run

You can pass protractor as an argument to node to invoke it, like so: 您可以将protractor作为参数传递给node以调用它,如下所示:

module.exports = function( grunt ) {

  grunt.initConfig(
    {
      shell : {
        e2e : {
          command : "node ./node_modules/protractor/bin/protractor protractor_conf.js"
        }
      }
    }
  );

  grunt.loadNpmTasks( "grunt-shell" );

  grunt.registerTask( "e2e", ["shell:e2e"] );
};

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

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