NPM 运行 并行任务,但等待资源可用于 运行 第二个任务

NPM run parallel task, but wait until resource is available to run second task

在 npm 中,我如何 运行 两个或多个并行任务,但要等待第一个任务创建的资源可供第二个任务使用,等等?

示例(概念):

npm run task1 & waitfor task1 then task2 & waitFor task3 then task4 ...

有什么想法吗?

编辑

举个例子:假设我的第一个任务是启动网络服务器,第二个任务是每次发生事件时都从该网络服务器中获取数据。另一个例子:我的第一个任务可能是启动 webdriver-manager,我的第二个任务是启动一个网络服务器,我的第三个任务是 运行 e2e tests every time my files are changed。因此,我需要所有这些任务同时保持 运行ning,但它们需要按特定的顺序和时间进行初始化。

我不确定我是否完全理解您的要求,因此我将提供一些可能的解决方案。

您可能想要按顺序 运行 执行任务(下面的第一个解决方案),但是在您的 post 中您提到了并行。


1。 运行 按顺序执行任务

npm-scripts 支持通常在 bash shell 中使用的 && 运算符。然而,npm-scripts 中使用的 && 运算符确实可以跨平台成功运行。

使用 && 运算符链接任务的伪示例:

"scripts": {
    "foo": "npm run task1 && npm run task2 && npm run task3"
},

运行 $ npm run foo 使用上面的示例通过 CLI...

  1. 最初 运行s task1.
  2. task1 成功完成时 (即 task1 以零状态退出) 然后 task2 将是 运行.
  3. task2 成功完成后 task3 将是 运行。

如果 && 运算符的左侧由于任何原因失败,(即它以非零值 code/status 退出),后续任务( s)右边会失败运行。例如;如果 task1 失败,则 task2task3 不会 运行.


2。 运行 同时执行任务

npm-scripts 还支持单个 & 运算符跨平台同时进行 运行ning 任务。

使用&运算符的伪例子:

"scripts": {
    "foo": "npm run task1 & npm run task2 & npm run task3"
},

在此示例中,双 && 和单 & 运算符之间的主要区别在于,如果单 & 的左侧失败,则右侧 运行s 无论如何。例如;如果 task1 失败,则 task2 仍然 运行s.


3。 运行 并行任务。

为了 运行 并行任务,我建议您使用 parallelshell:

$ npm i -D parallelshell

使用parallelshell的伪示例:

"scripts": {
    "foo": "parallelshell \"npm run task1\" \"npm run task2\" \"npm run task3\""
},

此示例最初看起来与使用上一节中显示的单个 & 运算符非常相似,但它提供了 documentation 中列出的其他好处。主要好处 (IMO) 是:

If command1 or command2 exit with non-zero exit code, then this will not effect the outcome of your shell (i.e. they can fail and npm/bash/whatever will ignore it). parallelshell will not ignore it, and will exit with the first non-zero exit code.


4。 运行 并行和顺序执行任务。

假设你想 运行 task1task2 在 parallel/simultaneously 然后 运行 task3 只有当两个 task1task2 已成功完成。

为此,您可以同时使用 parallelshell 内置 && 运算符。

使用 parallelshell&& 运算符链接任务的伪示例:

"scripts": {
    "foo": "parallelshell \"npm run task1\" \"npm run task2\" && npm run task3"
},

编辑

基于以下 OP update/edit 的解决方案:

Another example: My first task could be starting webdriver-manager, my second task, starting a webserver, and my third task, run e2e tests everty time my files are changed. So, I need all those tasks to keep running concurrently, but they need to be initialized in an specific order and time.

5。 运行 并发任务。

concurrently 可同时用于 运行 个任务:

$ npm i -D concurrently

使用concurrently的伪示例:

"scripts": {
    "foo":  "concurrently \"npm run task1\" \"npm run task2\" \"npm run task3\""
},

此示例将同时保留所有任务 (1、2 和 3) 运行,并且它们将按指定的顺序开始。我对您的示例用例中提到的工具了解不够详细,但是可以根据需要组合此 post 中提供的所有示例以满足您的确切要求。

concurrently也有几个有用的options

您可以尝试 concurrently with wait-on 包来管理 conccurent/sequential 脚本和输出。 wait-on 顺序启动支持 head 响应状态,tcp 侦听,...

例如:

"scripts": {
    "start": "concurrently -k -n \"DB,API,WEB\" -c \"blue,magenta,yellow\" \"npm run start-db\" \"npm run start-api\" \"npm run start-web\"",
    "start-db": "myDbServerCmd",
    "start-api": "wait-on tcp:27017 && myApiServerCmd",
    "start-web": "myFrontServerCmd",
}

感谢 dchambers 的想法 (source)。