From cf7d706e5aec9ef4d52c768c2d14e3a9f4f59d2b Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 9 Dec 2015 11:17:58 -0500 Subject: [PATCH] feat(output): prints nice table with relative age for each release, closes #11 --- .gitignore | 1 + README.md | 17 +++++++++++++++++ bin/available.js | 22 +++++++++++++++++----- package.json | 1 + src/available.js | 27 ++++++++++++++++++++------- 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index d135d46..b5a6826 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ cover/ +.DS_Store diff --git a/README.md b/README.md index d1594d5..50a2842 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,23 @@ What are new releases after `1.0.0`? releases lazy-ass@1.0.0 releases lazy-ass 1.0.0 +## Output + +A table with versios, timestamps and relative age + +``` +$ vers babel@6.1.15 +babel since 6.1.15 +--------------------------------------------------- +version timestamp age +------- --------------------------------- ------- +6.1.16 Thu Nov 12 2015 16:34:40 GMT-0500 a month +6.1.17 Thu Nov 12 2015 16:40:45 GMT-0500 a month +6.1.18 Thu Nov 12 2015 16:46:50 GMT-0500 a month +6.2.4 Tue Nov 24 2015 22:12:59 GMT-0500 15 days +6.3.13 Fri Dec 04 2015 06:57:56 GMT-0500 5 days +``` + ## API You can use this module from other modules diff --git a/bin/available.js b/bin/available.js index f844e65..8549bfe 100755 --- a/bin/available.js +++ b/bin/available.js @@ -5,6 +5,7 @@ const la = require('lazy-ass'); const check = require('check-more-types'); require('console.table'); +const moment = require('moment'); var help = [ 'USE: jso ', @@ -37,10 +38,20 @@ const options = { version: version }; -function versionToInfo(v) { - return { - version: v - }; +function toHumanFormat(versions, timestamps) { + la(check.array(versions), 'invalid versions', versions); + la(check.array(timestamps), 'invalid timestamps', timestamps); + la(versions.length === timestamps.length, + 'mismatch in numbers', versions, timestamps); + const now = moment(); + return versions.map(function (version, k) { + const t = moment(timestamps[k]); + return { + version: version, + timestamp: t, + age: moment.duration(now.diff(t)).humanize() + }; + }); } function printReleases(query, releases) { @@ -49,7 +60,8 @@ function printReleases(query, releases) { la(check.unemptyString(releases.name), 'missing name in', releases); la(check.array(releases.versions), 'no versions in', releases); - const humanInfo = releases.versions.map(versionToInfo); + const humanInfo = toHumanFormat(releases.versions, releases.timestamps); + la(check.array(humanInfo), 'could not construct human output from', releases); const title = options.version ? releases.name + ' since ' + options.version : diff --git a/package.json b/package.json index bdb9a1e..c0fc9fe 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "console.table": "0.4.0", "lazy-ass": "1.3.0", "lodash": "3.10.1", + "moment": "2.10.6", "npm-utils": "1.0.0", "q": "1.4.1", "request": "2.67.0", diff --git a/src/available.js b/src/available.js index 1211ca5..14793e7 100644 --- a/src/available.js +++ b/src/available.js @@ -23,6 +23,16 @@ function queryRegistry(query, silent, npmUrl) { request.get(url, onNPMversions); + function getTimestamps(info, versions) { + if (!check.object(info.time)) { + return; + } + la(check.array(versions), 'expected list of versions', versions); + return versions.map(function (v) { + return info.time[v]; + }); + } + function onNPMversions(err, response, body) { if (err) { console.error('ERROR when fetching info for package', name); @@ -38,12 +48,10 @@ function queryRegistry(query, silent, npmUrl) { deferred.reject(str); return; } - var versions; - if (info.time) { - versions = Object.keys(info.time); - } else if (info.versions) { - versions = Object.keys(info.versions); - } + var versionObject = info.versions || info.time; + la(check.object(versionObject), 'could not find versions in', info); + + var versions = Object.keys(versionObject); if (!Array.isArray(versions)) { throw new Error('Could not get versions for ' + name + ' from ' + info); } @@ -51,6 +59,7 @@ function queryRegistry(query, silent, npmUrl) { var validVersions = versions.filter(function (ver) { return cleanVersion(ver, name, silent); }); + if (query.version) { la(check.string(query.version), 'missing version string, have', query.version); validVersions = validVersions.filter(function (ver) { @@ -59,10 +68,14 @@ function queryRegistry(query, silent, npmUrl) { }); } + var timestamps = getTimestamps(info, validVersions); + deferred.resolve({ name: name, - versions: validVersions + versions: validVersions, + timestamps: timestamps }); + return; } catch (err) { console.error(err);