See exact publish date of an npm package

做~自己de王妃 提交于 2020-07-29 12:53:07

问题


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?


回答1:


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:

  1. Runs the npm view express time --json command and pipes the JSON to a nodejs script.
  2. The nodejs script utilizes the builtin process.stdin to read the piped JSON from stdin (fd 0).
  3. We then utilize JSON.parse to parse the JSON string, and obtain the value of the property/key named 4.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


来源:https://stackoverflow.com/questions/53148989/see-exact-publish-date-of-an-npm-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!