Do you want to request a feature or report a bug?
feature
What is the current behavior?
yarn run
does not have an option to exit with a zero code if the script run is not defined in package.json
.
My use case is that I manage a mono-repo using yarn and lerna and I use something like
lerna exec --scope -- yarn run script-name
or run yarn run script-name
in a loop over all the packages.
This is fragile because it breaks if any of the packages dont have the script-name
defined.
What is the expected behavior?
Similar to npm, provide a --if-present option that exits with a zero code if the script is undefined.
yarn run --if-defined script-name
Please mention your node.js, yarn and operating system version.
$ node -v
v8.15.0
$ yarn -v
1.12.3
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.13.6
BuildVersion: 17G3025
npm run
has a --if-present
flag, and it would be really handy if there was feature parity on this. Is there another way to achieve that with Yarn?
(Our use case is a shared CI configuration that runs a few scripts such as yarn test
and yarn lint
on a few repositories, not all of which have all those scripts)
EDIT: I missed that @HavaH123 had mentioned npm's --if-present
in his issue description.
Thanks for the info about npm's --if-present
flag. I didn't know that existed. I think this is a good idea. Care to work up a PR? 馃樃
@rally25rs Hey, I'd like to take this on!
Hi @Tranced, are you actively working on this? If not, I'd like to give it a shot. Thanks.
Any news on this? I'm really missing a feature like this at the moment. Would be amazing to get this in.
As a maintainer of a monorepo with a few packages, I'm also interested in having --if-present
. Some of them require build before publishing, but some do not due to their simplicity. Some are in TS, some are in pure JS. Some have tests, some do not. What I want to do in CI is to call top-level build
, lint:tsc
and test
commands without caring if they exist in every package.
// top-level package.json (ideal scenario)
"scripts": {
"build": "yarn workspaces run --if-present build",
// ...
"lint:tsc": "yarn workspaces run --if-present lint:tsc",
"test": "yarn workspaces run --if-present test"
},
Until --if-present
is available, some of my packages/脳/package.json
files have to contain these dummy scripts for the top-level commands to work:
// packages/脳/package.json
"scripts": {
"build": "echo \"build is not configured for package \\\"脳\\\", skipping.\"",
"lint:tsc": "echo \"TypeScript is not configured for package \\\"脳\\\", skipping.\"",
"test": "echo \"Unit testing is not configured for package \\\"脳\\\", skipping.\""
},
That's clearly redundant.
After accidentally hearing about npm run --if-present
I got quite exited, being sure that the same flag exists for Yarn Workspaces too. However, it now seems that this is not the case and I should undo my premature deletion of dummy scripts 馃榿
I'd like to pick this up - it wasn't too bad to get working locally, hope to have something reviewable soon.
Opened MR #7603 for this feature.
Didn't see @ulentini's open MR for this, the one I produced is not much different.
Should this issue be closed? The conversation on that MR stalled, this doesn't seem like something the maintainer wants added to yarn.
Also looking forward this.
@mbpreble I'm looking at the other PR (#7159) and I think some of the feedback still applies to yours. Notably this comment:
Wouldn't silencing with
|| true
also silence any errors if the script exist but fails? Imagine running a script liketest
,test:e2e
orlint
. There's definitely a use-case for exiting with 0 if the script just doesn't exist, especially for things like generalized CI-pipelines.
Maybe if there's a test case for that it will have a higher change of being accepted?
That comment seems to stand well enough on its own as being true. I agree this feature does provide a different behavior than just ignoring all errors when invoking yarn.
There are a few people people asking for this. @arcanis - could you take another look and help move this toward a resolution one way or the other?
Maybe @rally25rs could be deassigned to this issue because he doesn't seem involved anymore and maybe somebody else could be assigned? Somebody willing to do it like @mbpreble .
I would really like this feature too!
Issue still persists in March 30, 2020. Why did this issue go stale?
Guys any update over this issue?
yarn workspaces foreach run ...
(exclusive to the v2) already skips scripts that don't exist, and together with its options you have the same behavior as described in the op without having to add a lengthy option to run
.
Is there a way to benefit from this without using workspaces
? My workflow described above (https://github.com/yarnpkg/yarn/issues/6894#issuecomment-456922039) doesn't use them.
Most helpful comment
As a maintainer of a monorepo with a few packages, I'm also interested in having
--if-present
. Some of them require build before publishing, but some do not due to their simplicity. Some are in TS, some are in pure JS. Some have tests, some do not. What I want to do in CI is to call top-levelbuild
,lint:tsc
andtest
commands without caring if they exist in every package.Until
--if-present
is available, some of mypackages/脳/package.json
files have to contain these dummy scripts for the top-level commands to work:That's clearly redundant.
After accidentally hearing about
npm run --if-present
I got quite exited, being sure that the same flag exists for Yarn Workspaces too. However, it now seems that this is not the case and I should undo my premature deletion of dummy scripts 馃榿