How do I find out what version of a bower package

2019-04-17 22:49发布

Normally a bower.json file specifies some dependencies, but these are typically expressed so that they allow a range of versions of a bower package to be used (e.g. >=1.0, which means anything higher than version 1.0).

I have an automated process which needs to find what version of a bower package is actually installed on this system right now.

How can I find this out programmatically (just the version itself), ideally using standard Unix command line tools / the bower command?

bower info <thepackagename> does not show this - it shows information about what is currently available from the bower repository (for example, even if I do bower info apackageIdonthaveinstalled it will still show a valid JSON structure containing a version number).

cat bower_components/thepackagename/bower.json | node_modules/json/lib/json.js version works for some packages (assuming the npm package json is installed), but not all (e.g. jquery 2.2.0's bower package does not contain a bower.json).

3条回答
Juvenile、少年°
2楼-- · 2019-04-17 23:09

The best approach I've now found, which seems to work for every package I've come across so far, is:

cat bower_components/thepackagename/.bower.json | node_modules/json/lib/json.js version

(note the extra . in .bower.json).

It would appear that bower stores some metadata about the installed package in .bower.json, and that includes the installed version.

The best I've come up with so far is:

bower list | grep jquery | perl -pe 's/.*jquery#(.*?) .*$/$1/'

(if, for example, the package I was interested in was jquery).

That's pretty ugly for a variety of reasons:

  • I have to repeat the package name (although this could probably be improved with a better Perl script which filters lines too, I'm just being lazy).

  • bower list gets information about all installed packages, not just the one I'm interested in - the rest of the information is discarded.

  • bower list seems to require internet connectivity to check the registry, otherwise it fails.

Would be interested to see if this could be improved upon, particularly the last point.

查看更多
放荡不羁爱自由
3楼-- · 2019-04-17 23:10

Have you ever tried "bower list --json=0 --offline".

It would list all bower packages info.

查看更多
对你真心纯属浪费
4楼-- · 2019-04-17 23:17

Here's a grep command to do that: grep "version\"\:" bower_components/thepackagename/.bower.json

Also, a command to see versions of all bower components for the project - this list can be a handy CI artefact: grep "version\"\:" bower_components/*/.bower.json enter image description here

查看更多
登录 后发表回答