Skip to content

Commit

Permalink
feat(output): prints nice table with relative age for each release, c…
Browse files Browse the repository at this point in the history
…loses #11
  • Loading branch information
bahmutov committed Dec 9, 2015
1 parent ad8eea6 commit cf7d706
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
cover/
.DS_Store
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions bin/available.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <property name prefix>',
Expand Down Expand Up @@ -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) {
Expand All @@ -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 :
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 20 additions & 7 deletions src/available.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -38,19 +48,18 @@ 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);
}

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) {
Expand All @@ -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);
Expand Down

0 comments on commit cf7d706

Please sign in to comment.