配置
Gruntfile.js与文档中显示的示例类似的示例。
- 将的值设置
cmd
为npm
。 - 设置
run
和test-jest
在args
数组中。
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' ]);};跑步
$ grunt使用上面显示的配置通过CLI 运行将调用该
npm run test-jest命令。
注意:向Array 添加
--silent(或等效的简写
-s)
args只是有助于避免向控制台添加额外的npm日志。
编辑:
跨平台
通过
grunt-runWindows运行时,无法在Windows操作系统上使用上述解决方案
cmd.exe。引发以下错误:
Error: spawn npm ENOENT Warning: non-zero exit pre -4058 Use --force tocontinue.
对于跨平台解决方案,请考虑安装并使用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' ]);};笔记
grunt-shell
需要load-grunt-tasks来加载Task而不是典型的grunt.loadNpmTasks(...)
,因此您也需要安装它:
npm i -D load-grunt-tasks
- 对于Windows的较早版本,我必须安装的较早版本
grunt-shell
,即version1.3.0
,因此我建议安装较早的版本。
npm i -D grunt-shell@1.3.0
编辑2
grunt-run
如果您使用exec
键而不是cmd
和args
键,则在Windows上似乎确实可以使用…
出于跨平台的目的…我发现有必要根据
exec阅读以下文档的密钥将命令指定为单个字符串:
如果要将命令指定为单个字符串,这对于在一个任务中指定多个命令很有用,请使用exec:键
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' ]);};


