Yarn: How to run scripts for all packages under a workspace?

Created on 15 Sep 2017  路  18Comments  路  Source: yarnpkg/yarn

I'm not sure whether this qualify as a bug or feature. I couldn't find anything related to this.

By using workspaces, can we run a common script command from the root directory, and it'll execute that script in all packages under that workspace.? (Just like how lerna does. When we run lerna run test from the root directory, it'll execute the test script for all the packages under that repo).

This would help a lot for CI/CD stuff.

cat-feature high-priority needs-discussion triaged

Most helpful comment

Then let's come up with a solution in the form of an RFC? Here are some ideas I see that can work:

  • Have a yarn workspaces list command so people can get the resolved workspaces to iterate on them with bash or whatever
  • Have a yarn workspaces run command that runs a script in each workspace and errors out when one is not found

I'm sure you folks can come up with more and better solutions though :)

All 18 comments

Well, idk if there is something like lerna in yarn, but as workround you can try to create scripts in root package calling scripts in your packages

"scripts": { "test": "yarn test-package1 && yarn test-package2" "test-package1": "cd package1 && yarn test", "test-package2": "cd package2 && yarn test" }

Though this won't work if test script runs forever until you manually shut that down (for example starting server), but i'm sure you can hack it :)

This was the first way that came to my mind. But this run scripts will eventually grow. (The one that I'm currently working has 8 packages, and I can't even think of adding a specific run script to each package 馃槵 ).

On the other hand lerna handles this beautifully. I know yarn doesn't replace lerna, but the workspaces feature is a tempting one, and it'd be cool to have it as a built-in feature 馃槆

Ye, adding scripts to more then 2 packages is tedious. Having that feature in yarn would be nice, totally agree :)

image
^ This sentence makes me sad

Then let's come up with a solution in the form of an RFC? Here are some ideas I see that can work:

  • Have a yarn workspaces list command so people can get the resolved workspaces to iterate on them with bash or whatever
  • Have a yarn workspaces run command that runs a script in each workspace and errors out when one is not found

I'm sure you folks can come up with more and better solutions though :)

+1 for the list command. I guess the workspaces sub-command will reduce a lot of overhead in the CLI.

@giridharangm would you like to give sketching out a fuller picture in an RFC so someone can implement it?

@BYK yeah, sure!. I haven't done any RFCs before, so, 馃槆

Closing this in favor of the RFC. Let's continue the discussion there.

There is the wsrun package that runs commands in multiple packages in a yarn workspaces mono repo. It would be great to have the same functionality in yarn itself. I'm just thinking it might be worth to look at how wsrun works today and what options it has etc. in order to gather info for the RFC.

This can also be achieved via this command, but requires jq. (Not as good as ^ option)

COMMAND=build yarn workspaces info | awk 'NR>2 {print last} {last=$0}' | jq -r 'keys[]' | while IFS=$'\n' read -r workspace; do yarn workspace "$workspace" "$COMMAND"; done

Just in case, in the root folder, what I did was:

yarn add wsrun --dev

then in package.json added this script:

    "run-all": "yarn run wsrun",

so now I can run commands in all workspaces like this:

yarn run-all test

Although I agree with one of the contributors that yarn shouldn't be waiting time in this kind of high level features and probably this should be tackled by companion extensions / tools like this wsrun, I think this particular one is very easy to implement and very useful for large workspaces. Couldn't believe yarn don support it out of the box. I won't do PR cause probably will be rejected and this works fine.

my two cents.

This was added with #6244 and released in [email protected].

You can now do yarn workspaces run script

@BYK any plans on implementing --parallel flag? Current way makes our tests super slow and developers sad.

I think the original question was about running a script (like test) on all the packages in a workspace? @eps1lon right? I don't think yarn workspaces run script achieves that.

I think the original question was about running a script (like test) on all the packages in a workspace? @eps1lon right? I don't think yarn workspaces run script achieves that.

@Pranit-Harekar
From the context

(Just like how lerna does. When we run lerna run test from the root directory, it'll execute the test script for all the packages under that repo).

it was pretty clear that
yarn workspaces run test should be equivalent to yarn workspace workspaceA test && yarn workspace workspaceB test. It seems like that you think it should run a given script foreach workspace where cwd is the workspace directory? That is not how lerna works.

Thanks a lot for adding workspaces run! I can almost get rid of lerna now!

One missing piece, Is it possible to skip a package if no such script defined? For example, one package may not have "test" script defined in it's package.json. At the moment, yarn will fail with:

yarn run v1.13.0
error Command "test" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1

Thanks in advance!

@zhenwenc A new issue was created and referenced here: https://github.com/yarnpkg/yarn/issues/7121

Was this page helpful?
0 / 5 - 0 ratings