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

4

我希望简化我们的测试套件在命令行中的运行方式。因此,我添加了以下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"] );
};

当我运行任务时,出现以下错误:

'.' 不被识别为内部或外部命令、可执行程序或批处理文件。

我找到的所有运行shell命令的示例都运行了全局可访问的二进制文件,但我想启动本地安装的protractor二进制文件。我正在使用Windows上的bash,如果有影响的话。
2个回答

1
我正在使用OSX系统……但这对您仍然适用吗?
我只需在我的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']);
};

我使用它提供的正确输出。

我遇到了一些类似问题的其他有用任务:

grunt-execute,grunt-run


哈哈!这个对我帮助很大。不幸的是,我不能使用绝对路径,因为它会根据工作站而改变。但是使用 node 作为二进制文件,然后将 protractor 作为参数传递就可以完美解决。 - Oliver Salzburg

0

您可以将 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"] );
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接