简体   繁体   English

如何使用grunt-run执行npm脚本?

[英]How to execute npm script using grunt-run?

I have a npm task in my package.json file as follows to execute jest testing: 我在package.json文件中有一个npm任务,如下所示执行jest测试:

  "scripts": {
    "test-jest": "jest",
    "jest-coverage": "jest --coverage"
  },
  "jest": {
    "testEnvironment": "jsdom"
  },

I want to execute this task npm run test-jest using grunt. 我想使用grunt执行此任务npm run test-jest I installed grunt-run for the same and added the run task, but how do I invoke this npm task there? 我为此安装了grunt-run并添加了运行任务,但是如何在那里调用这个npm任务呢?

    run: {
        options: {
          // Task-specific options go here.
        },
        your_target: {
          cmd: 'node'
        }
      }

Configure your Gruntfile.js similar to the example shown in the docs. 配置您的Gruntfile.js类似于文档中显示的示例

  1. Set the value for the cmd to npm . cmd的值设置为npm
  2. Set run and test-jest in the args Array. args数组中设置runtest-jest

Gruntfile.js Gruntfile.js

module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-run');

  grunt.initConfig({
    run: {
      options: {
        // ...
      },
      npm_test_jest: {
        cmd: 'npm',
        args: [
          'run',
          'test-jest',
          '--silent'
        ]
      }
    }
  });

  grunt.registerTask('default', [ 'run:npm_test_jest' ]);

};

Running 运行

Running $ grunt via your CLI using the configuration shown above will invoke the npm run test-jest command. 使用上面显示的配置通过CLI运行$ grunt将调用npm run test-jest命令。

Note: Adding --silent (or it's shorthand equivalent -s ) to the args Array simply helps avoids the additional npm log to the console. 注意:将--silent (或它的简写等效-s )添加到args数组只会有助于避免额外的npm日志到控制台。


EDIT: 编辑:

Cross Platform 跨平台

Using the grunt-run solution shown above failed on Windows OS when running via cmd.exe . 在通过cmd.exe运行时,在Windows操作系统上使用上面显示的grunt-run解决方案失败。 The following error was thrown: 抛出以下错误:

Error: spawn npm ENOENT Warning: non-zero exit code -4058 Use --force to continue.

For a cross-platform solution consider installing and utlizing grunt-shell to invoke the npm run test-jest instead. 对于跨平台解决方案,请考虑安装和使用grunt-shell来调用npm run test-jest

npm i -D grunt-shell

Gruntfile.js Gruntfile.js

module.exports = function (grunt) {

  require('load-grunt-tasks')(grunt); // <-- uses `load-grunt-tasks`

  grunt.initConfig({
    shell: {
      npm_test_jest: {
        command: 'npm run test-jest --silent',
      }
    }
  });

  grunt.registerTask('default', [ 'shell:npm_test_jest' ]);

};

Notes 笔记

  1. grunt-shell requires load-grunt-tasks for loading the Task instead of the typical grunt.loadNpmTasks(...) , so you'll need to install that too: grunt-shell需要load-grunt-tasks来加载Task而不是典型的grunt.loadNpmTasks(...) ,所以你也需要安装它:

npm i -D load-grunt-tasks

  1. For older version of Windows I had to install an older version of grunt-shell , namely version 1.3.0 , so I recommend installing an earlier version. 对于旧版本的Windows,我必须安装旧版本的grunt-shell ,即版本1.3.0 ,所以我建议安装早期版本。

npm i -D grunt-shell@1.3.0


EDIT 2 编辑2

grunt-run does seem to work on Windows if you use the exec key instead of the cmd and args keys... 如果使用exec键而不是cmdargs键, grunt-run似乎在Windows上grunt-run ...

For cross platform purposes... I found it necessary to specify the command as a single string using the exec key as per the documentation that reads: 出于跨平台的目的......我发现有必要使用exec键将命令指定为单个字符串,其文档如下:

If you would like to specify your command as a single string, useful for specifying multiple commands in one task, use the exec: key 如果要将命令指定为单个字符串,对于在一个任务中指定多个命令很有用,请使用exec:key

Gruntfile.js Gruntfile.js

module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-run');

  grunt.initConfig({
    run: {
      options: {
        // ...
      },
      npm_test_jest: {
        exec: 'npm run test-jest --silent' // <-- use the exec key.
      }
    }
  });

  grunt.registerTask('default', [ 'run:npm_test_jest' ]);

};

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

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