-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (37 loc) · 795 Bytes
/
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
'use strict';
const npmStats = require('npm-stats');
const sortObject = require('sort-object');
const getDependents = el => new Promise((resolve, reject) => {
npmStats()
.module(el)
.dependents((err, res) => {
if (err) {
reject(err);
}
resolve(res);
});
});
const getPackages = user => new Promise((resolve, reject) => {
npmStats()
.user(user)
.list((err, res) => {
if (err) {
reject(err);
}
resolve(res);
});
});
module.exports = user => {
const ret = {};
if (!user) {
return Promise.reject(new Error('You have to provide a user'));
}
return getPackages(user)
.then(data => Promise.all(data.map(el => getDependents(el).then(res => {
if (res.length === 0) {
return;
}
ret[el] = res;
})))
.then(() => sortObject(ret)));
};