yarn
yarn copied to clipboard
Add --if-present option for scripts
Summary
Added --if-present
option flag in order to allow undefined scripts to run without exiting with a non-zero code.
This addresses #6894.
Test plan
Added a couple of tests in run.js
test file.
Not a fan. You shouldn't call the script in the first place, or should use || true
to silence errors if you need it.
Not a fan. You shouldn't call the script in the first place, or should use
|| true
to silence errors if you need it.
It was a feature request on #6894 to replicate an npm feature.
By the way, just out of curiosity, why do you think silencing with || true
would somehow be different than implementing this flag?
Wouldn't silencing with || true
also silence any errors if the script exist but fails? Imagine running a script like test
, test:e2e
or lint
. There's definitely a use-case for exiting with 0 if the script just doesn't exist, especially for things like generalized CI-pipelines.
This would be very much appreciated
+1
+1
Any chance the build error could be checked and this passed? This would be a lovely bit of functionality.
Any progress? This feature is really good
adding to the pile of people interesting in running this. this is quite useful and a npm standard 🙏
EDIT: if you ever want this into github actions, know that jq
is provided by default in the ubuntu latest machines and that therefore you can do:
[[ $(cat package.json| jq .scripts.test) != null ]] && yarn test
Why did this go stale? Would love to have this available.
@arcanis Please reconsider this. It has been demonstrated here and in the linked issue that it's an useful feature, especially in a CI environment. I don't see why there's such an opposition to this from your side, especially since npm
supports it and the implementation is fairly straightforward.
I hate it, always the same story. NPM doesnt support nohoist so I switch to yarn. Yarn then doesnt support something else because someone is not a fan of something he could simply merge and not use. So here I go again searching for a good package manager.
Not a fan. You shouldn't call the script in the first place, or should use
|| true
to silence errors if you need it.
@arcanis You have missed the point entirely. || true
does not cover the very real use case of running the script only if it exists, not just silently failing the script, weather it exists or not. Both npm
and pnpm
have this flag/feature.
Also a duplicate of #7603.
We have 50+ jobs using Jenkins Shared Libraries and the --if-present option would certainly make things easier as some repos have tests and some do not. However, for anyone encountering this on Jenkins on Windows I found a workaround that skips the Test stage if "test" is not defined in the package.json's scripts.
It's rather lengthy but seems to work in our case.
stage("Test") {
when {
expression {
// skip stage when "test" scripts are not defined
def scriptOutput = pwsh(returnStdout: true, script: '''
$script = Get-Content "package.json"
| ConvertFrom-Json
| Select-Object -ExpandProperty "scripts"
| Select-Object -ExpandProperty "test"
$?
''').trim()
return scriptOutput == 'True'
}
}
steps {
pwsh "yarn test"
}
}
So here I go again searching for a good package manager.
@fob-mobility https://pnpm.io 😉