如何使用grunt-run来执行npm脚本? [英] How to execute npm script using grunt-run?
问题描述
在我的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
类似于示例。文件中显示。
- 将
cmd
的值设置为npm
。 - 在
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']);
};
正在运行 注意:添加 编辑: 跨平台 b $ b 使用上面显示的 对于跨平台解决方案,请考虑安装和使用 grunt-shell 来调用 Gruntfile.js
<使用上面显示的配置通过CLI运行 $ grunt
将会调用 npm run test-jest
命令。 / p>
- silent
(或者它的缩写相当于 -s
数组只是帮助避免了额外的npm日志到控制台。 b
$ b grunt-run
解决方案在通过 cmd.exe
运行时在Windows OS上失败。 。引发以下错误:
错误:spawn npm ENOENT警告:非零退出代码-4058使用 - -force继续。
npm run test-jest
。
npm i -D grunt-shell
$ 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']);
};
笔记
-
grunt-shell
需要 load-grunt-tasks 来加载Task,而不是典型的grunt.loadNpmTasks(...)
,所以你需要安装也是:
npm i -D load-grunt-tasks
- 对于旧版本的Windows,我必须安装旧版本的
grunt-shell
,即版本1.3.0
,所以我建议安装早期版本。
$ bnpm i -D grunt-shell@1.3.0
编辑2
grunt-run
如果使用exec
键而不是cmd
和args
键...
对于跨平台的用途...我发现它需要使用
exec
键将命令指定为单个字符串,如下所示:
如果您想将您的命令指定为单个字符串,可以使用
来指定一个任务中的多个命令,请使用exec:key
Gruntfile.js
$ bmodule.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.- Set the value for the
cmd
tonpm
. - Set
run
andtest-jest
in theargs
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 thenpm run test-jest
command.Note: Adding
--silent
(or it's shorthand equivalent-s
) to theargs
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 viacmd.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
grunt-shell
requires load-grunt-tasks for loading the Task instead of the typicalgrunt.loadNpmTasks(...)
, so you'll need to install that too:
npm i -D load-grunt-tasks
- For older version of Windows I had to install an older version of
grunt-shell
, namely version1.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 theexec
key instead of thecmd
andargs
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屋!
- Set the value for the