【问题标题】:How to execute npm script using grunt-run?如何使用 grunt-run 执行 npm 脚本?
【发布时间】:2018-04-28 05:33:37
【问题描述】:

我的 package.json 文件中有一个 npm 任务,如下所示执行开玩笑测试:

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

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

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

【问题讨论】:

    标签: node.js reactjs npm gruntjs jestjs


    【解决方案1】:

    配置您的Gruntfile.js,类似于文档中显示的example

    1. cmd 的值设置为npm
    2. args 数组中设置runtest-jest

    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 命令。

    注意:在 args 数组中添加 --silent(或其等效的简写 -s)只是有助于避免将额外的 npm 日志添加到控制台。


    编辑:

    跨平台

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

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

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

    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(...),因此您也需要安装它:

    npm i -D load-grunt-tasks

    1. 对于旧版本的 Windows,我必须安装旧版本的 grunt-shell,即版本 1.3.0,因此我建议安装旧版本。

    npm i -D grunt-shell@1.3.0


    编辑 2

    如果您使用exec 键而不是cmdargs 键,grunt-run 似乎确实可以在 Windows 上运行...

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

    如果您想将命令指定为单个字符串,这很有用 要在一项任务中指定多个命令,请使用 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' ]);
    
    };
    

    【讨论】:

    • 通过 CLI 运行任务向我抛出了这个错误:运行“run:npm_test_jest”(运行)任务>>错误:spawn npm ENOENT 警告:非零退出代码 -4058 使用 --force 继续.由于警告而中止。
    • 是的,使用 grunt-run 在 Windows 上运行时我也遇到了同样的错误 - 但它在 Mac 上成功运行。您是否尝试过使用grunt-shell 而不是grunt-run - 如我回答的跨平台 部分所述?我在 Windows 和 Mac 上都成功使用了 grunt-shell
    • 谢谢 Manu - 实际上我刚刚找到了一种使用 grunt-run 跨平台使用 exec 键的方法。因此,如果您的偏好是使用 grunt-run 而不是 grunt-shell,请在 EDIT 2 部分下查看我的更新答案。
    • @Mark - 您是否尝试过完全省略args 键/值(根据我在edit2 中的建议),并将npm run build 命令作为exec 键的值。例如:grunt.initConfig({ run: { build: { exec: 'npm run build' } } });
    • @Mark - 尝试直接在命令行中运行npm run build,如果错误仍然存​​在,则表明该错误与grunt-run 无关。它很可能与您正在运行的 nodejs 脚本有关。在您的app.js 脚本中,您肯定需要更改this comment 中的建议。直接在命令行中运行npm run build 成功后,使用grunt-run 应该没问题。
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2021-04-13
    • 1970-01-01
    • 2021-10-14
    • 2020-03-02
    相关资源
    最近更新 更多