This repository has been archived by the owner on Jun 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.js
executable file
·140 lines (127 loc) · 3.69 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
const argv = require('yargs').argv;
const axios = require('axios');
const distanceInWordsToNow = require('date-fns/distance_in_words_to_now');
const Table = require('cli-table3');
const chalk = require('chalk');
const packageNames = argv._;
function getPackageDetails(name) {
const url = `https://api.npms.io/v2/package/${encodeURIComponent(name)}`;
return axios
.get(url)
.then(res => {
if (res.status !== 200) return Promise.reject(res.data.message);
return res.data;
})
.then(mapResponseToPackage)
.catch(err => {
console.error('Could not fetch package details:', name, err);
});
}
/*
Stats that are compared:
name, version, description, rating, author, modified, downloads, stars, issues, repository, dependencies
*/
function mapResponseToPackage(response) {
const {
metadata: {
name,
version = '',
description = '',
author = { name: '' },
date,
links = { repository: '' },
dependencies = {},
},
npm = { downloads: 0 },
github = { starsCount: 0, issues: { openCount: 0 } },
} = response.collected;
const [daily = 0, weekly = 0, monthly = 0] = npm.downloads.map(
data => data.count,
);
const package = {
name,
description,
version,
rating: formatRating(response.score.final),
modified: distanceInWordsToNow(date),
author: author.name,
repository: links.repository,
dependencies: Object.keys(dependencies).length,
stars: github.starsCount,
issues: github.issues.openCount,
daily,
weekly,
monthly,
};
return package;
}
function formatRating(rating = 0) {
return parseFloat(Math.round(rating * 1000) / 100).toFixed(2);
}
function printTable(packages) {
const table = new Table();
const keys = Object.keys(packages[0]);
keys.forEach(function(key, index) {
if (index === 0) {
table.push({
[chalk.red(key.toUpperCase())]: packages.map(package =>
chalk.blue(package[key]),
),
});
} else {
let highest = undefined;
let lowest = undefined;
packages.map(function(package, index) {
if (['stars', 'daily', 'weekly', 'monthly', 'rating'].includes(key)) {
if (highest === undefined) {
highest = 0;
}
if (package[key] > packages[highest][key]) {
highest = index;
}
}
if (['dependencies', 'issues'].includes(key)) {
if (lowest === undefined) {
lowest = 0;
}
if (package[key] <= packages[lowest][key]) {
lowest = index;
}
}
return package[key];
});
table.push({
[chalk.red(key.toUpperCase())]: packages.map(function(package, index) {
if (['issues', 'stars', 'daily', 'weekly', 'monthly'].includes(key)) {
package[key] = package[key].toLocaleString('en');
}
if (highest !== undefined) {
if (index === highest || package[key] === packages[highest][key]) {
return chalk.green(package[key]);
} else {
return chalk.red(package[key]);
}
} else if (lowest !== undefined) {
if (index === lowest || package[key] === packages[lowest][key]) {
return chalk.green(package[key]);
} else {
return chalk.red(package[key]);
}
} else {
return package[key];
}
}),
});
}
});
console.log(table.toString());
}
function init() {
Promise.all(packageNames.map(getPackageDetails))
.then(printTable)
.catch(err => {
console.error('Oops, looks like the comparison failed', err);
});
}
init();