Javascript 如何并行运行多个 npm 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30950032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run multiple npm scripts in parallel?
提问by André Pena
In my package.json
I have these two scripts:
在我的package.json
我有这两个脚本:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
}
I have to run these 2 scripts in paralleleverytime I start developing in Node.js. The first thing I thought of was adding a third script like this:
每次开始在 Node.js 中进行开发时,我都必须并行运行这两个脚本。我想到的第一件事是添加这样的第三个脚本:
"dev": "npm run start-watch && npm run wp-server"
... but that will wait for start-watch
to finish before running wp-server
.
...但这将start-watch
在运行之前等待完成wp-server
。
How can I run these in parallel?Please keep in mind that I need to see the output
of these commands. Also, if your solution involves a build tool, I'd rather use gulp
instead of grunt
because I already use it in another project.
我怎样才能并行运行这些?请记住,我需要查看output
这些命令。此外,如果您的解决方案涉及构建工具,我宁愿使用gulp
而不是grunt
因为我已经在另一个项目中使用它。
回答by Neil Kistner
Use a package called concurrently.
使用名为concurrently的包。
npm i concurrently --save-dev
npm i concurrently --save-dev
Then setup your npm run dev
task as so:
然后这样设置你的npm run dev
任务:
"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
回答by Diogo Cardoso
If you're using an UNIX-like environment, just use &
as the separator:
如果您使用的是类 UNIX 环境,只需&
用作分隔符:
"dev": "npm run start-watch & npm run wp-server"
Otherwise if you're interested on a cross-platform solution, you could use npm-run-allmodule:
否则,如果您对跨平台解决方案感兴趣,则可以使用npm-run-all模块:
"dev": "npm-run-all --parallel start-watch wp-server"
回答by o.v.
回答by nir
You should use npm-run-all(or concurrently
, parallelshell
), because it has more control over starting and killing commands. The operators &
, |
are bad ideas because you'll need to manually stop it after all tests are finished.
您应该使用npm-run-all(或concurrently
, parallelshell
),因为它可以更好地控制启动和终止命令。运营商&
,|
是坏的想法,因为你需要手动停止所有的测试都完成之后。
This is an example for protractor testing through npm:
这是通过 npm 进行量角器测试的示例:
scripts: {
"webdriver-start": "./node_modules/protractor/bin/webdriver-manager update && ./node_modules/protractor/bin/webdriver-manager start",
"protractor": "./node_modules/protractor/bin/protractor ./tests/protractor.conf.js",
"http-server": "./node_modules/http-server/bin/http-server -a localhost -p 8000",
"test": "npm-run-all -p -r webdriver-start http-server protractor"
}
-p
= Run commands in parallel.
-p
= 并行运行命令。
-r
= Kill all commands when one of them finishes with an exit code of zero.
-r
= 当其中一个命令以零退出代码结束时终止所有命令。
Running npm run test
will start Selenium driver, start http server (to serve you files) and run protractor tests. Once all tests are finished, it will close the http server and the selenium driver.
运行npm run test
将启动 Selenium 驱动程序,启动 http 服务器(为您提供文件)并运行量角器测试。完成所有测试后,它将关闭 http 服务器和 selenium 驱动程序。
回答by Behnam Mohammadi
You can use one &
for parallel run script
您可以将一个&
用于并行运行脚本
"dev": "npm run start-watch & npm run wp-server"
回答by Corey
A better solution is to use &
更好的解决方案是使用 &
"dev": "npm run start-watch & npm run wp-server"
回答by Darkowic
I've checked almost all solutions from above and only with npm-run-allI was able to solve all problems. Main advantage over all other solution is an ability to run script with arguments.
我已经检查了上面几乎所有的解决方案,只有使用npm-run-all我才能解决所有问题。与所有其他解决方案相比,主要优势是能够使用参数运行脚本。
{
"test:static-server": "cross-env NODE_ENV=test node server/testsServer.js",
"test:jest": "cross-env NODE_ENV=test jest",
"test": "run-p test:static-server \"test:jest -- {*}\" --",
"test:coverage": "npm run test -- --coverage",
"test:watch": "npm run test -- --watchAll",
}
Note
run-p
is shortcut fornpm-run-all --parallel
注意
run-p
是快捷方式npm-run-all --parallel
This allows me to run command with arguments like npm run test:watch -- Something
.
这允许我运行带有类似参数的命令npm run test:watch -- Something
。
EDIT:
编辑:
There is one more useful optionfor npm-run-all
:
还有一个有用的选项为npm-run-all
:
-r, --race - - - - - - - Set the flag to kill all tasks when a task
finished with zero. This option is valid only
with 'parallel' option.
Add -r
to your npm-run-all
script to kill all processes when one finished with code 0
. This is especially useful when you run a HTTP server and another script that use the server.
添加-r
到您的npm-run-all
脚本以在完成代码时终止所有进程0
。这在您运行 HTTP 服务器和使用该服务器的另一个脚本时特别有用。
"test": "run-p -r test:static-server \"test:jest -- {*}\" --",
回答by Entity Black
I have a crossplatform solution without any additional modules. I was looking for something like a try catch block I could use both in the cmd.exe and in the bash.
我有一个没有任何附加模块的跨平台解决方案。我正在寻找类似 try catch 块的东西,我可以在 cmd.exe 和 bash 中使用。
The solution is command1 || command2
which seems to work in both enviroments same. So the solution for the OP is:
解决方案command1 || command2
似乎在两种环境中都有效。所以OP的解决方案是:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
// first command is for the cmd.exe, second one is for the bash
"dev": "(start npm run start-watch && start npm run wp-server) || (npm run start-watch & npm run wp-server)",
"start": "npm run dev"
}
Then simple npm start
(and npm run dev
) will work on all platforms!
那么简单npm start
(和npm run dev
)将适用于所有平台!
回答by Neil Girardi
If you replace the double ampersand with a single ampersand, the scripts will run concurrently.
如果将双与号替换为单个与号,脚本将同时运行。
回答by noego
npm-run-all --parallel task1 task2
edit:
编辑:
You need to have npm-run-allinstalled beforehand. Also check this pagefor other usage scenarios.
您需要事先安装npm-run-all。另请查看此页面以了解其他使用场景。