How to execute npm script using grunt-run?

后端 未结 1 912
闹比i
闹比i 2020-12-19 08:26

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

  \"scripts\": {
    \"test-jest\": \"jest\",
    \"jest-coverage\": \"jest --c         


        
相关标签:
1条回答
  • 2020-12-19 08:59

    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' ]);
    
    };
    
    0 讨论(0)
提交回复
热议问题