-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (52 loc) · 1.4 KB
/
index.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
import alfy from 'alfy';
const getPackages = async (q) => await alfy.fetch('https://api.npms.io/v2/search', {
query: {
q,
size: 10,
}
});
const getPackageSize = async (pkg) => await alfy.fetch('https://bundlephobia.com/api/size', {
query: {
package: pkg,
}
});
// human readible file size
function hfs(size) {
var i = Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};
(async () => {
const packages = await getPackages(alfy.input);
if (packages.results.length > 1 && packages.results[0].searchScore < 100000) {
const items = packages.results
.filter(result => result.package.name.length > 1)
.map(result => {
const { name, description, version, links: { npm } } = result.package;
const { final: rank } = result.score;
return {
title: `${name}@${version}`,
subtitle: description,
arg: npm,
mods: {
cmd: {
subtitle: `Trust rating: ${(rank * 100).toFixed(2)}%`,
},
}
};
});
alfy.output(items);
} else {
const packageName = packages.results[0].package.name;
const { name, size, gzip, version, repository, description } = await getPackageSize(packageName);
alfy.output([{
title: `${name}@${version}`,
subtitle: `${hfs(size)}, ${hfs(gzip)} GZIPPED`,
arg: repository,
mods: {
cmd: {
subtitle: description,
},
}
}]);
}
})();