Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if latest npm package version has same major version

Tags:

Say I have an NPM package:

current_version="1.2.3";
latest_version=`npm view "$package_name" version`

how can I use the semver command line tool to determine if the latest version in npm has the same major version as the current_version? Something like:

semver --same-major "$current_version" "$latest_version" 

?

Basically, what I want to do is install to the most recent version with the same major version.

Another related question - how can I find the most recent version in the NPM registry that has the same major semver version as the current_version?

https://www.npmjs.com/package/semver

like image 465
Alexander Mills Avatar asked Oct 11 '18 20:10

Alexander Mills


1 Answers

you can use carret range like this

semver -r "^$current_version" "$latest_version"

if the input is valid (the same major number), this command print the version and exit with status code 0

else this will print nothing and exit with code 1

PS: you can check for exit code with echo $?

like image 79
Mohammed Essehemy Avatar answered Nov 15 '22 11:11

Mohammed Essehemy