如何使用 grunt-run 执行 npm 脚本? [英] How to execute npm script using grunt-run?

查看:13
本文介绍了如何使用 grunt-run 执行 npm 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 package.json 文件中有一个 npm 任务,用于执行 jest 测试:

I have a npm task in my package.json file as follows to execute jest testing:

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

我想使用 grunt 执行这个任务 npm run test-jest.我为此安装了 grunt-run 并添加了 run 任务,但是如何在那里调用此 npm 任务?

I want to execute this task npm run test-jest using grunt. I installed grunt-run for the same and added the run task, but how do I invoke this npm task there?

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

推荐答案

配置你的 Gruntfile.js 类似于 示例 显示在文档中.

Configure your Gruntfile.js similar to the example shown in the docs.

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

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' ]);

};

跑步

使用上面显示的配置通过 CLI 运行 $ grunt 将调用 npm run test-jest 命令.

Running $ grunt via your CLI using the configuration shown above will invoke the npm run test-jest command.

注意:向 args 数组添加 --silent(或其速记等效的 -s)只是有助于避免额外的 npm 日志到控制台.

Note: Adding --silent (or it's shorthand equivalent -s) to the args Array simply helps avoids the additional npm log to the console.

跨平台

通过 cmd.exe 运行时,使用上面显示的 grunt-run 解决方案在 Windows 操作系统上失败.抛出了以下错误:

Using the grunt-run solution shown above failed on Windows OS when running via cmd.exe. The following error was thrown:

错误:生成 npm ENOENT 警告:非零退出代码 -4058 使用 --force 继续.

对于跨平台解决方案,请考虑安装和使用 grunt-shell 来调用 npm run test-jest 代替.

For a cross-platform solution consider installing and utlizing grunt-shell to invoke the npm run test-jest instead.

npm i -D grunt-shell

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' ]);

};

注意事项

  1. grunt-shell 需要 load-grunt-tasks用于加载任务而不是典型的 grunt.loadNpmTasks(...),因此您也需要安装它:
  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:

npm i -D load-grunt-tasks

  1. 对于较旧版本的 Windows,我必须安装较旧版本的 grunt-shell,即版本 1.3.0,因此我建议安装较早版本.
  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.

npm i -D grunt-shell@1.3.0

编辑 2

grunt-run 似乎确实适用于 Windowsargs 键...

grunt-run does seem to work on Windows if you use the exec key instead of the cmd and args keys...

出于跨平台目的...我发现有必要使用 exec 键将命令指定为单个字符串,如以下文档所示:

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

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' ]);

};

这篇关于如何使用 grunt-run 执行 npm 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆