Run script for all workspaces without having to add every possible script to every workspace

Workspaces have different requirements. They may be grouped into related packages that accept certain script commands appropriate for their functionality.

It is not possible to run a script for all workspaces without failing on the first workspace that does not have a script.

If there’s some packages that use “apply”, then we have to add: "apply": "echo", to every other package across the project, even if that script is meaningless for a given package.

It would be excellent if we could use yarn workspaces run script across all workspaces, while having the option to skip workspaces which do not have a script.

Author: Fantashit

3 thoughts on “Run script for all workspaces without having to add every possible script to every workspace

  1. Teh Background

    yarn workspaces run [script] fails if a package is missing the script. These are the relevant lines of code

    It’s just a dead-simple iterator that tries to execute an arbitrary yarn command on a package. It has no knowledge of what the command is.

    Teh Problem

    Adding checks to see if we’re issuing an npm run esque command and then doing introspection would turn the yarn workspaces command into what I personally deem terribly organized code.


    1. yarn workspaces --error=early|late|never
      early is default, current behavior
      late finishes looping if any workspaces errors, but the result is an error (the first? an array?). Missing scripts will still result in an error
      never finishes looping, doesn’t yield errors ever

    2. yarn run [script] --no-missing-error
      The basic script runner will get the seemingly useless flag to not throw errors if the specified script is missing. This means we simply use this flag in the yarn workspaces scenario. The workspaces command doesn’t get cluttered with logic and checks specifically for npm run-like commands. yarn run gets a weird flag that’s useless by itself.
      This enables: yarn workspaces run [script] --no-missing-error but this flag would have to be passed every time you want a forgiving yarn workspaces command.

    I could submit a PR but I’m interested in suggestions on how to refactor this to not be hella-ugly code.


  2. @ProLoser, So we can expect this comes out in yarn 2. Does it only scoped to yarn 2, or could we have this as a minor feature release to v1, it looks super helpful! 💪

Comments are closed.