In npm, how can I run two or more parallel tasks, but waiting for the resource that the first task will create to be available to the second task to use it, and so forth?
example (conceptual):
npm run task1 & waitfor task1 then task2 & waitFor task3 then task4 ...
any ideas?
EDIT
As an example: Lets say that my first task is starting a webserver, and my second task is consuming data from that web-server every time an event happens. 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.
I'm not sure I fully understand your requirement, so I'll provide a few possible solutions.
It may be that you are wanting to run tasks sequentially (the first solution below), however in your post you mention parallel.
1. Running Tasks sequentially
npm-scripts
supports the&&
operator which is typically used in bash shells. The&&
operator, used innpm-scripts
, does however work successfully cross-platform.Pseudo example using the
&&
operator for chaining tasks:Running
$ npm run foo
via your CLI using the example above...task1
.task1
has successfully completed (i.e.task1
exits with a status of zero) thentask2
will be run.task2
has successfully completed thentask3
will be run.If the left side of
&&
operator fails for whatever reason, (i.e. it exits with non-zero code/status), the subsequent task(s) on the right side will fail to run. For example; iftask1
fails thentask2
andtask3
will not run.2. Running Tasks simultaneously
npm-scripts
also supports the single&
operator cross-platform for running tasks simultaneously.Pseudo example using the
&
operator:In this example the main difference between the double
&&
and the single&
operator is that if the left side of the single&
fails, then the right side runs regardless. For example; iftask1
fails thentask2
still runs.3. Running Tasks in parallel.
To run tasks in parallel I recommend you utilize parallelshell:
$ npm i -D parallelshell
Pseudo example using
parallelshell
:This example initially seems very similar to using the single
&
operator shown in the previous section, however it offers additional benefits as listed in the documentation. The main benefit (IMO) being:4. Running Tasks in parallel and sequentially.
Lets say you want to run
task1
andtask2
in parallel/simultaneously and then runtask3
only when bothtask1
andtask2
have successfully completed.To achieve this you can utilize both
parallelshell
and the built-in&&
operator.Pseudo example using
parallelshell
and the&&
operator for chaining tasks:EDIT
A solution based on the following OP's update/edit:
5. Running Tasks concurrently.
concurrently can be utilized to run tasks concurrently:
$ npm i -D concurrently
Pseudo example using
concurrently
:This example will keep all tasks (1,2, and 3) running concurrently, and they'll start in the order specified. I don't know enough detail of the tools mentioned in your example use case, however all examples provided in this post can be combined as necessary to meet your exact requirement.
concurrently
also has several useful options too.You can try concurrently with wait-on package in order to manage conccurent/sequential scripts and outputs. wait-on sequential launches support head response status, tcp listening, ...
For example :
Thanks dchambers for the idea (source).