json 如何找出实际安装的 bower 包的版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35368144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:16:55  来源:igfitidea点击:

How do I find out what version of a bower package is actually installed?

jsonbowerversioningsemantic-versioning

提问by Andrew Ferrier

Normally a bower.jsonfile 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).

通常一个bower.json文件指定了一些依赖项,但这些通常被表达为允许使用一系列版本的 bower 包(例如>=1.0,这意味着任何高于 1.0 的版本)。

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

我有一个自动化过程,需要查找当前系统上实际安装了哪个版本的 Bower 软件包。

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

我如何以编程方式(仅版本本身)找到它,理想情况下使用标准 Unix 命令行工具/ bower 命令?

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

bower info <thepackagename>没有表明这一点-它显示了什么是当前可从凉亭库信息(例如,即使我做bower info apackageIdonthaveinstalled它仍然会显示一个包含版本号的有效的JSON结构)。

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

cat bower_components/thepackagename/bower.json | node_modules/json/lib/json.js version适用于某些软件包(假设json安装了 npm 软件包),但不是全部(例如 jquery 2.2.0 的 bower 软件包不包含 bower.json)。

采纳答案by Andrew Ferrier

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

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

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

(注意额外..bower.json)。

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

看起来 bower 在 中存储了一些关于已安装包的元数据.bower.json,其中包括已安装的版本。

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

到目前为止我想出的最好的是:

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

(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 listgets information about all installed packages, not just the one I'm interested in - the rest of the information is discarded.

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

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

(例如,如果我感兴趣的包是jquery)。

由于各种原因,这非常难看:

  • 我必须重复包名(虽然这可能可以通过更好的 Perl 脚本来改进,它也过滤行,我只是懒惰)。

  • bower list获取有关所有已安装软件包的信息,而不仅仅是我感兴趣的信息 - 其余信息将被丢弃。

  • bower list似乎需要互联网连接来检查注册表,否则它会失败。

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

有兴趣看看这是否可以改进,尤其是最后一点。

回答by Artem Vasiliev

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

这是执行此操作的 grep 命令: 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

此外,还有一个用于查看项目所有 bower 组件版本的命令——这个列表可以是一个方便的 CI 人工制品: grep "version\"\:" bower_components/*/.bower.json 在此处输入图片说明

回答by yujohnnyzhou

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

您是否尝试过“凉亭列表 --json=0 --offline”。

It would list all bower packages info.

它将列出所有凉亭包信息。