belhadj haythem
belhadj haythem

Reputation: 711

Yarn run multiple scripts in parallel

I'm migrating from NPM to Yarn, and I want to run scripts in parallel such as:

npm-run-all --parallel script1 script2 script3

What is its equivalent in Yarn?

What I found as its equivalent is to run each separately:

yarn run script1 && yarn run script2 && yarn run script3

but I can't run scripts in parallel.

how to use multiple scripts & in parallel?

Upvotes: 46

Views: 65233

Answers (6)

magikMaker
magikMaker

Reputation: 5607

There is a difference between using & and &&. Using & will run scripts in parallel, using && will run scripts one after the other.

package.json:

{
    "parallel": "yarn script1 & yarn script2",
    "serial": "yarn script1 && yarn script2",
    "script1": "... some script here",
    "script2": "... some there script here"
}

Upvotes: 69

Ifnot
Ifnot

Reputation: 5103

If your scripts are intended to run continuously in background like daemons, you can use fly-run.

I am using it when I need to spawn multiple processes (yarn serve of multiple micro-services projects in development).

Install it:

yarn global add @ifnot/fly-run

Create a configuration file config.js:

module.exports = [
  {
    name: 'script1',
    command: '/path/to/script1'
  },
  {
    name: 'script1',
    command: '/path/to/script2'
  },
  // more ...
]

Run it using the configuration:

fly-run config.js

Use it :

CTRL+C closes all your spawned process, type restart script1 for restarting one script, more commands and configuration on the github.

Upvotes: -1

yorch
yorch

Reputation: 7318

Probably a bit off the question, but in case Yarn v2+ with Workspaces is being used, there is a plugin (workspace-tools) that makes it easier to run a script in all of the workspaces's packages (ie: if you have a monorepo and need to run start in all of them):

# Install plugin:
yarn plugin import workspace-tools

# Run script start:dev on all of the packages in parallel
yarn workspaces foreach -p -v -i run start
  • -p: Run the commands in parallel
  • -v: Prefix each output line with the name of the originating workspace
  • -i: Print the output of commands in real-time instead of buffering it. Found this useful as in some cases, no output will be shown.

More info: https://yarnpkg.com/cli/workspaces/foreach

Upvotes: 3

Kevin Struillou
Kevin Struillou

Reputation: 914

You can use https://www.npmjs.com/package/yarn-run-all which is made for this purpose.

Edit:

My answer was wrong.

npm-run-all is compatible with yarn:

If a script is invoked with Yarn, npm-run-all will correctly use Yarn to execute the plan's child scripts.

Additionnaly, the yarn-run-all package is linked to repository https://github.com/mysticatea/npm-run-all...

In the end, I don't understand why the yarn-run-all package exists.

Anyways, just use npm-run-all package instead of yarn-run-all.

Upvotes: 13

raul
raul

Reputation: 620

From what I read on documentation of npm-run-all, you can just keep using it, and, as long as you run the script with yarn it will use YARN to run scripts in parallel.

Here is the original quote from https://github.com/mysticatea/npm-run-all

Yarn Compatibility

If a script is invoked with Yarn, npm-run-all will correctly use Yarn to execute the plan's child scripts.

Upvotes: 18

arcuri82
arcuri82

Reputation: 970

you can use concurrently. E.g.:

concurrently "yarn run script1"   "yarn run script2"   "yarn run script3"

Upvotes: 21

Related Questions