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.
I wanted postinstall
to work with yarn workspaces,
"postinstall": "yarn workspaces run postinstall"
So I had to add postinstall
scripts to every package, even if they didn't need it.
It makes pages of scrolling:
[4/4] 🔨 Building fresh packages...
success Saved lockfile.
$ yarn workspaces run postinstall
yarn workspaces v1.15.2
yarn run v1.15.2
✨ Done in 0.43s.
yarn run v1.15.2
$ echo
✨ Done in 0.10s.
yarn run v1.15.2
$ echo
✨ Done in 0.10s.
yarn run v1.15.2
$ echo
✨ Done in 0.09s.
yarn run v1.15.2
$ echo
✨ Done in 0.10s.
yarn run v1.15.2
$ echo
✨ Done in 0.12s.
yarn run v1.15.2
$ echo
✨ Done in 0.09s.
yarn run v1.15.2
$ echo
✨ Done in 0.10s.
yarn run v1.15.2
$ echo
✨ Done in 0.11s.
yarn run v1.15.2
$ echo
...
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.
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.
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
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.
Thoughts?
Any news on this ? :)
@hrougier https://github.com/yarnpkg/berry/pull/163 which closes https://github.com/yarnpkg/berry/issues/62
berry is the yarn2 repo
@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! 💪
I'm sorry but I would like to ask not to point to Yarn 2 being a solution as even the docs says - "Yarn v2 is a very different software from the v1".
I've tried to use Yarn 2 in my workspace project at it failed to install dependencies (first because of lack of git resolvers and then because it failed to list releases for who know what reasons) while NPM and Yarn v1 both installing without any issues.
So that being said - we would really love to see this behavior (running non failing scripts in workspace) in current Yarn v1 version.
I suggest using Lerna. It can be configured to work on top of Yarn Workspaces, so for all install needs it would actually call yarn.
Then in Lerna you can:
lerna run postinstall
I've tried to use Yarn 2 in my workspace project at it failed to install dependencies (first because of lack of git resolvers and then because it failed to list releases for who know what reasons)
@gund there is a way to use Yarn 2 but keep the legacy "node_modules" resolution system, which should fix the installation issues you have.
@PaulRBerg yes I'm aware of that and in fact I did use it and got those errors anyways just because Yarn v2 is doing things differently than v1 or NPM.
Even though I managed to update my project to work with Yarn v2 I think other project may not be able to do this or it might be just too much of an effort to migrate.
That is why I suggested not to rely on "fixed by Yarn v2" approach but add this feature right here on Yarn v1 workspaces.
Most helpful comment
Teh Background
yarn workspaces run [script]
fails if a package is missing the script. These are the relevant lines of codeIt'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 theyarn workspaces
command into what I personally deem terribly organized code.Solutions
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
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. Theworkspaces
command doesn't get cluttered with logic and checks specifically fornpm 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 forgivingyarn workspaces
command.I could submit a PR but I'm interested in suggestions on how to refactor this to not be hella-ugly code.
Thoughts?