yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Add --if-present option for scripts

Open ulentini opened this issue 5 years ago • 15 comments

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.

ulentini avatar Mar 31 '19 09:03 ulentini

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 avatar Apr 02 '19 11:04 arcanis

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?

ulentini avatar Apr 02 '19 15:04 ulentini

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.

charud avatar Apr 08 '19 19:04 charud

This would be very much appreciated

flaviouk avatar Nov 12 '20 13:11 flaviouk

+1

Fi1osof avatar Nov 17 '20 00:11 Fi1osof

+1

iamomiid avatar Feb 15 '21 08:02 iamomiid

Any chance the build error could be checked and this passed? This would be a lovely bit of functionality.

builtbybrayne avatar Mar 31 '21 14:03 builtbybrayne

Any progress? This feature is really good

risen228 avatar May 16 '21 15:05 risen228

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

y-nk avatar Oct 27 '21 09:10 y-nk

Why did this go stale? Would love to have this available.

michaelfaith avatar May 20 '22 02:05 michaelfaith

@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.

Papooch avatar May 23 '23 10:05 Papooch

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.

fob-mobility avatar Jun 28 '23 19:06 fob-mobility

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.

danielbayley avatar Oct 24 '23 11:10 danielbayley

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"
    }
}

ryanewtaylor avatar Jan 05 '24 22:01 ryanewtaylor

So here I go again searching for a good package manager.

@fob-mobility https://pnpm.io 😉

danielbayley avatar Jan 06 '24 13:01 danielbayley