Npm-check-updates: Return 1 when nothing is updated

Created on 14 Apr 2020  路  15Comments  路  Source: raineorshine/npm-check-updates

  • [x] I have read the list of known issues before filing this issue.
  • [x] I have searched for similiar issues before filing this issue.

  • node version: 12.16.2

  • npm version: 6.14.4
  • npm-check-updates version: 4.1.2

What about returning 1 when nothing is updated?

How to know that something was updated. I have a chain of CLI commands and I want to stop the chain if nothing is done.

However when I run

ncu -u && echo 1

It prints 1 even if nothing is updated

faq

Most helpful comment

ncu -e 2 || (ncu -u && git add -a -m "update packages" && git push)

might do what you're looking for

edit: seems like -u and -e can't be used in combination

All 15 comments

Maybe you can use the error-level option:

-e, --error-level
Set the error-level. 1: exits with error code 0 if no errors occur. 2: exits with error code 0 if no packages need updating (useful for continuous integration)

ncu -e 2

~Does either error level work for you? I can鈥檛 tell if that鈥檚 a typo in the README or not. I鈥檒l have to try it myself in the morning.~

Thanks, I think it should be that. Now I cannot test it cause I'm in a different computer, but can I combine both parameters in a single command?

ncu -u -e 2 && echo 1

Or shall I chain it?

ncu -e 2 && ncu -u

Tested now and it does not fit my purpose

$ ncu -u -e 0 && echo 1
Upgrading /home/joao/autocosts/package.json
[====================] 51/51 100%

All dependencies match the latest package versions :)
1

I want to return 0 (success) only if packages are updatable

I'm using for a chain like that

ncu -u && git add -a -m "update packages" && git push

How to return 0 only if the packages were indeed updated (that is, package.json written)?

Maybe I'm missing some unix bash trick

ncu -e 2 || (ncu -u && git add -a -m "update packages" && git push)

might do what you're looking for

edit: seems like -u and -e can't be used in combination

That鈥檚 strange. I鈥檒l open an issue for that.

@stoically that's it, you may close the issue. But you could add an extra option 3 for error to avoid this bash tweak.

@jfoclpf @stoically Can you try npm install -g npm-check-updates@next to test out the fix? It's going to be a major version upgrade, so I want to make sure it works as intended. The tests pass.

@raineorshine Seems like the npm registry lags behind for me, can't get @next. However,

npx npm-check-updates@tjunnone/npm-check-updates -u -e 2

works as expected. :+1:

I also confirm that
npx npm-check-updates@tjunnone/npm-check-updates -u -e 2
works as expected, nonetheless I would like to have a -e 3 that outputs success only when packages are updated or updatable.

I would like to have a -e 3 that outputs success only when packages are updated or updatable.

We'll leave that to bash: use the ! operator.

I would like to have a -e 3 that outputs success only when packages are updated or updatable.

We'll leave that to bash: use the ! operator.

It's not that simple in command line, you need || operator and parentheses

$ true || echo howdy
$ true || echo howdy && echo hey
hey
$ true || (echo howdy && echo hey)
$ false || (echo howdy && echo hey)
howdy
hey

In my package.json now I use these two extra scripts

"updatePackages": "ncu -e 2 || (ncu -u && npm i --ignore-scripts && npm run commitAndUpload)",
"commitAndUpload": "npm test && git commit -a -m 'update' && npm version patch && npm publish && git push"`

Install v5.0.0-alpha.2:

$ npm install -g [email protected]

Use || operator with parentheses:

"updatePackages": "ncu -u -e 2 || (npm i --ignore-scripts && npm run commitAndUpload)"

Or use ! operator:

"updatePackages": "! ncu -u -e 2 && npm i --ignore-scripts && npm run commitAndUpload"

thanks @raineorshine , I was missing the space between ! and ncu

No worries... bash is an enigma!

Published in v5.0.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eddieedease picture eddieedease  路  4Comments

HitomiTenshi picture HitomiTenshi  路  6Comments

mkungla picture mkungla  路  6Comments

fatso83 picture fatso83  路  3Comments

Eschon picture Eschon  路  7Comments