This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathload-npm-data.js
More file actions
75 lines (61 loc) · 2.2 KB
/
load-npm-data.js
File metadata and controls
75 lines (61 loc) · 2.2 KB
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
// if your GitHub location field matches this then we'll guess you're Aussie
const GITHUB_REPO_REGEX = /github.com[:\/]([\.\-\w]+)\/([^$\/\.]+)/
, NPM_ALL_PACKAGES_URL = 'https://skimdb.npmjs.com/registry/_all_docs'
, NPM_SINGLE_PACKAGE_URL = 'https://registry.npmjs.org/{packageId}/latest'
, request = require('request').defaults({json:true})
, async = require('async');
function matchGitHubRepo (npmPackage, repo) {
var match = repo
&& typeof repo.url == 'string'
&& repo.url.match(GITHUB_REPO_REGEX);
return match && {
githubUser : match[1]
, githubRepo : match[2]
, npmPackage : npmPackage
};
}
function getPackageData(repositories, allPackages, packageData, callback){
request(NPM_SINGLE_PACKAGE_URL.replace('{packageId}', packageData.id), function (err, response, data) {
if (err) {
// log and continue usually just a timeout, possibly needs retry logic
console.log('error getting data for package: ' + packageData.id, err.message);
return callback();
}
// Bad maintainers property there are MANY just skip for much speed increase
if(!data.maintainers || !Array.isArray(data.maintainers)){
return callback();
}
var repo = matchGitHubRepo(data.name, data.repository);
if (repo){
repositories.push(repo);
}
allPackages.push({
name : data.name
, maintainers : (data.maintainers || []).map(function (m) { return m && m.name })
, githubUser : repo ? repo.githubUser : null
, githubRepo : repo ? repo.githubRepo : null
, description : data.description
});
callback();
});
}
function getAllPackages(callback){
var repositories = []
, allPackages = [];
// https://github.com/npm/npm-registry-couchapp/issues/162
request(NPM_ALL_PACKAGES_URL, function(err, response, body){
if (err) {
return callback(err);
}
if(!body || !body.rows){
body = { rows: [] };
}
async.mapLimit(body.rows, 10, getPackageData.bind(null, repositories, allPackages), function(err){
if (err) {
return callback(err);
}
callback(null, { repositories: repositories, allPackages: allPackages })
})
});
}
module.exports = getAllPackages