查看 npm 包的确切发布日期 [英] See exact publish date of an npm package
问题描述
使用 npm 6.x,当我使用 npm view [package]
命令时,我可以看到相对于今天的发布日期,例如:
$ npm view expressexpress@4.16.4 |麻省理工|深度:30 |版本:261[...关于快递包裹的其他信息]3 周前由 dougwilson <doug@somethingdoug.com> 发表;
如何以 YYYY-MM-DD HH:mm:ss 等格式获取确切的发布日期时间?
tl;dr - 利用下面解决方案"部分中提供的解决方案.>
使用npm view
,您可以运行以下命令:
$ npm view express time --json
这会将类似于以下内容的内容记录到控制台:
<块引用>{修改":2018-10-31T23:01:06.660Z",创建":2010-12-29T19:38:25.450Z",0.14.0":2010-12-29T19:38:25.450Z",0.14.1":2010-12-29T19:38:25.450Z",...4.16.3":2018-03-12T17:50:14.119Z",4.16.4":2018-10-11T03:59:14.308Z",5.0.0-alpha.7":2018-10-27T03:12:11.060Z"}
如您所见,命令 (above) 返回一个包含属性的 JSON 对象;modified
、created
,还有每个版本的属性(例如0.14.0"
、0.14.1";
等...).每个属性的关联值是一个日期.
如何获取特定版本的发布日期?
npm-view
的文档说明如下;
您可以通过用句点分隔来查看子字段.
因此,获取modified
和created
的值(即日期),您可以分别运行以下任一命令:
$ npm view express time.modified# 打印 -->`2018-10-31T23:01:06.660Z`
和
$ npm view express time.created# 打印 -->`2010-12-29T19:38:25.450Z`
但是,在获取特定版本属性/键(例如 4.16.4
)的值/日期时,您需要使用不同的方法,因为诸如以下的命令执行不工作:
# 这不起作用...$ npm view express time.4.16.4
# 这也不起作用...$ npm view express time.'4.16.4'
# 这也不起作用...$ npm view express time[4.16.4"]
解决方案:
以下命令演示了如何成功获取express
包:
$ npm view express time --json |node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['4.16.4'])});";# 打印:-->2018-10-11T03:59:14.308Z
注意:您需要根据需要将 '4.16.4'
部分替换为适当的版本.
这个解决方案:
- 运行
npm view express time --json
命令并将 JSON 通过管道传输到 nodejs 脚本. - nodejs 脚本使用内置的
process.stdin
从stdin
(fd 0) 读取管道 JSON. - 然后我们使用
JSON.parse
解析JSON字符串,获取名为4.16.4
的property/key的值
注意
如果您想要最新版本的发布日期,您可以运行以下两个 bash 命令:
$ version=$(npm 查看 express 版本)$ npm view express time --json |node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['"$version"'])});"# 打印:-->2018-10-11T03:59:14.308Z
这里我们首先运行npm view express version
(获取最新版本号)并将返回的值赋给名为version
的变量(即我们利用命令替换).然后我们在节点脚本中引用 version
值.
您还可以使用 &&
操作符链接这两个命令以形成一行命令,如下所示:
$ version=$(npm view express version) &&npm view express time --json |node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['"$version"'])});";# 打印:-->2018-10-11T03:59:14.308Z
With npm 6.x, when I use the npm view [package]
command, I can see the publish date relative to today, for example:
$ npm view express
express@4.16.4 | MIT | deps: 30 | versions: 261
[... other info about express package ]
published 3 weeks ago by dougwilson <doug@somethingdoug.com>
How can I get the exact publish date-time in a format such as YYYY-MM-DD HH:mm:ss?
tl;dr - Utilize the solution provided in the "Solution" section below.
Using npm view
you can run the following command:
$ npm view express time --json
This logs something like the following to the console:
{ "modified": "2018-10-31T23:01:06.660Z", "created": "2010-12-29T19:38:25.450Z", "0.14.0": "2010-12-29T19:38:25.450Z", "0.14.1": "2010-12-29T19:38:25.450Z", ... "4.16.3": "2018-03-12T17:50:14.119Z", "4.16.4": "2018-10-11T03:59:14.308Z", "5.0.0-alpha.7": "2018-10-27T03:12:11.060Z" }
As you can see, the command (above) returns a JSON object containing properties; modified
, created
, and also has properties for each version (e.g. "0.14.0"
, "0.14.1"
, etc... ). The associated value for each property is a date.
How to obtain the published date for a specific version?
The docs for npm-view
state the following;
You can view child fields by separating them with a period.
Therefore, obtaining the values (i.e. dates) for modified
and created
you can run either of the following commands respectively:
$ npm view express time.modified
# prints --> `2018-10-31T23:01:06.660Z`
and
$ npm view express time.created
# prints --> `2010-12-29T19:38:25.450Z`
However, when obtaining the value/date for a specific version property/key such as 4.16.4
you'll need to use a different approach, because commands such as the following do not work:
# This does not work...
$ npm view express time.4.16.4
# This also does not work...
$ npm view express time.'4.16.4'
# This does not work either...
$ npm view express time["4.16.4"]
Solution:
The following command demonstrates how to successfully obtain the published date for version 4.16.4
of the express
package:
$ npm view express time --json | node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['4.16.4'])});"
# prints: --> 2018-10-11T03:59:14.308Z
Note: You'll need to replace the '4.16.4'
part with the appropriate version as required.
This solution:
- Runs the
npm view express time --json
command and pipes the JSON to a nodejs script. - The nodejs script utilizes the builtin
process.stdin
to read the piped JSON fromstdin
(fd 0). - We then utilize
JSON.parse
to parse the JSON string, and obtain the value of the property/key named4.16.4
Note
If you want the published date for the latest version you could run the following two bash commands:
$ version=$(npm view express version)
$ npm view express time --json | node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['"$version"'])});"
# prints: --> 2018-10-11T03:59:14.308Z
Here we firstly run npm view express version
(to obtain the latest version number) and assign the value returned to the variable named version
(i.e. we utilize Command Substitution). Then we reference the version
value in the node script.
You can also chain the two commands using the &&
operator to form a one line command as follows:
$ version=$(npm view express version) && npm view express time --json | node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['"$version"'])});"
# prints: --> 2018-10-11T03:59:14.308Z
这篇关于查看 npm 包的确切发布日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!