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

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

问题描述

在我的package.json文件中有一个npm任务,如下执行笑话测试:

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

我想执行这个任务 npm使用grunt运行test-jest

  run:我运行grunt-run并添加了运行任务,但是如何在那里调用这个npm任务? {
选项:{
//特定于任务的选项转到此处。
},
your_target:{
cmd:'node'
}
}


解决方案

配置您的 Gruntfile.js 类似于示例。文件中显示。


  1. cmd 的值设置为 npm

  2. args 数组中设置 run test-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 命令。 / p>

注意:添加 - silent (或者它的缩写相当于 -s 数组只是帮助避免了额外的npm日志到控制台。 b

编辑:
$ b

跨平台 b $ b

使用上面显示的 grunt-run 解决方案在通过 cmd.exe 运行时在Windows OS上失败。 。引发以下错误:


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


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



npm i -D grunt-shell



Gruntfile.js


$ b

  module.exports = function(grunt) {

require('load-grunt-tasks')(grunt); //< - 使用`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 来加载Task,而不是典型的 grunt.loadNpmTasks(...),所以你需要安装也是:

npm i -D load-grunt-tasks


  1. 对于旧版本的Windows,我必须安装旧版本的 grunt-shell ,即版本 1.3.0 ,所以我建议安装早期版本。
    $ b

    npm i -D grunt-shell@1.3.0






    编辑2



    grunt-run 如果使用 exec 键而不是 cmd args 键...



    对于跨平台的用途...我发现它需要使用 exec 键将命令指定为单个字符串,如下所示:


    如果您想将您的命令指定为单个字符串,可以使用
    来指定一个任务中的多个命令,请使用exec:key


    Gruntfile.js


    $ b

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

    };


    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"
      },
    

    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'
            }
          }
    

    解决方案

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

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

    Running

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

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


    EDIT:

    Cross Platform

    Using the grunt-run solution shown above failed on Windows OS when running via cmd.exe. 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.

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

    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:

    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.

    npm i -D grunt-shell@1.3.0


    EDIT 2

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

    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:

    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天全站免登陆